Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

OUTLINE OF THE SOURCE CODE: #include <stdio.h> #include <stdlib.h> #define SIZE

ID: 3710925 • Letter: O

Question

OUTLINE OF THE SOURCE CODE:

#include <stdio.h>

#include <stdlib.h>

#define SIZE 200

float sum(int size,float array1[]);

float sum_prod(int size, float array1[], float array2[]);

float sum_squared(int size, float array1[]);

int main (void)

{

     //Suggested variable declarations, use more or others if you want

     FILE *fp;

     int i, FileLength;

     float x[SIZE],y[SIZE];

    

     //Describe code to user

    

    //Code for part 1 goes here, open files and read in data

  

   

     printf("The summation of x is:");

     //call function sum sending x data and print result

    

     printf("The summation of y is:");

     //call function sum sending y data and print result

    

     printf("The summation of xy is:");

     //call function sum_prod and print result

    

     printf("The summation of x squared is:");

     //call function sum_squared sending x data and print result

    

     printf("The summation of y squared is:");

     //call function sum_squared sending y data and print result

     //Extra Credit goes here

    

    

     return 0;

}

float sum(int size,float array1[])

//Code for part 2 goes here

float sum_prod(int size, float array1[], float array2[])

//Code for part 3 goes here

float sum_squared(int size, float array1[])

//Code for part 4 goes here

Problem: Write a C program to find the following given two data sets, x and y: Extra credit: Find the correlation coefficient, r, between two data sets, x and y, using the equation below: OUTPUT TABLE Sum of squares Sum of products EXTRA CREDIT: r Filename Sum datax.txt datay.txt

Explanation / Answer

PROGRAM

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#define SIZE 200 // define constant SIZE=200

// Declare prototype of functions

float sum(int size,float array1[]);

float sum_prod(int size, float array1[], float array2[]);

float sum_squared(int size, float array1[]);

int main (void)

{

FILE *fp1,*fp2; // Create File Pointers fp1,fp2

// Declare variables

int i=0, FileLength=1;

float r,r1,r2,r3,r4;

float sumx,sumy,ssqx,ssqy,sprod;

char c,c1;

float x[SIZE],y[SIZE]; // Declare array of float elements of SIZE

//open files in read mode

fp1=fopen("f:\datax.txt","r");

fp2=fopen("f:\datay.txt","r");

  

while((c=fgetc(fp1))!=EOF) // create while loop until end of file fp1 for counting length of file

if(c==' ') FileLength++; // check condition every new line and count legnth of the file

printf(" datax.txt File Length: %d",FileLength); // display length of datax.txt file

  

FileLength=1; // again intialize FileLength=1

while((c=fgetc(fp2))!=EOF) // create while loop until end of file fp2 for counting length of file

if(c==' ') FileLength++; // check condition every new line and count legnth of the file

printf(" datay.txt File Length: %d",FileLength); // display length of datay.txt file

// close two files

fclose(fp1);

fclose(fp2);

// again open files in read mode

fp1=fopen("f:\datax.txt","r");

fp2=fopen("f:\datay.txt","r");

printf(" ");

  

for(i=0;i<SIZE;i++) // create for loop until SIZE

fscanf(fp1,"%f",&x[i]); // read every elements in datax.txt file and stored into x[] array

  

for(i=0;i<SIZE;i++) // create for loop until SIZE

fscanf(fp2,"%f",&y[i]); // read every elemets in datay.txt file and stored into y[] array

printf(" The summation of x is:");

sumx=sum(SIZE,x); // calling sum() function

printf(" Sum of X: %.2f ",sumx); // display sum of datax.txt elements

printf(" The summation of y is:");

sumy=sum(SIZE,y); // calling sum() function

printf(" Sum of Y: %.2f ",sumy); // display sum of datay.txt elements

printf(" The summation of xy is:");

sprod=sum_prod(SIZE,x,y); // calling sum_prod() function

printf(" Sum of X*Y Product: %.2f ",sprod); // display product of two array elements

printf(" The summation of x squared is:");

ssqx=sum_squared(SIZE,x); // calling sum_squared() function

printf(" Sum of X Squares: %.2f ",ssqx); // display sum of squared elements in x array

printf(" The summation of y squared is:");

ssqy=sum_squared(SIZE,y); // calling sum_squared() fucntion

printf(" Sum of Y Squares: %.2f ",ssqy); // display sum of squared elements in y array

// calculate Correlation coefficients

r1=(SIZE*sprod)-(sumx*sumy); // calculate numerator

r2= sqrt((SIZE*ssqx)- pow(sumx,2)); // calculate first square root elements

r3=sqrt((SIZE*ssqy)-pow(sumy,2)); // calculate second square root elements

r4=r2*r3; // product of two square root elements

r=r1/r4; // finally calculate coefficient elements

printf(" Correlation Coefficient between data sets X and Y is: %f",r); // display correlation coefficient b/w x and y elements

// close two files

fclose(fp1);

fclose(fp2);

  

return 0;

}

// implement sum() function

float sum(int size,float array1[])

{

float s=0.0; // declare constant float value s=0

int i; // declare integer variable i

for(i=0;i<size;i++) // create for loop until size

s+=array1[i]; // calculate sum of array elements

return s; // return resultant sum of array elements

}

// implement sum_prod() function

float sum_prod(int size, float array1[], float array2[])

{

float sp=0.0; // declare constant float value sp=0

int i;

for(i=0;i<size;i++) // create for loop until size

sp+=array1[i]*array2[i]; // calculate product of two array elements

return sp; // return resultant sum of product elements

}

// implement sum_squared() function

float sum_squared(int size, float array1[])

{

float sq; // declare float variable sq

sq=sum(size,array1); // calling sum of array elements

return sq*sq; // return resultant square of sum of array elements

}

OUTPUT


datax.txt File Length: 200
datay.txt File Length: 200


The summation of x is:
Sum of X: 20527.00

The summation of y is:
Sum of Y: 9627.00

The summation of xy is:
Sum of X*Y Product: 1126477.00

The summation of x squared is:
Sum of X Squares: 421357728.00

The summation of y squared is:
Sum of Y Squares: 92679128.00


Correlation Coefficient between data sets X and Y is: 0.000704