Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Posts
    3

    Unanswered: Help with c programming please

    Hello all this is my first post.

    I'm writing a program in 'c' using lcc win32.

    The program is to perform eulers calculation and store answers in arrays.
    The program is to use pointers, arrays and functions.

    This is the start of my program any help or tips are appreciated.



    #include <stdio.h>
    #include <math.h>

    void calculation (float Y[], float X[], int i, float step_interval)

    {

    Y[0] = 0;
    Y[0] = 1;

    for (i=0; i<=step_interval; i++)

    {
    Y[i+1] = Y[i] + (step_interval) * (X[i] * (exp, (-X[i])));
    }

    for (i = 0; i <= step_interval; i++)

    {
    printf ("%f, %f", Y[i], X[i]);

    }

    }
    void main()

    {

    float X, Y, step_size, time_interval;
    int num, exp, sum, i;

    printf("\n Please enter the intial conditions of 'x': ");
    scanf("%f", &X);

    printf("\n Please enter the intial conditions of 'y': ");
    scanf("%f", &Y);


    printf("\n Please enter the step size (h): ");
    scanf("%f", &step_size);

    printf("\n Please enter the time interval: ");
    scanf("%f", &time_interval);

    Y[i+1] = calculation(X, Y, step_size, time_interval);


    printf("\n The value of sum = %f\n\n",calculation);

    }
    Thank You
    Last edited by barmyarmy; 03-21-04 at 05:48.

  2. #2
    Join Date
    Mar 2004
    Location
    Roseville, Calfornia
    Posts
    2

    Re: Help with c programming please

    Well, the first problem I see is that you've declared X, & Y as floating point numbers but you treat them as arrays. If you manage to convince your compiler that this code is ok, then very bad things will happen when you run this program.

    replace your declaration of X & Y with

    float X[1024];
    float Y[1024];

    C++ treats arrays as pointers so you would lose the "&" in the scanf instructions (though some people would prefer separate intial variables or using &(X[0])) instead for readability reasons).

    You also declare "i" and use it as an array index but never initialize or assign it. Very bad.

    Good luck with your project.

    void main()

    {

    float X, Y, step_size, time_interval;
    int num, exp, sum, i;

    printf("\n Please enter the intial conditions of 'x': ");
    scanf("%f", &X);

    printf("\n Please enter the intial conditions of 'y': ");
    scanf("%f", &Y);


    printf("\n Please enter the step size (h): ");
    scanf("%f", &step_size);

    printf("\n Please enter the time interval: ");
    scanf("%f", &time_interval);

    Y[i+1] = calculation(X, Y, step_size, time_interval);


    printf("\n The value of sum = %f\n\n",calculation);

    }
    Thank You [/SIZE][/QUOTE]

  3. #3
    Join Date
    Mar 2004
    Posts
    3
    Cheers for reply, go program working. Here is wrking program.

    #include <stdio.h>
    #include <math.h>

    #define MAX_NO_POINTS 100

    double h, x[MAX_NO_POINTS], y[MAX_NO_POINTS]; /////*declaring variables*//////
    int No_Steps;


    void EULER() /////*function where euler's calculation is performed*//////
    {
    int i;
    double Function;

    i =1;
    while (i <= No_Steps)
    {
    Function = (x[i-1] * exp(-x[i-1]));
    y[i] = y[i-1] + (h) * Function;
    x [i] = x[i-1] + h;
    i++;
    }

    }

    void display() ///////* function for displaying results*///////
    {
    int i=1;
    while (i <= No_Steps)
    {
    printf("\n Yi at step %d is %.4f",(i-1), y[i]);
    i++;
    }
    }


    void main() ///////*main part of the program*/////////

    {
    double Init_x, Init_y, a, b ;
    char user;

    int i;

    do
    {

    printf("\n -----------------------------------------------------------------");
    printf("\n | This will calculate eulers calculation different variables |");
    printf("\n | for the particular solution (X exp -X) |");
    printf("\n -----------------------------------------------------------------\n\n");

    do
    {
    printf("\n Please enter the intial conditions x0 >: "); ///////*prompt user to enter variables*///////
    fflush(stdin);
    }
    while (scanf("%lf", &x[0]) == 0); ///////* question is asked again if user enters an alpha character*///////

    do
    {
    printf("\n Please enter the intial conditions of y0>: ");
    fflush(stdin);
    }
    while (scanf("%lf", &y[0]) == 0);

    do
    {
    printf("\n Please enter step size (h)>: ");
    fflush(stdin);
    }
    while (scanf("%lf", &h) == 0);

    do
    {
    printf("\n Please enter the number of steps >: ");
    fflush(stdin);
    }
    while (scanf("%d", &No_Steps) == 0);


    EULER(); //////*calling euler function*///////
    display(); ///////*caling display function*///////
    fflush(stdin);
    printf("\n\n Would you like another go (y/n)"); ///////*prompting user if they would like another go*////////
    scanf("%c", &user);
    }
    while((user == 'y') || (user == 'Y')); ////////*loop for 'yes, they would like another go' for lower/uppercase*////////
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •