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

0 A. Write a function named addup which will add all the elements in an array of

ID: 3915347 • Letter: 0

Question

0 A. Write a function named addup which will add all the elements in an array of integers. The function should return the sum. The array and the size of the array are passed as parameters. B. (*)Write a prototype for the function in the previous problem. C. (*)Write a function named fill that will read values into an array from a file. Assume that the array is large enough to hold all values in the file, and that there may be more elements in the array than are needed. Your function should read values into the array until end of file is reached. The array is passed as a parameter. The function should return the array and the number of values read into the array. (You can declare and open the input file inside the function.) D. () Write a prototype for the function in the previous problem. E. () Write the statements to declare an array, call the fill function, call the addup function, and print the sum.

Explanation / Answer

//A,B

#include <stdio.h>

int addUp(int *arr,int size);//prototype

int main(int argc, char const *argv[])
{
   int n=0;
   scanf("%d",&n);
   int a[n];
   for (int i = 0; i < n; ++i)
   {
       scanf("%d",&a[i]);
   }

   printf("sum is %d ",addUp(a,n) );
   return 0;
}

int addUp(int *arr,int size){
   int sum=0;
   int *p;
   p=arr;
   for (int i = 0; i < size; ++i)
   {
       sum=sum+ *p;
       p++;
   }
   return sum;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//C,D,E

#include <stdio.h>
#include<stdlib.h>
////////////////////////////////
struct result
{
    int *arr;
    int num;
};
struct result*fill(int *a);//prototype
// a function cant return two values so we made a structure
int addUp(int *arr,int size);//prototype
int main(int argc, char const *argv[])
{
    int a[1000];
    struct result *r=malloc(sizeof(struct result *));
    r=fill(a);
    int *p;
    p=r->arr;
    int num=r->num;
    printf("%d ",num);
    // for (int i = 0; i < num; ++i)
    // {
    //    printf("%d ",*p);
    //    p++;
    // }
    printf("%d ",addUp(p,num));
    return 0;
}

struct result*fill(int *a){
    FILE *myFile;
    myFile = fopen("numbers.txt", "r");
    int *p;
    p=a;

    //read file into array
    int i=0;
    if (myFile != NULL) {
        printf("file open ");
        while (!feof(myFile)) {
           fscanf(myFile,"%d",p);
           // printf("%lf ",array[i]);
           i++;
           p++;
        }
    }
    fclose(myFile);
    struct result *r=malloc(sizeof(struct result *));
    r->arr=a;
    r->num=i;

    return r;

}

int addUp(int *arr,int size){
    int sum=0;
    int *p;
    p=arr;
    for (int i = 0; i < size; ++i)
    {
        sum=sum+ *p;
        p++;
    }
    return sum;
}


//Please rate and ask in comment if any doubt