Jump to content

how to use struct referencing

- - - - -

  • Please log in to reply
2 replies to this topic

#1
codehunter22

codehunter22

    Newbie

  • Members
  • Pip
  • 2 posts
Hello folks,

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.
I wrote the code and I am stuck with the concept of Structures... like I need help in using struct variables and referencing them in main and the functions. please help me out.. here is the code
#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)


#2
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
What's the problem?

If you meant your code is not compiling. Here are two fixes i did

1. changed to return type of function calc to void instead of double since the function is not returning any thing

2. Changed the type of x,y and z in main from float to double

i.e.

    float x, y, z; // this was changed to


    double x,y,z;



Code compiles and successfully starts executing after these changes are made asking for a file name. I tried it on Visual studio 2010.

If there is any thing else you need, please ask specific question i.e. what you want to do and what you are currently getting. It is hard to read through so much code without knowing what to solve.

#3
gdjs

gdjs

    Newbie

  • Members
  • PipPip
  • 10 posts
Well, I'll comment on structs:


struct something

{

    int a;

    float b;

};


The code above declares a struct whose tag is "something", containing two members: one of type int, called 'a', and one of type float, called 'b'. To declare an object whose type is the struct we created, use


struct something foo;


'foo' identifies an object containing 'a' and 'b', which can be accessed via the dot operator:


foo.a = 10;

foo.b = 10.0;


If you wish to work with pointers to 'struct something's, you can use the arrow operator to improve readability:


struct something *p = malloc (sizeof *p);

// some error handling...


// access *p's members:

p->a = 10;    // equivalent to (*p).a

p->b = 10.0; // equivalent to (*p).b



You're probably confused because most examples typedef away the struct signatures:


typedef struct something

{

    int a;

    float b;

} sometype;


After the code above, 'sometype' is equivalent to 'struct something':


sometype foo;

sometype *p;


// use as before


The code above is equivalent to:


struct something

{

    int a;

    float b;

};


typedef struct something sometype;


Read carefully and it should become crystal-clear. If not, well... I recommend doing what you should've already done: get a good book ;-)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users