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]);
}
}
Related
When I change the type of array from float to double (float array[][] to double array[][]), it doesn't scan the values correctly. All the values become zero. For example, if I enter 5 for input when it's float, it's 5.000000. However, when it's double, every value I enter is scanned as 0.0000000.
#include <stdio.h>
#include <math.h>
int main() {
int limestone, min=0;
//for entering height of array (number of limetstone)
printf("Enter number of limestone: ");
scanf("%d", &limestone);
float array[limestone][3];
//double array[limestone][3]; (the problem)
//for getting inputs
for(int i=0;i<limestone;i++)
{
printf("Enter the %d porosity, hydraulic conductivity (m/s), specific gravity: ", i+1);
scanf("%f %f %f", &array[i][0], &array[i][1], &array[i][2] );
}
//for print array (you can remove it)
for(int i=0;i<limestone;i++)
{
for(int j=0;j<3;j++)
{
printf("%f ",array[i][j]);
}
printf("\n");
}
//Comparing 3rd (Last) Column
for(int i=limestone-1;i>=0;i--)
{
if(array[i][2]<array[min][2])
{
min=i;
}
}
printf("The limestone with the lowest specific gravity is Limestone %d with a specific gravity of %f",min+1,array[min][2]);
return 0;
}
I got your code to work by changing your scanf line to
scanf("%lf %lf %lf", &array[i][0], &array[i][1], &array[i][2] );
All I did was change the %f's to %lfs.
This works for me at least, I think it's doing what you want
i have this data in file
ATOM 1 N MET A 1 25.909 16.164 -8.037
ATOM 2 CA MET A 1 25.498 14.786 -8.206
ATOM 3 C MET A 1 26.612 13.934 -8.806
ATOM 4 O MET A 1 27.442 14.402 -9.588
this is my full code
#include<stdio.h>
#include<math.h>
typedef struct prot {
char atom[10];
int atomno;
char site[10];
char residue[10];
char chain;
int residueno;
float x, y, z;
} Td;
int main()
{
FILE*fd;
fd=fopen("2zoi.pdb","r+");
Td data[1300];
char buffer[1300];
int a=0;
while(fgets(buffer, 1300, fd))
{
sscanf(buffer, "%s %d %s %s %c %d %f %f %f",data[a].atom,&data[a].atomno,data[a].site,
data[a].residue,&data[a].chain,&data[a].residueno,&data[a].x, &data[a].y, &data[a].z);
printf("%s %d %s %s %c %d %6.3f %6.3f %6.3f \n",data[a].atom,data[a].atomno,data[a].site,
data[a].residue,data[a].chain,data[a].residueno,data[a].x, data[a].y, data[a].z);
a++;
}
//---user input
int fr,sr;
int fa,sa;
int i=0;
float x1,x2,y1,y2,z1,z2;
float distance=0;
printf("Enter first no of atom :");
scanf("%d",&fa);
printf("Enter second no of atom :");
scanf("%d",&sa);
while(data[i].atomno>=0)
{
if(fa==data[i].atomno)
{
x1=data[i].x;
y1=data[i].y;
z1=data[i].z;
}
else if(sa==data[i].atomno)
{
x2=data[i].x;
y2=data[i].y;
z2=data[i].z;
}
i++;
}
printf("%f %f %f",x1,y1,z1);
printf("%f %f %f",x2,y2,z2);
//distance= sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2) + pow((z2 - z1), 2));
//printf("%6.3f",distance);
return 0;
}
the problem is at this part
printf("%f %f %f",x1,y1,z1);
printf("%f %f %f",x2,y2,z2);
i try to return to values from above loop where the value for x1,y1,z1 is for the first atom and x2,y2,z2 for second atom.
when i input 3 and 4, it gives answer
26.612 13.934 -8.806
27.442 14.402 -9.588
which is correct. but when i input 1 and 2, it gives rubbish answer. it seems like i cant input number 2 also number 10.did i do any mistake?
You have a big problem with data array values: not all elements of the array are set to 0 then the final loop can be broken.
You can use memset to reset to 0 all bytes of data array.
#include<stdio.h>
#include<math.h>
#include <string.h>
typedef struct prot
{
char atom[10];
int atomno;
char site[10];
char residue[10];
char chain;
int residueno;
float x, y, z;
} Td;
int main()
{
FILE*fd;
Td data[1300];
char buffer[1300];
size_t a=0;
fd=fopen("2zoi.pdb","r+");
if (fd != NULL)
{
memset(data, 0x00, sizeof(data));
while ((fgets(buffer, 1300, fd)) && (a < sizeof(data)/sizeof(data[0])))
{
sscanf(buffer, "%s %d %s %s %c %d %f %f %f",data[a].atom,&data[a].atomno,data[a].site,
data[a].residue,&data[a].chain,&data[a].residueno,&data[a].x, &data[a].y, &data[a].z);
printf("%s %d %s %s %c %d %6.3f %6.3f %6.3f \n",data[a].atom,data[a].atomno,data[a].site,
data[a].residue,data[a].chain,data[a].residueno,data[a].x, data[a].y, data[a].z);
a++;
}
//---user input
int fr,sr;
int fa,sa;
size_t i=0;
float x1,x2,y1,y2,z1,z2;
float distance=0;
printf("Enter first no of atom :");
scanf("%d",&fa);
printf("Enter second no of atom :");
scanf("%d",&sa);
while ((data[i].atomno>=0) && (i < sizeof(data)/sizeof(data[0])))
{
if(fa==data[i].atomno)
{
x1=data[i].x;
y1=data[i].y;
z1=data[i].z;
}
else if(sa==data[i].atomno)
{
x2=data[i].x;
y2=data[i].y;
z2=data[i].z;
}
i++;
}
printf("%f %f %f\n",x1,y1,z1);
printf("%f %f %f\n",x2,y2,z2);
}
else
{
fprintf(stderr, "Error opening file\n");
exit(1);
}
return 0;
}
You should check the fopen return value (as you can see in code posted), but as a standard rule: you should always check functions return values.
So I wrote this code to find the relative error between an estimate of PI and a fixed theoretical value (3.14159). But whenever I run the program, I get this in the command line: "After 5 terms pi is approximately -1.#INF00", and the same for the relative error value. I'm not sure what's wrong
int main(void)
{
float i, n, error;
float term = 0;
printf("Enter the number of terms: ");
scanf("%f", &n);
for(i=0; i<=n; i++)
{
term=term + pow(-1.0, i+1)/(i*i);
}
error=fabs(term-3.141593)/(3.141593);
printf("The actual value of pi is %f\n", 3.141593);
printf("After %0.0f terms pi is apporximately %f\n", n, term);
printf("The relative error is %f %", error);
return 0;
}
I ve written this program to calculate the solutions of 3X3 system of linear equations using the cramer rule. The program works and outputs correct results but before it ends normally it is prompted an error message "Run time check failure #2-stack around the variable x2 was corrupted" and the program does not terminate correctly. Here is the code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void cramer (double** a, double* b);
int main()
{
int i,j;
double** a;
double* b;
a=(double**)malloc(3*sizeof(double*));
for (i=0;i<3;i++)
a[i]=(double*)malloc(3*sizeof(double));
b=(double*)malloc(3*sizeof(double));
printf ("This program computes the solutions of a system of linear equations 3X3\n");
printf ("a11x1+a12x2a13x3=b1\n");
printf ("a21x2+a22x2+a23x3=b2\n");
printf ("a31x3+a32x2+a33x3=b3\n");
printf ("Insert a and b\n");
for (i=0;i<3;i++){
for (j=0;j<3;j++){
printf("a%d%d\n",i+1,j+1);
scanf("%lf",&a[i][j]);
}
printf("b%d\n",i+1);
scanf("%lf",&b[i]);
}
cramer(a,b);
for (i=0;i<3;i++)
free(a[i]);
free(a);
free(b);
return 0;
}
void cramer(double** a,double* b)
{
int i,j;
double x1[3][3],x2[3][3],x3[3][3],A[3][3];
for (i=0;i<3;i++){
for (j=0;j<3;j++){
A[i][j]=a[j][i];
x1[i][j]=a[i][j]*A[i][j];
x2[i][j]=a[i][j]*A[i][j];
x3[i][j]=a[i][j]*A[i][j];
}
x1[i][1]=b[i];
x2[i][2]=b[i];
x3[i][3]=b[i];
}
printf(" %f %f %f\n",x1[0][0],x1[0][1],x1[0][2]);
printf("x1=%f %f %f\n",x1[1][0],x1[1][1],x1[1][2]);
printf(" %f %f %f\n",x1[2][0],x1[2][1],x1[2][2]);
printf("\n");
printf(" %f %f %f\n",x2[0][0],x2[0][1],x2[0][2]);
printf("x2=%f %f %f\n",x2[1][0],x2[1][1],x2[1][2]);
printf(" %f %f %f\n",x2[2][0],x2[2][1],x2[2][2]);
printf("\n");
printf(" %f %f %f\n",x3[0][0],x3[0][1],x3[0][2]);
printf("x3=%f %f %f\n",x3[1][0],x3[1][1],x3[1][2]);
printf(" %f %f %f\n",x3[2][0],x3[2][1],x3[2][2]);
}
x1[i][1]=b[i];
x2[i][2]=b[i];
x3[i][3]=b[i]; // index is out of range
Probably this is what you mean:
x1[i][0]=b[i];
x2[i][1]=b[i];
x3[i][2]=b[i];
The line
x3[i][3]=b[i];
is accessing memory beyond the bounds of the array you defined. C has zero based indexing, so for your 3x3 arrays the maximum valid index is 2.
Probably should have
x1[i][0]=b[i];
x2[i][1]=b[i];
x3[i][2]=b[i];
I am trying to do what's been done here Read co-ordinates from a txt files using C Program . The data that I am trying to input is in this format:
f 10 20 21
f 8 15 11
. . . .
f 11 12 25
The only difference in my point structure is that I have a an extra char to store the letter in the first column (which may or may not be the letter f). I guess im either declaring my char wrong, or I'm calling it in printf incorrectly. Either way, I only get the first line read and then my program terminates. Any ideas ?
Here is my MWE below
#define FILEPATHtri "/pathto/grid1DT.txt"
#define FILEPATHorg "/pathto/grid1.txt"
#define MAX 4000
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
typedef struct
{
float x;
float y;
float z;
char t[1];
}Point;
int main(void) {
Point *points = malloc( MAX * sizeof (Point) ) ;
FILE *fp ;
fp = fopen( FILEPATHtri,"r");
int i = 0;
while(fscanf(fp, "%s %f %f %f ", points[i].t, &points[i].x, &points[i].y, &points[i].z ) == 4 )
{
i++;
}
fclose(fp);
int n;
for (n=0; n<=i; n++){
printf("%c %2.5f %2.5f %2.5f \n", points[i].t, points[n].x, points[n].y, points[n].z ); }
printf("There are i = %i points in the file \n And I have read n = %i points ",i,n);
return 0;
}
Since there's only 1 char in there, not a string just use a single char in your code:
char t;
}Point;
Then when you read it in:
while(fscanf(fp, "%c %f %f %f ", &points[i].t, &points[i].x, &points[i].y, &points[i].z ) == 4 )
{
I'll note that having an array of 1 char, at the end of a structure, sets you up for the struct hack which might not have been your intentions... A good reason to use just char t instead of char t[1]
Also this line:
for (n=0; n<=i; n++){
Should be
for (n=0; n<i; n++){
One last note... if you wanted to print the character out that you read in the prints at the bottom, you should be using n:
// note your previous code was points[i].t
printf("%c %f %f %f \n", points[n].t, points[n].x, points[n].y, points[n].z ); }
Check this
while(fscanf(fp, "%c %f %f %f ", points[i].t, &points[i].x, &points[i].y, &points[i].z ) == 4 )
{
i++;
}
fclose(fp);
int n;
for (n=0; n<i; n++){
printf("%c %2.5f %2.5f %2.5f \n", points[n].t, points[n].x, points[n].y, points[n].z ); }
printf("There are i = %i points in the file \n And I have read n = %i points ",i,n);
getch();
return 0;
}
modification are since only a single character is read %s modified to %c also in printf its not points[i].t its points[n].t . Also the limit checking in for loop is also corrected to n<i