I am a beginner in C and I am working on a code here ....
the problem statement is
Suppose there are N point charges at rest in a region of three-dimensional space. The k-th point charge has a charge value Qk and is located at the coordinate Pk = (xk, yk, zk). The index k ranges from 1 to N.
Write a program that computes the electric field intensity E at an arbitrary position P = (x, y, z). The program should read the charge data from a file.
For a detailed look at the math used to determine E, review this page: Electric Field Equations
For information on the file format, check this page: Charge Data File Format
Program Requirements
Quote
Prompt the user to enter the name of a file to open and store the response in a string (i.e., an array of type char).
Write a function to read the charge data file.
If the file cannot be opened for reading, then the program should display an error message and then quit.
The charge data should be stored in an array of structures with the following properties:
The maximum size of the array is 100.
The structure definition should have member variables for holding the charge value and the coordinates of the point charge.
After reading in the charge data, the program should neatly display the values to the terminal screen.
Write a separate function that asks the user to enter the x, y, and z coordinates of the position in space at which to compute the electric field.
Write a function that computes and displays the value of the electric field at the user's desired position.
Your program must be ANSI/ISO C90 compliant.
Write a function to read the charge data file.
If the file cannot be opened for reading, then the program should display an error message and then quit.
The charge data should be stored in an array of structures with the following properties:
The maximum size of the array is 100.
The structure definition should have member variables for holding the charge value and the coordinates of the point charge.
After reading in the charge data, the program should neatly display the values to the terminal screen.
Write a separate function that asks the user to enter the x, y, and z coordinates of the position in space at which to compute the electric field.
Write a function that computes and displays the value of the electric field at the user's desired position.
Your program must be ANSI/ISO C90 compliant.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct electric {
double chargeval;
double x;
double y;
double z;
}field;
struct electric *s = &field;
double function_square1(double x, int p) /*square of double type */
{
double k;
int n;
k=x;
for(n=1; n<p; n++)
{
x=x*k;
}
return x;
}
double asking(double *q, double *w, double *e)
{
double a,b,c;
printf("Enter x coordinate of test position: ");
scanf("%f", &a);
printf("Enter y coordinate of test position: ");
scanf("%f", &b);
printf("Enter z coordinate of test position: ");
scanf("%f", &c);
*q = a;
*w = b;
*e = c;
return 0;
}
double calc(double x, double y, double z, float (*a)[4],int e)
{
int m;
float v,c,ex,ey,ez;
float sx=0,sy=0,sz=0;
for(m=0; m<e; m++)
{
v=((x-a[m][1])*(x-a[m][1])+(y-a[m][2])*(y-a[m][2])+(z-a[m][3])*(z-a[m][3]));
c=function_square1(v, 3);
ex=a[m][0]*(x-a[m][1])/sqrt(c);
sx=sx+ex;
}
for(m=0; m<e; m++)
{
v=((x-a[m][1])*(x-a[m][1])+(y-a[m][2])*(y-a[m][2])+(z-a[m][3])*(z-a[m][3]));
c=function_square1(v, 3);
ey=a[m][0]*(y-a[m][2])/sqrt(c);
sy=sy+ey;
}
for(m=0; m<e; m++)
{
v=((x-a[m][1])*(x-a[m][1])+(y-a[m][2])*(y-a[m][2])+(z-a[m][3])*(z-a[m][3]));
c=function_square1(v, 3);
ez=a[m][0]*(z-a[m][3])/sqrt(c);
sz=sz+ez;
}
printf("The electric field at the test position is:\n");
printf("(Ex, Ey, Ez) = (%f, %f, %f)", sx/4, sy/4, sz/4);
}
int main ()
{
char fname[400];
char arr[100];
int i=0, k=0, t=0, m=0, b=0, q=0 ,p, e;
float x, y, z;
float a[25][4];
FILE *fp;
printf("Enter file name: \n");
scanf("%s", fname);
fp = fopen(fname, "r");
if(fp == NULL)
{
printf("Write Error!!\n");
return 0;
}
while (fscanf(fp,"%s", arr) != EOF)
{
a[i][k]=atof(arr);
k=k+1;
q=q+1;
if(k==4)
{
i=i+1;
k=0;
}
}
e=q/4;
printf("List of point charges in space: \n\n");
for(p=1; p<=e; p++)
{
printf("[%d] Q= [%11.6f] P=( %6.3f, %6.3f, %6.3f) \n", p, a[p-1][0], a[p-1][1], a[p-1][2], a[p-1][3]);
}
printf("\n");
asking(&x, &y, &z);
/* printf("x= %f ,y= %f ,z= %f\n", x,y,z); */
/* printf("e= %d\n", e); */
calc(x,y,z,a,e);
fclose(fp);
return 0;
}
Thanks !!!!!
Edited by Alexander, 01 June 2011 - 03:03 PM.
([code]/formatting where needed)


Sign In
Create Account

Back to top









