How can we add two array?

Need help with c programming array question :)?

  • Thanks for checking out my question :) So, the question goes like this: Write a program, addarrays.c, which reads two arrays x1 and x2 from the file addarrays.in, computes their sum and writes the sum to the file addarrays.out. In more detail main carrys out the following steps: open the two files addarrays.in and addarrays.out. read the array size, n, from the file. allocate the two arrays x1 and x2 read the values from the file into the two arrays call the function add which creates a new array, y, that is the sum of the two arrays x1 and x2 write the array size followed by sum, y, to the file addarrays.out. Array size allocation has to be done dynamically. ______________________________________… And here is my solution: #include <stdio.h> #include <stdlib.h> void add(float *x1,float *x2,float *y,int *n); int main(void) { float* x1; float* x2; float *y; /* the sum of x1 and x2 */ int n; /* the size of the arrays */ int i=0; float sum; FILE *fin = fopen("addarrays.in","r"); FILE *fout = fopen("addarrays.out","w"); fscanf(fin,"%d",&n); x1 = (float*) malloc(n*sizeof(float)); x2 = (float*) malloc(n*sizeof(float)); for(i=0;i<n;i++) { fscanf(fin,"%f%f",&x1[i],&x2[i]); } add(x1,x2,y,&n); fprintf(fout,"%d\n",n); for(i=0;i<n;i++) { //fprintf(fout,"%f\n",x1[i]); fprintf(fout,"%f\n",y[i]); } fclose(fin); fclose(fout); system("pause"); return 0; } void add(float *x1,float *x2,float *y,int *n) { int i; y = (float*) malloc((*n)*sizeof(float)); for(i=0;i<*n;i++) { y[i] = x1[i]+x2[i]; } } ______________________________________… The program just crashes :( Any help is appreciated :)

  • Answer:

    The problem is that the output array is allocated in the "add()" method, but is NOT returned to the calling method. In order to change your program as little as possible, I just turned it into a reference parameter. #include <stdio.h> #include <stdlib.h> void add (float *x1, float *x2, float **y, int *n); int main (void) {   float *x1;   float *x2;   float *y; /* the sum of x1 and x2 */   int n; /* the size of the arrays */   int i = 0;   float sum;   FILE *fin = fopen ("addarrays.in", "r");   FILE *fout = fopen ("addarrays.out", "w");   fscanf (fin, "%d", &n);   x1 = (float *) malloc (n * sizeof (float));   x2 = (float *) malloc (n * sizeof (float));   for (i = 0; i < n; i++) {     fscanf (fin, "%f%f", &x1[i], &x2[i]);   }   add (x1, x2, &y, &n);   fprintf (fout, "%d\n", n);   for (i = 0; i < n; i++) {     //fprintf(fout,"%f\n",x1[i]);     fprintf (fout, "%f\n", y[i]);   }   fclose (fin);   fclose (fout);   // system ("pause");   return 0; } void add(float *x1, float *x2, float **y, int *n) {   int i;   *y =  (float *) malloc ((*n) * sizeof (float));   for (i = 0; i < *n; i++) {     (*y)[i] = x1[i] + x2[i];   } }

sam T at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.