Please use simple C language. Do not use cout or cin instead use printf and scan
ID: 3866937 • Letter: P
Question
Please use simple C language. Do not use cout or cin instead use printf and scanf. Thank you so much 1. If two arrays have the same size we can add them as we do with vectors. For example: x1 x2 3.8 5.4 5.3 2.7 3.7 2.1 3.2 2.0 3.0 0.0 3.0 Write a program, addarrays.c, which reads two arrays x1 and x2 from the file addarrays.txt computes their sum and writes the sum to the file addarraysout.txt In more detail main carries out the following steps open the two files addarrays.txt and addarraysout.txt . read the values from the file into the two arrays call the function add to compute the sum, y, of two arrays xl and x2 write the array size followed by sum, y, to the file addarraysout.txt. File: addarrays.c This program reads two arrays from a file then calls a function to compute their sum and writes the sum to a file Assignment: 5 Programmer Date: Question: 1 DalID: Course: ENGM 1081 include #define MAXN 10 void add (int n, float* x1, floats x2, float* y); int main (void) float xi[MAXN]; float x2[MAXN]; float y [MAXN* the sum of x1 and x2/ int n / the size of the arrays/
Explanation / Answer
#include<stdio.h>
#define MAXN 10
void add(int n,float *x1,float *x2,float *y);
int main()
{
float x1[MAXN];
float x2[MAXN];
float y[MAXN];
char x_1[10];
char x_2[10];
int n=0;
int i=0;
FILE *fp =NULL;
FILE *fp1 =NULL;
fp= fopen("addarrays.txt","r"); // File opened for reading
fscanf(fp,"%s",x_1);fscanf(fp,"%s",x_2); // Skipping x1 and x2 in file
while(fscanf(fp, "%f", &x1[n]) != EOF && fscanf(fp, "%f", &x2[n]) != EOF){ // Fetching both the values of x1 and x2 and storing in arrays
n++;
}
fp1= fopen("addarraysout.txt","r+"); // Open the second file
fprintf(fp1,"Size of array = %d Sum ",n);
for(i=0;i<n;i++)
{
y[i]=x1[i]+x2[i];
fprintf(fp1,"%f ",y[i]); // Write the answer
}
fclose(fp);
fclose(fp1); // Close both the file pointers
return 0;
}