Thread: Passing arrays to functions

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    9

    Passing arrays to functions

    Hello guys,

    I'm looking for some advice on how to pass arrays to functions, rather than having the arrays declared in the function.

    Right now my program works fine, but I'm using one mass of code, instead of separating tasks into functions as my lab specifies.

    I have to calculate the linear regression of a set of experimental values to find other values that the user will input. In short, I have to have a function that computes m, and another one that computes b in the equation y = mx + b.

    Here is my code for the program as one block:
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #define SIZE 6
    
    
    void main()
    {
    
    
    int x_time[SIZE]={0, 1, 2, 3, 4, 5};                /* Array for time values in minutes*/
    int y_temp[SIZE]={20, 36, 61, 68, 77, 110};         /* Array for temperature values in C*/
    int  i, choice, N=6;
    float user_input, user_temp, m, b;
    float sum_x, sum_y, sum_xy, sum_x2;
    float temp_at15, temp_at43;
    
    
    printf("These are the time values measures in minutes on the left, and the temperature values measured in celcius on the right:\n");
    
    
    // below is printing the initial values in a table format
    
    
    for (i=0;i<6;i++)
        {
            printf("\n%d\t%d\n", x_time[i],y_temp[i]);
        }
    
    
    // below are the calculations for the formula for linear regression
    
    
    sum_x= x_time[0]+x_time[1]+x_time[2]+x_time[3]+x_time[4]+x_time[5];
    
    
    
    
    sum_y= y_temp[0]+y_temp[1]+y_temp[2]+y_temp[3]+y_temp[4]+y_temp[5];
    
    
    
    
    sum_xy= (x_time[0]*y_temp[0])+(x_time[1]*y_temp[1])+(x_time[2]*y_temp[2])+(x_time[3]*y_temp[3])+(x_time[4]*y_temp[4])+(x_time[5]*y_temp[5]);
    
    
    
    
    sum_x2= (x_time[0]*x_time[0])+(x_time[1]*x_time[1])+(x_time[2]*x_time[2])+(x_time[3]*x_time[3])+(x_time[4]*x_time[4])+(x_time[5]*x_time[5]);
    
    
    
    
    m = ((N*sum_xy)-(sum_x*sum_y))/(N*(sum_x2)-(sum_x*sum_x));/*calculate the slope for the equation y=mx+b*/
    
    
    
    
    b = ((sum_y)-(m)*(sum_x))/N; /*calculate b in the equation y=mx+b*/
    
    
    
    
    temp_at15=(m*1.5)+b;        /* Calculating the temperature at 1.5 minutes and 4.3 minutes*/
    temp_at43=(m*4.3)+b;
    printf("\nThe temperatures at 1.5 minutes and 4.3 minutes are %f and %f\n\n", temp_at15, temp_at43);
    
    
    while (user_input!=-1)          /*Loop that enables the user to continually enter time values to see the corresponding temperature values*/
    {
        printf("\nEnter a time value to see its corresponding temperature, or press -1 to quit:");
    
    
        scanf("%f", &user_input);
    
    
        if (user_input!=-1)
            {
                user_temp=(m*user_input)+b;
                printf("\n the temp is %f", user_temp);
            }
    
    
    }
    
    
    
    
    }

    Now I created another program to test out seperating the calculations into separate functions, and I'm running into problems of how to declare the function before main.
    Then when calling it, I do not know how to enter the parameters, or pass the arrays into them.

    Here is my failed attempt:
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    
    
    float calculate_m(void) /*declare the function as a float*/
    
    
    int main(void)
    
    
    {
    
    
    int x_time[6]={0, 1, 2, 3, 4, 5};
    int y_temp[6]={20, 36, 61, 68, 77, 110};
    int  i, choice, N=6;
    float user_input, user_temp, m, b;
    float sum_x, sum_y, sum_xy, sum_x2;
    float temp_at15, temp_at43;
    
    
    calculate_m(x_time, int 6, y_temp, int 6, float m, sum_x, sum_x2, sum_y, sum_xy, int N=6); /* Calling the function*/
    
    
    printf("this is m \t %f", m); /* print the returned value of m */
    
    
    //below is the function calculating m
    
    
        float calculate_m(x_time[], int 6, y_temp[], int 6, float m, sum_x, sum_x2, sum_y, sum_xy, int N=6);
        {
    
    
            sum_x= x_time[0]+x_time[1]+x_time[2]+x_time[3]+x_time[4]+x_time[5];
    
    
    
    
            sum_y= y_temp[0]+y_temp[1]+y_temp[2]+y_temp[3]+y_temp[4]+y_temp[5];
    
    
    
    
            sum_xy= (x_time[0]*y_temp[0])+(x_time[1]*y_temp[1])+(x_time[2]*y_temp[2])+(x_time[3]*y_temp[3])+(x_time[4]*y_temp[4])+(x_time[5]*y_temp[5]);
    
    
    
    
            sum_x2= (x_time[0]*x_time[0])+(x_time[1]*x_time[1])+(x_time[2]*x_time[2])+(x_time[3]*x_time[3])+(x_time[4]*x_time[4])+(x_time[5]*x_time[5]);
    
    
    
    
            m = ((N*sum_xy)-(sum_x*sum_y))/(N*(sum_x2)-(sum_x*sum_x));/*calculate the slope for the equation y=mx+b*/
            return m;
        };
    
    
    }
    If anyone could give me some advice, I'd really appreciate it - and meanwhile I'll keep plugging away.

    Thank you,

    -cda67


    (I am aware that for some of the calculations I could have used loops)

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First your function prototype, function implementation, and your function call must all agree on the number and type of parameters, yours do not agree. Second you are missing a semicolon after your function prototype. Also you can not implement your function inside another function.

    Jim

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    Thank you, Jim.

    Your suggestions led me to victory!

    -cda67

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You might also want to investigate the use of loops for your calculations... you could probably simplify them significantly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arrays between functions
    By jtay in forum C Programming
    Replies: 6
    Last Post: 04-15-2010, 03:54 PM
  2. passing arrays to functions
    By rakeshkool27 in forum C Programming
    Replies: 4
    Last Post: 01-17-2010, 01:41 PM
  3. Passing 2D arrays between functions
    By taurus in forum C Programming
    Replies: 10
    Last Post: 09-28-2009, 05:05 AM
  4. Passing 2d arrays to functions
    By owi_just in forum C Programming
    Replies: 2
    Last Post: 05-06-2005, 05:22 PM
  5. passing arrays through functions
    By Ecko in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2003, 08:21 PM

Tags for this Thread