Write a c code program to solve
5a. Given the data as follows:
x[100] = {61,58,97,7,72,91,20,90,83,67,66,54,15,62,38,44,60,88,12,99,86,45,84,30,52,76,39,27,27,59,45,45,98,80,22,95,6,3,21,30,37,86,95,7,41,95,23,5,1,22,68,14,71,39,37,97,9,26,42,71,75,4,79,32,42,9,35,13,31,95,78,83,12,21,60,2,28,67,21,69,11,45,13,52,26,16,43,56,31,74,35,5,41,33,36,6,75,8,30,42};
y[100] = {75,63,113,23,79,96,35,99,102,81,69,70,29,65,43,63,65,89,23,100,98,54,101,42,54,90,42,46,28,74,61,60,106,98,32,108,9,20,30,49,53,91,98,10,46,100,28,14,8,39,80,32,86,54,55,100,12,36,48,85,80,9,92,45,62,29,50,30,39,98,94,96,32,37,80,5,43,73,34,79,23,64,22,54,42,24,53,62,44,91,48,15,60,40,37,7,91,28,45,48}.
Compute the average, variance and standard deviation (S.D.) of x and y separately.
5b. Given the data as follows:
x[100] = {61,58,97,7,72,91,20,90,83,67,66,54,15,62,38,44,60,88,12,99,86,45,84,30,52,76,39,27,27,59,45,45,98,80,22,95,6,3,21,30,37,86,95,7,41,95,23,5,1,22,68,14,71,39,37,97,9,26,42,71,75,4,79,32,42,9,35,13,31,95,78,83,12,21,60,2,28,67,21,69,11,45,13,52,26,16,43,56,31,74,35,5,41,33,36,6,75,8,30,42};
y[100] = {75,63,113,23,79,96,35,99,102,81,69,70,29,65,43,63,65,89,23,100,98,54,101,42,54,90,42,46,28,74,61,60,106,98,32,108,9,20,30,49,53,91,98,10,46,100,28,14,8,39,80,32,86,54,55,100,12,36,48,85,80,9,92,45,62,29,50,30,39,98,94,96,32,37,80,5,43,73,34,79,23,64,22,54,42,24,53,62,44,91,48,15,60,40,37,7,91,28,45,48}.
Compute Linear regression, i.e., find a and b such that y=ax+b results in the smallest total error.
5c. For the data given in problem 5, check the relationship between average(x) and average(y), and also the relationship between S.D.(x) and S.D. (y). Explain your observation.
Please write out the programs and have screenshots of the outputs
5a. Code in C and output
#include <stdio.h>
#include <math.h>
float sum(int arr[]){
float result = 0;
for(int i = 0; i<100;i++){
result += arr[i];
}
return result;
}
float avg(float total){
return total / 100;
}
float var(int arr[], float myu){
float result = 0;
for(int i = 0; i <100; i++){
result += ((arr[i] - myu)*(arr[i] - myu))/100;
}
return result;
}
float std(float variance){
return sqrt(variance);
}
int main(){
// intialize the data
int x[100] = {61,58,97,7,72,91,20,90,83,67,66,54,15,62,38,44,60,88,12,99,86,45,84,30,52,76,39,27,27,59,45,45,98,80,22,95,6,3,21,30,37,86,95,7,41,95,23,5,1,22,68,14,71,39,37,97,9,26,42,71,75,4,79,32,42,9,35,13,31,95,78,83,12,21,60,2,28,67,21,69,11,45,13,52,26,16,43,56,31,74,35,5,41,33,36,6,75,8,30,42};
int y[100] = {75,63,113,23,79,96,35,99,102,81,69,70,29,65,43,63,65,89,23,100,98,54,101,42,54,90,42,46,28,74,61,60,106,98,32,108,9,20,30,49,53,91,98,10,46,100,28,14,8,39,80,32,86,54,55,100,12,36,48,85,80,9,92,45,62,29,50,30,39,98,94,96,32,37,80,5,43,73,34,79,23,64,22,54,42,24,53,62,44,91,48,15,60,40,37,7,91,28,45,48};
float x_bar = avg(sum(x));
float y_bar = avg(sum(y));
float x_var = var(x,x_bar);
float y_var = var(y,y_bar);
float x_std = std(x_var);
float y_std = std(y_var);
printf("The average of X = %f\n", x_bar);
printf("The average of Y = %f\n", y_bar);
printf("The variance of X = %f\n", x_var);
printf("The variance of Y = %f\n", y_var);
printf("The std deviation of X = %f\n", x_std);
printf("The std deviation of Y = %f\n", y_std);
return 0;
}
5b. Code and output
#include <stdio.h>
#include <math.h>
float sum(int arr[]){
float result = 0;
for(int i = 0; i<100;i++){
result += arr[i];
}
return result;
}
float sum_2(int arr[]){
float result = 0;
for(int i = 0; i<100;i++){
result += pow(arr[i],2);
}
return result;
}
float sumxy(int arr1[], int arr2[]){
float result = 0;
for(int i = 0; i<100;i++){
result += arr1[i] * arr2[i];
}
return result;
}
int main(){
// intialize the data
int x[100] = {61,58,97,7,72,91,20,90,83,67,66,54,15,62,38,44,60,88,12,99,86,45,84,30,52,76,39,27,27,59,45,45,98,80,22,95,6,3,21,30,37,86,95,7,41,95,23,5,1,22,68,14,71,39,37,97,9,26,42,71,75,4,79,32,42,9,35,13,31,95,78,83,12,21,60,2,28,67,21,69,11,45,13,52,26,16,43,56,31,74,35,5,41,33,36,6,75,8,30,42};
int y[100] = {75,63,113,23,79,96,35,99,102,81,69,70,29,65,43,63,65,89,23,100,98,54,101,42,54,90,42,46,28,74,61,60,106,98,32,108,9,20,30,49,53,91,98,10,46,100,28,14,8,39,80,32,86,54,55,100,12,36,48,85,80,9,92,45,62,29,50,30,39,98,94,96,32,37,80,5,43,73,34,79,23,64,22,54,42,24,53,62,44,91,48,15,60,40,37,7,91,28,45,48};
int n = 100;
float sum_xy = sumxy(x,y);
// Y = aX + b
// by linear regression formulas to calculate the slope and intercept
float a = (sum(y)*sum_2(x) - sum(x)*sum_xy) / (n*sum_2(x) - pow(sum(x),2));
float b = (n*sum_xy - sum(x)*sum(y)) / (n*sum_2(x) - pow(sum(x),2));
printf("For the linear regression (Y = aX + b), we get a = %f, b = %f\n", a,b);
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.