FIX THIS C PROGRAM FOR ME PLEASE THANK YOU If two arrays hav
FIX THIS C PROGRAM FOR ME PLEASE. THANK YOU.
If two arrays have the same size, we can “add” them as we do with vectors. Write a program, addarrays.c, which reads two arrays x1 and x2 from the file addarraysin.txt, and writes their sum to the file addarraysout.txt.
In more detail, the main carries out the following steps:
• opens the two files addarraysin.txt and addarraysout.txt.
• reads the pairs of values from the file addarraysin.txt into the two arrays x1 and x2
• calls the function add which adds the two arrays and stores the result into the array sum
• writes the size of sum followed by the values of sum to the file addarraysout.txt.
The output should look like this:
Size of arrays is 5
x1 x2 sum
1.1 2.7 3.8
3.7 1.7 5.4
2.1 3.2 5.3
1.0 2.0 3.0
0.0 3.0 3.0
#include
#define N 100
void add(float *x, float *y, float *sum, int n);
int main(void){
int i, n;
float x[N], y[N], sum[N];
FILE *fin = fopen(\"addarraysin.txt\", \"r\");
FILE *fout = fopen(\"addarraysout.txt\", \"w\");
fscanf(fin, \"%d\", &n);
fprintf(fout, \"Size of arrays is %d\ \ \", n);
for(i = 0; i < n; i++){
fscanf(fin, \"%f %f\", &x[i], &y[i]);
}
add(x, y, sum, n);
fprintf(fout, \"x1 x2 sum\ \ \");
for(i = 0; i < n; i++){
fprintf(fout, \"%2.1f %2.1f %2.1f\ \", x[i], y[i], sum[i]);
}
fclose(fin);
fclose(fout);
system(\"notepad.exe addarraysout.txt\");
return 0;
}
void add(float *x, float *y, float *sum, int n){
int i;
for(i = 0; i < n; i++){
sum[i] = x[i] + y[i];
}
return;
}
This is addarraysin.txt
1.1 2.7
3.7 1.7
2.1 3.2
1.0 2.0
0.0 3.0
Solution
#include<stdio.h>
#include<stdlib.h>
#define N 100
void add(float *x, float *y, float *sum, int n);
int main(void){
int i, n=5;
float x[N], y[N], sum[N];
FILE *fin = fopen(\"addarraysin.txt\", \"r\");
FILE *fout = fopen(\"addarraysout.txt\", \"w\");
for(i = 0; i < n; i++){
fscanf(fin, \"%f %f\", &x[i], &y[i]);
}
fprintf(fout, \"Size of arrays is %d\ \ \", n);
add(x, y, sum, n);
fprintf(fout, \"x1 x2 sum\ \ \");
for(i = 0; i < n; i++){
fprintf(fout, \"%2.1f %2.1f %2.1f\ \", x[i], y[i], sum[i]);
}
fclose(fin);
fclose(fout);
system(\"notepad.exe addarraysout.txt\");
return 0;
}
void add(float *x, float *y, float *sum, int n){
int i;
for(i = 0; i < n; i++){
sum[i] = x[i] + y[i];
}
return;
}


