Im codding a programm that has multiple functions.

Once functions calculates the means of 2 sets of data and prints meanx and meany.

Now another function will use those means to do some other calculation.

I dont know how to get those values from one function and pass them into another?
Can anyone help me here??

Thank you

Recommended Answers

All 3 Replies

ok, here is example code

void print(int x, int y)
{
    cout << x << " " << y << "\n";
}

void calculate( int* meanx, int* meany)
// The two parameters are pointers to the integers that
// are declared in main()
{
    *meanx = 1;
    *meany = 2;
}

int main()
{
    int meanx = 0; 
    int meany = 0;
    calculate(&meanx, &meany);
    print(meanx, meany);
}

ok, here is example code

void print(int x, int y)
{
    cout << x << " " << y << "\n";
}

void calculate( int* meanx, int* meany)
// The two parameters are pointers to the integers that
// are declared in main()
{
    *meanx = 1;
    *meany = 2;
}

int main()
{
    int meanx = 0; 
    int meany = 0;
    calculate(&meanx, &meany);
    print(meanx, meany);
}

Can u show me in this code, I still didnt quet grasp it.

#include <stdio.h>
#include <string.h>
#include<stdlib.h>

typedef struct
{
unsigned int larray;
double *x;
double *y;
}data;

double getArrayBar( double *array , int length ) ;

data getdata(const char file[]);
double corr(data data_sm);



int main(void)
{
data data_sm;

char filename[20];

printf("\nEnter the file name  : ");
scanf("%s",&filename);

data_sm = getdata(filename);


corr(data_sm);

return 0;
}

double getArrayBar( double *array , int length )
{
	double sum = 0 ;
	for ( int i = 0 ; i < length ; i++ )
		sum += array[i] ;
		
	return sum/length ;
}

data getdata(const char file[])
{
	data d;
	int i=0,j=0;
	FILE *inputFile;
	double meanx, meany;
	/*double a[200],b[200];*/
	d.x = 0;
	d.y = 0;
	inputFile = fopen(file,"r");
		
			if(inputFile == NULL)
			{	
			printf ("Error opening data file \n");
			exit(1);	
			}

			do
			{
			
			d.x = realloc(d.x, (i+1)*sizeof(double));
			d.y = realloc(d.y, (i+1)*sizeof(double));
			i++;
			
			if(!d.y || !d.x) {
    			printf("Allocation Error\n");
    			exit(1);}
			}
			while(fscanf(inputFile,"%lf %lf",&d.x[i-1],&d.y[i-1]) == 2);
			
	meanx = getArrayBar (d.x , i -1 );
	meany = getArrayBar (d.y , i -1 );
	
	printf("X bar is %lf\n", meanx) ;
	printf("Y bar is %lf\n", meany) ;
	   
   	fclose(inputFile);	  
	free (d.x);
	free (d.y);
	return d;	 
}
/* Calculates the linear correlation coeffcient */
double corr(data data_sm)
{




}

As you can see one function reads a file and calculates the means of x and y, now in function double corr i want to be able to use meanx and meany.

double corr(data data_sm, double meanx, double meany);

data getdata(const char file[], double* meanx, double* meany)
{
<snip>
	*meanx = getArrayBar (d.x , i -1 );
	*meany = getArrayBar (d.y , i -1 );

}

int main()
{
    double meanx = 0, meany = 0;
   <snip>
   data_sm = getdata(filename, &meanx, &meany);
    corr(data_sm, meanx, meany);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.