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:If anyone could give me some advice, I'd really appreciate it - and meanwhile I'll keep plugging away.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; }; }

Thank you,

-cda67

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