Magnetic fiel calculation in C [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 days ago.
Improve this question
I am trying to calculate the magnetic field of a plane of magnetics dipols and its evolution over time but the orders of the fields seems to be a bit out of order and I do not see how it is wrong.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define X 31
#define Y 31
#define T 10
int main(){
double Mx[T][X][Y];
double My[T][X][Y];
double Mz[T][X][Y];
double mx[T][X][Y];
double my[T][X][Y];
double mz[T][X][Y];
double Hdipx[T][X][Y]={0};
double Hdipy[T][X][Y]={0};
double Hdipz[T][X][Y]={0};
double Hexx[T][X][Y];
double Hexy[T][X][Y];
double Hexz[T][X][Y];
double Hx[T][X][Y];
double Hy[T][X][Y];
double Hz[T][X][Y];
double renorma[T][X][Y];
double Ms= 1.0*pow(10,6); //A m^-1
double Amin=10.0*pow(10,-12); //J m^-1
double Amax=20.0*pow(10,-12); //J m^-1
double u0=1.26*pow(10,-6); //T*m/A
double x0=9*pow(10,-9);
double lex=sqrt(2)*x0; //asi mas o menos la placa tiene 400nm
double alpha=0.1/pow(Ms,2);
double a=0.1;
int i,j,k,l,n;
double z={0};
double R[X][Y]={0};
double Rx[X][Y]={0};
double Ry[X][Y]={0};
double kappa=2*Amax/(pow(Ms,2)*u0);
for(i=0;i<X;i++){
for(j=0;j<Y;j++){
Rx[i][j]=i*lex;
Ry[i][j]=j*lex;
R[i][j]=sqrt(Rx[i][j]*Rx[i][j]+Ry[i][j]*Ry[i][j]);
}
}
//C.I
for(i=0;i<X;i++){
for(j=0;j<Y;j++){
if(j==15 && i==15){
Mz[0][i][j]=1;
}
else{
Mz[0][i][j]=0;
}
}
}
for(i=0;i<X;i++){
for(j=0;j<Y;j++){
if(j==15 && i==15){
Mx[0][i][j]=0;
My[0][i][j]=0;
}
if(j<=15 && i<=15){
Mx[0][i][j]=1;
My[0][i][j]=0;
}
if(j<15 && i>15){
Mx[0][i][j]=0;
My[0][i][j]=1;
}
if(j>=15 && i>=15){
Mx[0][i][j]=-1;
My[0][i][j]=0;
}
if(j>15 && i<15){
Mx[0][i][j]=0;
My[0][i][j]=-1;
}
}
}
for(i=1;i<X-1;i++){
for(j=1;j<Y-1;j++){
mx[0][i][j]=Mx[0][i][j]*lex*lex*lex;
my[0][i][j]=My[0][i][j]*lex*lex*lex;
mz[0][i][j]=Mz[0][i][j]*lex*lex*lex;
}
}
for(i=1;i<X-1;i++){
for(j=1;j<Y-1;j++){
for(k=i-10;k<i+10;k++){
for(l=j-10;l<j+10;l++){
if (R[k][l] != R[i][j]) {
Hdipx[0][i][j]+=(3*(Rx[k][l]-Rx[i][j])*(mx[0][k][l]*(Rx[k][l]-Rx[i][j])+my[0][k][l]*(Ry[k][l]-Ry[i][j])))/((4*M_PI)*fabs(pow(R[k][l]-R[i][j],5)))-mx[0][k][l]/((4*M_PI)*fabs(pow(R[k][l]-R[i][j],3)));
Hdipy[0][i][j]+=(3*(Ry[k][l]-Ry[i][j])*(mx[0][k][l]*(Rx[k][l]-Rx[i][j])+my[0][k][l]*(Ry[k][l]-Ry[i][j])))/((4*M_PI)*fabs(pow(R[k][l]-R[i][j],5)))-my[0][k][l]/((4*M_PI)*fabs(pow(R[k][l]-R[i][j],3)));
Hdipz[0][i][j]+=(-1.0*mz[0][k][l])/(fabs(pow(R[k][l]-R[i][j],3))*(4*M_PI));
}
}
}
Hexx[0][i][j]=(kappa/(lex*lex))*(Mx[0][i+1][j]-2*Mx[0][i][j]+Mx[0][i-1][j]+Mx[0][i][j+1]-2*Mx[0][i][j]+Mx[0][i][j-1]);
Hexy[0][i][j]=(kappa/(lex*lex))*(My[0][i+1][j]-2*My[0][i][j]+My[0][i-1][j]+My[0][i][j+1]-2*My[0][i][j]+My[0][i][j-1]);
Hexz[0][i][j]=(kappa/(lex*lex))*(Mz[0][i+1][j]-2*Mz[0][i][j]+Mz[0][i-1][j]+Mz[0][i][j+1]-2*Mz[0][i][j]+Mz[0][i][j-1]);
Hx[0][i][j]=Hdipx[0][i][j]+Hexx[0][i][j];
Hy[0][i][j]=Hdipy[0][i][j]+Hexy[0][i][j];
Hz[0][i][j]=Hdipz[0][i][j]+Hexz[0][i][j];
}
}
/*
printf("%lf \n",Hdipx[0][15][9]);
printf("%lf \n",Hexx[0][15][9]);
printf("%lf \n",Mx[0][15][9]);
printf("%lf \n",Hdipy[0][15][9]);
printf("%lf \n",Hdipz[0][15][9]);
printf("%lf \n",pow(10,9)*(Rx[10][20]-Rx[1][1]));
printf("%lf \n",pow(10,9)*(R[10][20]-R[1][1]));
printf("%lf \n",pow(10,9)*(Ry[10][20]-Ry[1][1]));
*/
for(i=1;i<X-1;i++){
for(j=1;j<Y-1;j++){
printf("%lf \n",Mx[0][i][j]);
}
}
for(k=0;k<T-1;k++){
for(j=1;j<Y-1;j++){
for(i=1;i<X-1;i++){
Mx[k+1][i][j]=Mx[k][i][j]-a*Mx[k][i][j]*(Hx[k][i][j]*Mx[k][i][j]+Hy[k][i][j]*My[k][i][j]+Hz[k][i][j]*Mz[k][i][j])+a*Hx[k][i][j];
My[k+1][i][j]=My[k][i][j]-a*My[k][i][j]*(Hx[k][i][j]*Mx[k][i][j]+Hy[k][i][j]*My[k][i][j]+Hz[k][i][j]*Mz[k][i][j])+a*Hy[k][i][j];
Mz[k+1][i][j]=Mz[k][i][j]-a*Mz[k][i][j]*(Hx[k][i][j]*Mx[k][i][j]+Hy[k][i][j]*My[k][i][j]+Hz[k][i][j]*Mz[k][i][j])+a*Hz[k][i][j];
}
}
for(j=0;j<Y;j++){
Mx[k+1][0][j]=0;
My[k+1][0][j]=0;
Mz[k+1][0][j]=0;
}
for(j=0;j<Y;j++){
Mx[k+1][X-1][j]=0;
My[k+1][X-1][j]=0;
Mz[k+1][X-1][j]=0;
}
for(i=0;i<X;i++){
Mx[k+1][i][0]=0;
My[k+1][i][0]=0;
Mz[k+1][i][0]=0;
}
for(i=0;i<X;i++){
Mx[k+1][i][Y-1]=0;
My[k+1][i][Y-1]=0;
Mz[k+1][i][Y-1]=0;
}
for(j=1;j<Y-1;j++)
for(i=1;i<X-1;i++){
renorma[k+1][i][j]=sqrt(Mx[k+1][i][j]*Mx[k+1][i][j]+My[k+1][i][j]*My[k+1][i][j]+Mz[k+1][i][j]*Mz[k+1][i][j]);
Mx[k+1][i][j]=Mx[k+1][i][j]/renorma[k+1][i][j];
My[k+1][i][j]=My[k+1][i][j]/renorma[k+1][i][j];
Mz[k+1][i][j]=Mz[k+1][i][j]/renorma[k+1][i][j];
}
for(j=1;j<Y-1;j++){
for(i=1;i<X-1;i++){
mx[k+1][i][j]=Mx[k+1][i][j]*lex*lex*lex;
my[k+1][i][j]=My[k+1][i][j]*lex*lex*lex;
mz[k+1][i][j]=Mz[k+1][i][j]*lex*lex*lex;
}
}
for(i=1;i<X-1;i++){
for(j=1;j<Y-1;j++){
for(n=1;n<X-1;n++){
for(l=1;l<Y-1;l++){
if (R[n][l] != R[i][j]){
Hdipx[k+1][i][j]+=(1/(4*M_PI))*((3*(Rx[n][l]-Rx[i][j])*(mx[k+1][n][l]*(Rx[n][l]-Rx[i][j])+my[k+1][n][l]*(Ry[n][l]-Ry[i][j])))/fabs(pow(R[n][l]-R[i][j],5))-mx[k+1][n][l]/fabs(pow(R[n][l]-R[i][j],3)));
Hdipy[k+1][i][j]+=(1/(4*M_PI))*((3*(Ry[n][l]-Ry[i][j])*(mx[k+1][n][l]*(Rx[n][l]-Rx[i][j])+my[k+1][n][l]*(Ry[n][l]-Ry[i][j])))/fabs(pow(R[n][l]-R[i][j],5))-my[k+1][n][l]/fabs(pow(R[n][l]-R[i][j],3)));
Hdipz[k+1][i][j]+=(1/(4*M_PI))*(-1.0*mz[k+1][n][l]/fabs(pow(R[n][l]-R[i][j],3)));
}
}
}
Hexx[k+1][i][j]=(kappa/(lex*lex))*(Mx[k+1][i+1][j]-2*Mx[k+1][i][j]+Mx[k+1][i-1][j]+Mx[k+1][i][j+1]-2*Mx[k+1][i][j]+Mx[k+1][i][j-1]);
Hexy[k+1][i][j]=(kappa/(lex*lex))*(My[k+1][i+1][j]-2*My[k+1][i][j]+My[k+1][i-1][j]+My[k+1][i][j+1]-2*My[k+1][i][j]+My[k+1][i][j-1]);
Hexz[k+1][i][j]=(kappa/(lex*lex))*(Mz[k+1][i+1][j]-2*Mz[k+1][i][j]+Mz[k+1][i-1][j]+Mz[k+1][i][j+1]-2*Mz[k+1][i][j]+Mz[k+1][i][j-1]);
Hx[k+1][i][j]=Hdipx[k+1][i][j]+Hexx[k+1][i][j];
Hy[k+1][i][j]=Hdipy[k+1][i][j]+Hexy[k+1][i][j];
Hz[k+1][i][j]=Hdipz[k+1][i][j]+Hexz[k+1][i][j];
}
}
}
char b[30];
FILE* ficheroi;
for(i=0;i<T;i++){
sprintf(b,"1gif%i.txt",i);
ficheroi=fopen(b,"w");
for (int j = 0; j < Y; j++) {
for(k=0;k<X;k++){
fprintf(ficheroi, "%lf \t %lf \t %lf \n",pow(10,9)*Rx[k][j],pow(10,9)*Ry[k][j],z);
}
}
sprintf(b,"2gif%i.txt",i);
ficheroi=fopen(b,"w");
for (int j = 0; j < Y; j++) {
for(k=0;k<X;k++){
fprintf(ficheroi, "%lf \t %lf \t %lf \n",Mx[i][k][j]/Ms,My[i][k][j]/Ms,Mz[i][k][j]/Ms);
}
}
}
fclose(ficheroi);
return 0;
}
I tried to calculate the dipole fiel over a loop of al the other dipoles and the exchange camp only affecting their surroundings.

Related

Arithmetic and geometric average error solution

My code causes problems connected with giving the final result.
Earlier the program returned that there is an error in division by zero in geometric average. Now the program in arithmetic average returns -2.00000.
Program shows #IND00 error in SredniaGeometryczna(); — it is Geometric Average.
Do you have any idea how to solve it? Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
int ilosc(int*);
double sredniaArytmetyczna(int, int*);
double sredniaGeometryczna(int, int*);
double sredniaHarmoniczna(int, int*);
int main()
{
int tab[10];
char wybor;
printf("Wybierz, ktora srednia chcesz policzyc: [A]rytmetyczna, [G]eometryczna, [H]armoniczna.\n");
wybor=getch();
int ile;
long double srednia;
switch(wybor)
{
case 'a':
case 'A':
//instrukcje dla arytmetycznej
ile=ilosc(tab);
srednia=sredniaArytmetyczna(ile, tab);
printf("Srednia to: %f",srednia);
//tab* == tab[0]
//printf("Srednia to: %f", sredniaArytmetyczna(ilosc(tab),tab)); - mozna tak samo zrobic za pomoca jednej linijki
break;
case 'g':
case 'G':
//instrukcje dla geometrycznej
ile=ilosc(tab);
srednia=sredniaGeometryczna(ile, tab);
printf("Srednia to: %f",srednia);
break;
case 'h':
case 'H':
ile=ilosc(tab);
srednia=sredniaHarmoniczna(ile, tab);
printf("Srednia to: %f",srednia);
break;
default:
printf("Bledny wybor");
}
return 0;
}
int ilosc(int* tablica)
{
int ileLiczb, i;
printf("Podaj ile liczb chcesz wprowadzic (max 10) : \n");
scanf("%d", &ileLiczb);
i=0;
while(i<ileLiczb)
{
printf("Podaj %d liczbe calkowita: ", i+1);
scanf("%d", &tablica[i]);
++i;
}
return 0;
}
double sredniaArytmetyczna(int iloscLiczb, int *Tab)
{
double wynik=0;
for(int i=0; i<iloscLiczb; ++i)
{
wynik+=Tab[i];
}
return wynik/iloscLiczb;
}
double sredniaGeometryczna(int iloscLiczb, int *Tab)
{
double wynik=0;
for(int i=0; i<iloscLiczb; ++i)
{
wynik=Tab[i]*Tab[++i];
}
return pow(wynik,1/iloscLiczb);
}
double sredniaHarmoniczna(int iloscLiczb, int *Tab)
{
double wynik=0;
for(int i=0; i<iloscLiczb; ++i)
{
wynik+=(1/Tab[i]);
}
return iloscLiczb/wynik;
}
ilosc is supposed to return the count of numbers, but it always returns 0. Change
return 0;
to
return ileLiczb;
And when you're calculating the power in sredniaGeometryczna, you're performing integer arithmetic. You need to change 1/iloscLiczb to 1/(double)iloscLiczb so it will perform floating point arithmetic. Or you could change the declaration of the iloscLiczb parameter to double, and it will be converted automatically when the function is called.
You need a similar change in sredniaHarmoniczna. Change 1/Tab[i] to 1/(double)Tab[i]

Printf function pass by reference

I'm trying to make a simple program with C that gets name, hourly rate and hours for input and calculates gross pay, tax, net pay etc, but I got stuck as I was making a function that prints out results.
I created two loops: one loop that generates overtime, gross pay, tax and net pay. Another loop that prints out the generated data. First one works fine. It generates data just fine, but as I run the program, it simply ignores the second loop and just finish the program.
It is just a simple void function that has no return value, and yet I'm stuck. I used pass by reference. If you know what's wrong, please let me know.
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#define size 5
char name[size][20];
float rate[size];
float hours[size];
int i = 0;
void input();
float getbasepay(float *rt, float *hr);
float getovtime(float *rt, float *hr);
float getgross(float *base, float *ovt);
float gettax(float *gross);
float getnetpay(float *gross, float *tax);
void printout(char name, float rate, float hours, float base, float ovt, float gross, float tax, float net);
void input()
{
for (i = 0; i < size; i++)
{
puts("\ntype name: (type -1 to quit) \n");
scanf_s("%19s", &name[i], 20);
if (strcmp(name[i], "-1") == 0) { break; }
puts("\ntype hourly rate: (type -1 to quit) \n");
scanf_s("%f", &rate[i]);
if (rate[i] == -1) { break; }
puts("\ntype hours worked: (type -1 to quit) \n");
scanf_s("%f", &hours[i]);
if (hours[i] == -1) { break; }
}
return ;
}
float getbasepay(float *rt, float *hr)
{ float base;
float baseo;
float excessive = (*hr - 40);
if (*hr <= 40)
{
base = (*rt) * (*hr);
return base;
}
if (*hr >= 40)
{
baseo = (*hr - excessive) * (*rt);
return baseo;
}
}
float getovtime(float *rt,float *hr)
{
float time;
float ovt;
if (*hr >= 40)
{
time = (*hr - 40);
ovt = time * (*rt) *1.5;
return ovt;
}
else
{
return 0;
}
}
float getgross(float *base, float *ovt)
{
float gross = *base + *ovt;
return gross;
}
float gettax(float *gross)
{
float tax = *gross * 0.20;
return tax;
}
float getnetpay(float *gross, float *tax)
{
float net;
net = *gross - *tax;
return net;
}
void printout(char *name, float *rate, float *hours, float *base, float * ovt, float *gross, float *tax, float *net)
{
printf("\nPay to: %s \n", *name);
printf("Hourly rate: %f \n", *rate);
printf("Hours: %f \n", *hours);
printf("Base pay: $%f \n", *base);
printf("Overtime: $%f \n", *ovt);
printf("Gross Pay: $%f \n", *gross);
printf("Tax: $%f \n", *tax);
printf("Net Pay: $%f \n", *net);
}
int main(void)
{
int o, z;
float overtime[size] = { 0,0,0,0,0, };
float basepay[size] = { 0,0,0,0,0, };
float tax[size] = { 0,0,0,0,0, };
float grosspay[size] = { 0,0,0,0,0, };
float netpay[size] = { 0,0,0,0,0, };
float sum;
input();
for (o = 0; o < i; o++)
{
basepay[o] = getbasepay(&rate[o], &hours[o]);
overtime[o] = getovtime(&rate[o], &hours[o]);
grosspay[o] = getgross(&basepay[o], &overtime[o]);
tax[o] = gettax(&grosspay[o]);
netpay[o] = getnetpay(&grosspay[o], &tax[o]);
}
for (z = 0; z > i; z++)
{
printout(&name[z], &rate[z], &hours[z], &basepay[z], &overtime[z], &grosspay[z], &tax[z], &netpay[z]);
}
}
Your code doesn't compile:
gcc -o main main.c
Answers:
error: conflicting types for ‘printout’
And indeed, the signature in the declaration is different than the one you
wrote in the implementation (for example char *name instead of char name etc.)

Issue with FILE I/O and using data within

This code for tolerance analysis compiles and runs, but the output is incorrect and I believe it to be the incorrect handling of the file and the data within the file.
The data in the file is:
PART,2.000,-1,0.050,V
PART,0.975,-1,0.025,V
PART,3.000,+1,0.010,F
GAP,0.000,0.080
The code:
#include <stdlib.h>
#include <string.h>
#include <math.h>
void parsePart(char input[], float*pnom, int*pinp, float*ptol, char*pFV);
void parseGap(char input[], float *pmin, float *pmax);
float meanGap(float nom[], float imp[], int size);
float tolGap(float tol[], int size);
int main()
{
FILE *ptable;
int i, num_of_parts;
int impact[10];
float nominal[10], tolerance[10];
char FV[10];
char input_str[30];
float max, min, gap, tol;
float curr_max, curr_min;
ptable=fopen("D:\\Input.txt", "r");
for(i=0; i<11; i++);
{
fgets(input_str, 30, ptable);
if(input_str[0] == 'P')
{
parsePart(input_str, &nominal[i], &impact[i], &tolerance[i], &FV[i]);
}
else
{
parseGap(input_str, &min, &max);
num_of_parts = i;
}
}
gap = meanGap(nominal, impact, num_of_parts);
tol = tolGap(tolerance, num_of_parts);
curr_max = gap+tol;
curr_min = gap-tol;
printf("Gap mean is: %f inches\n", gap);
printf("Gap tolerance is: %f inches\n", tol);
if(fabs(max-curr_max)< 0.0001 && curr_max > (gap*2)) //they are equal
{
printf("The maximum gap is %f which is greater than the allowed %f\n", curr_max, gap*2);
}
else
{
printf("The maximum gap is %f which is within the allowed %f\n", curr_max, gap*2);
}
if(fabs(min+curr_min)<0.0001 && curr_min < gap-gap)
{
printf("The minimum gap is %f which is less than the allowed %f\n", curr_min, gap-gap);
}
else
{
printf("The minimum gap is %f which is within the allowed %f\n", curr_min, gap-gap);
}
return 0;
}
void parsePart(char input[], float*pnom, int*pinp, float*ptol, char*pFV)
{
int i;
char * field[5];
field[0]=strtok(input, ",");
for(i=1; i<5; i++);
{
field[i] = strtok(NULL, ",");
}
*pnom = atof(field[1]);
*pinp = atoi(field[2]);
*ptol = atof(field[3]);
*pFV = *field[4];
}
void parseGap(char input[], float *pmin, float *pmax)
{
char *field[2];
field[0] = strtok(input, ",");
field[1] = strtok(NULL, ",");
field[2] = strtok(NULL, ",");
*pmin = atof(field[1]);
*pmax = atof(field[2]);
}
float meanGap(float nom[], float imp[], int size)
{
int i;
float sum=0;
for(i=0; i<size; i++);
{
sum += nom[i]*imp[i];
}
return sum;
}
float tolGap(float tol[], int size)
{
int i;
float sum=0;
for(i=0; i<size; i++);
{
sum += tol[i];
}
return sum;
}
The output should look something like:
Actual Gap Mean: 0.025”
Actual Gap Tolerance: 0.085”
The Maximum Gap (0.110”) is (Greater) than specified (0.080”)
The Minimum Gap (-0.060”) is (Less) than the specified (0.000”)
I get a 0 for gap mean and all the other values are incredibly large numbers.
Any and all hints as to where I could be going wrong or need improvement are great. Thank you.
In line 85, char *field[2]; should be char *field[3];.
Firstly arrays impact et al are sized at 10, so
for(i=0; i<11; i++);
Is wrong for 2 reasons:
1) loop should go from 0 to 9
2) a for statement shouldn't have a ; after it, that terminates the whole statement.
typedef struct{char c[6];} String;
String field1,field2,… …;
String *pf1,*pf2,… …;
pf1=field1;
pf2=field2;
…
…
pf1=strtok(input,…
Is the correct (outline) way to code parseGap assuming the longest data in the input file is 5 characters long,

C Calculator with a lot of functions in functions in functions

I had programmed first an easy calculator. Now I would like to outsource the individual program components in Functions. The Problem is the switch-part.The program always gives me the default message:Bad operator. Please take a look and give me some tipps.Is something wrong with the pointers and double-pointers?
Here is my code:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
void newcalc(char*,double*,char*,double*);
double switchfunk(double**, char**, double**);
double readcalc(double**,char**,double**);
double addition(double,double);
double subtraction(double,double);
double multiplication(double,double);
double division(double,double);
int main()
{
double a=0, b=0;
char op='R', restart = 'Y';
newcalc(&restart,&a,&op,&b);
fflush(stdin);
getchar();
return 0;
}
double switchfunk(double** x, char** opp, double** y)
{
printf("\n ---CALCULATOR--- \n\n\n");
switch (**opp)
{
case '+':printf("%lf + %lf = %lf\n", **x, **y, addition(**x, **y)); break;
case '-':printf("%lf - %lf = %lf\n", **x, **y, subtraction(**x, **y)); break;
case '*':printf("%lf * %lf = %lf\n", **x, **y, multiplication(**x, **y)); break;
case '/':printf("%lf / %lf = %lf\n", **x, **y, division(**x, **y)); break;
default:printf("bad operator!");
}
return 0;
}
void newcalc(char* restart,double* x, char* opp, double* y)
{
while (restart != (char*)'N')
{
readcalc(&x, &opp, &y);
switchfunk(&x, &opp, &y);
printf("New Calc? (Y,N) \n");
fflush(stdin);
scanf("%c", &restart);
if (restart != (char*)'Y'&&restart != (char*)'N')
{
printf("Bad input!");
}
}
}
double readcalc(double** x,char** opp,double** y)
{
printf("\n Type your calculation!(z.B.4+7)\n");
scanf("%lf%c%lf",x,opp,y);
return 0;
}
double addition(double a,double b)
{
double c = 0;
c = a + b;
return c;
}
double subtraction(double a, double b)
{
double c = 0;
c = a - b;
return c;
}
double multiplication(double a, double b)
{
double c = 0;
c = a*b;
return c;
}
double division(double a, double b)
{
double c = 0;
c = a / b;
return c;
}
Best regards!
You have so many problems in your code, most of them related to your use of pointers. Here's one problem:
In the newcalc function you have the following condition in a loop:
restart != (char*)'N'
That will not work as you expect, in fact it will always be true and give you an infinite loop.
The reason is that restart is a pointer which points to the location of the local restart variable in the main function. It will never be the same as (char *) 'N' (which is a pointer pointing to address 78).
How to solve this specific problem? To start with, don't have it as an argument, declare it as a local (non-pointer!) variable:
char restart = 'y';
Then use it normally in the loop condition
while (restart == 'y' || restart == 'Y') { ... }
And to hint to more pointer problems, remember that scanf want a pointer to the variable where to store the value?
But in e.g. the readcalc function the variables x, opp and y are pointers to pointers to where the data should be stored, and yet you pass these pointers-to-pointer to scanf:
scanf("%lf%c%lf",x,opp,y);
Here, you should use pointers as argument to readcalc function, but not pointers-to-pointers.
In other words, it should be declared as
double readcalc(double*,char*,double*);
You most likely have many other problems with your (unnecessary) use of pointers and pointers-to-pointers, but these were the ones that really stood out.
I think you are misusing pointers and using addresses rather than the values themselves. Also, you are using pointers to pointers while you are not in need to do so. Take a look at the following code.
#include <stdio.h>
#include <stdlib.h>
void newcalc (char*,double*,char*,double*);
double switchfunk(double*, char*, double*);
double readcalc(double*,char*,double*);
double addition(double,double);
double subtraction(double,double);
double multiplication(double,double);
double division(double,double);
int main()
{
double a=0, b=0;
char op='R', restart = 'Y';
newcalc(&restart,&a,&op,&b);
fflush(stdin);
getchar();
return 0;
}
double switchfunk (double* x, char* opp, double* y)
{
printf("\n ---CALCULATOR--- \n\n\n");
switch ( *opp )
{
case '+':
printf("%lf + %lf = %lf\n", *x, *y, addition(*x, *y));
break;
case '-':
printf("%lf - %lf = %lf\n", *x, *y, subtraction(*x, *y));
break;
case '*':
printf("%lf * %lf = %lf\n", *x, *y, multiplication(*x, *y));
break;
case '/':
printf("%lf / %lf = %lf\n", *x, *y, division(*x, *y));
break;
default:printf("bad operator!");
}
return 0;
}
void newcalc(char* restart,double* x, char* opp, double* y)
{
while ( *restart == 'Y' || *restart == 'y')
{
readcalc(x, opp, y);
switchfunk(x, opp, y);
printf("New Calc? (Y,N) \n");
fflush(stdin);
scanf("%c", restart);
// if (restart != (char*)'Y'&&restart != (char*)'N')
// {
// printf("Bad input!");
// }
}
}
double readcalc(double* x,char* opp,double* y)
{
printf("\n Type your calculation!(z.B.4+7)\n");
scanf("%lf%c%lf",x,opp,y);
return 0;
}
double addition(double a,double b)
{
return a + b;
}
double subtraction(double a, double b)
{
return a - b;
}
double multiplication(double a, double b)
{
return a*b;
}
double division(double a, double b)
{
return a / b;
}

How to use complex header file in keil uvision 4?

Following is the program I have written
#include <stdio.h>
#include<complex.h>
#include<math.h>
int main()
{
unsigned char i,j,p,q;
complex arr[3][2]={{60 ,0},{80 ,-120},{100, -240}};
complex fabcr[3][1]={60+0i,-40-69.2820i,-50+86.6025i};
complex fa012[3][1], D[3][1],B[3][1],Vbal[3][1];
complex Vpol[3][2];
complex sum,l;
complex A[3][3]={{1 , 1 , 1}, {1 ,-0.5+0.866i ,-0.5-0.866i}, {1 ,-0.5-0.866i, -0.5+0.866i}};
complex invA[3][3]= {{0.3333 - 0.0000i , 0.3333 + 0.0000i, 0.3333},
{0.3333 ,-0.1667 - 0.2887i, -0.1667 + 0.2887i},
{ 0.3333 + 0.0000i , -0.1667 + 0.2887i , -0.1667 - 0.2887i}};
complex a=-0.5+0.866i;
//a^2 =-0.5-0.866i
printf("%f +i %f \n %f %f",creal(a),cimag(a),cabs(a),(180/M_PI)*carg(a));
printf("\n line voltage fabcr \n\n");
for(i=0;i<3;i++)
{
for(j=0;j<1;j++)
{
printf( "%f +i %f ",creal(fabcr[i][j]),cimag(fabcr[i][j]));
}
printf("\n");
}
printf("\n A matrix \n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf( "%f +i %f ",creal(A[i][j]),cimag(A[i][j]));
}
printf("\n");
}
printf("\ninvA matrix\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf( "%f +i %f ",creal(invA[i][j]),cimag(invA[i][j]));
}
printf("\n \n");
}
//matrix multiplication
for(p=0;p<3;p++)
{
sum=0;
for(q=0;q<3;q++)
{
l=fabcr[q][0]*A[p][q];
sum=sum+l;
}
printf("%f i %f \n", creal(sum),cimag(sum));
//fa012[p][1]=(1/3)*creal(sum)+i*(1/3)*cimag(sum);
fa012[p][0]=sum*0.33;
}
printf("\n symmetrical componenet fa012\n \n");
for(p=0;p<3;p++)
{
printf("%f i %f \n", creal(fa012[p][0]),cimag(fa012[p][0]));
}
D[0][0]=-fa012[0][0];
D[1][0]=0;
D[2][0]=-fa012[2][0];
printf("\n matrix D \n \n");
for(p=0;p<3;p++)
{
printf("%f i %f \n", creal(D[p][0]),cimag(D[p][0]));
}
printf("\n\nzero and negative sequence component in line voltage terms\n\n");
for(p=0;p<3;p++)
{
sum=0;
for(q=0;q<3;q++)
{
l=D[q][0]*invA[p][q];
sum=sum+l;
}
printf("%f i %f \n", creal(sum),cimag(sum));
//fa012[p][1]=(1/3)*creal(sum)+i*(1/3)*cimag(sum);
B[p][0]=sum*3;
}
printf("\n\nfinal balanced voltage \n\n") ;
for(p=0;p<3;p++)
{
Vbal[p][0]=fabcr[p][0]+B[p][0];
printf("\n %f i %f \n %f %f \n", creal( Vbal[p][0]),cimag(Vbal[p][0]),cabs(Vbal[p][0]),(180/M_PI)*carg(Vbal[p][0]));
}
printf("\n\n conversion to polar cordinates \n\n") ;
for(i=0;i<3;i++)
{
Vpol[i][0]= cabs(Vbal[i][0]);
Vpol[i][1]= (180/M_PI)*carg(Vbal[i][1]);
printf("%f %f \n",Vpol[i][0],Vpol[i][1] );
}
getch();
}
If I want to use this in keil it shows error.
Please suggest how to do complex manipulations?
I also copied complex.h and _mingw.h from devcpp include folder to keil/c51/inc folder.
You need to do four things to make your program compile using Keil uVision:
Add the switch --c99 to the compiler arguments. The reason is that armcc shipped
with Keil uVision 4 uses C90 mode for *.c files as can be seen when calling armcc in
a terminal without arguments:
--c90 Switch to C mode (default for .c files)
Change all the occurences of complex in your code to float complex or
double complex as these are the names for complex data types in C99. Look also
here, for example.
The definitions of complex numbers like -0.5+0.866i do not work. Change these
to -0.5+0.866*I.
Make your own definition of PI as it is not defined in <math.h> which is shipped
with armcc. You can do this using e.g.
#define M_PI 3.1415926535897932384626433832795
After these steps the code will at least compile. However, there are a few more problems:
You are using %f to output a complex value instead of a float in the line
printf("%f %f \n",Vpol[i][0],Vpol[i][1] );
As Vpol[i][0] is the the magnitude of a complex number and Vpol[i][1] its
argument, you might as well output cabs(Vpol[i][0]) and creal(Vpol[i][1]).
Of course, it would be even better to change the definition to float Vpol[3][2];
as you are only storing real values in this array.
The function getch() might not be part of <stdio.h>, i.e. you possibly have to
provide your own implementation before linking.
(Not really a "problem"). The variable arr is never used.
Hope that helps!
#include <stdio.h>
#include <complex.h>
#include <math.h>
#define M_PI 3.1415926535897932384626433832795
int main()
{
unsigned char i,j,p,q;
float complex fabcr[3][1] = {60+0*I,-40-69.2820*I,-50+86.6025*I};
float complex fa012[3][1], D[3][1],B[3][1],Vbal[3][1];
float Vpol[3][2];
float complex sum,l;
float complex A[3][3] = {{1, 1, 1},
{1, -0.5+0.866*I, -0.5-0.866*I},
{1, -0.5-0.866*I, -0.5+0.866*I}};
float complex invA[3][3]
= {{0.3333 - 0.0000*I, 0.3333 + 0.0000*I, 0.3333},
{0.3333, -0.1667 - 0.2887*I, -0.1667 + 0.2887*I},
{0.3333 + 0.0000*I, -0.1667 + 0.2887*I, -0.1667 - 0.2887*I}};
float complex a = -0.5+0.866*I;
//a^2 =-0.5-0.866i
printf("%f +i %f \n %f %f", creal(a), cimag(a),
cabs(a), (180/M_PI) * carg(a));
printf("\n line voltage fabcr \n\n");
for(i=0; i<3; i++)
{
for(j=0; j<1; j++)
{
printf( "%f +i %f ",creal(fabcr[i][j]),cimag(fabcr[i][j]));
}
printf("\n");
}
printf("\n A matrix \n\n");
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
printf( "%f +i %f ",creal(A[i][j]),cimag(A[i][j]));
}
printf("\n");
}
printf("\ninvA matrix\n\n");
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
printf( "%f +i %f ",creal(invA[i][j]),cimag(invA[i][j]));
}
printf("\n \n");
}
// matrix multiplication
for(p=0; p<3; p++)
{
sum=0;
for(q=0; q<3; q++)
{
l=fabcr[q][0]*A[p][q];
sum=sum+l;
}
printf("%f i %f \n", creal(sum),cimag(sum));
// fa012[p][1]=(1/3)*creal(sum)+i*(1/3)*cimag(sum);
fa012[p][0]=sum*0.33;
}
printf("\n symmetrical component fa012\n \n");
for(p=0; p<3; p++)
{
printf("%f i %f \n", creal(fa012[p][0]),cimag(fa012[p][0]));
}
D[0][0] = -fa012[0][0];
D[1][0] = 0;
D[2][0] = -fa012[2][0];
printf("\n matrix D \n \n");
for(p=0; p<3; p++)
{
printf("%f i %f \n", creal(D[p][0]),cimag(D[p][0]));
}
printf("\n\nzero and negative sequence component in line voltage terms\n\n");
for(p=0; p<3; p++)
{
sum=0;
for(q=0; q<3; q++)
{
l=D[q][0]*invA[p][q];
sum=sum+l;
}
printf("%f i %f \n", creal(sum),cimag(sum));
//fa012[p][1]=(1/3)*creal(sum)+i*(1/3)*cimag(sum);
B[p][0]=sum*3;
}
printf("\n\nfinal balanced voltage \n\n") ;
for(p=0; p<3; p++)
{
Vbal[p][0] = fabcr[p][0]+B[p][0];
printf("\n %f i %f \n %f %f \n",
creal(Vbal[p][0]), cimag(Vbal[p][0]),
cabs(Vbal[p][0]), (180/M_PI) * carg(Vbal[p][0]));
}
printf("\n\n conversion to polar cordinates \n\n") ;
for(i=0; i<3; i++)
{
Vpol[i][0] = cabs(Vbal[i][0]);
Vpol[i][1] = (180/M_PI) * carg(Vbal[i][1]);
printf("%f %f \n", Vpol[i][0], Vpol[i][1]);
}
}

Resources