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

For C++ 10 ) A. Write a function named addup which wil1 add all the elements in

ID: 3915391 • Letter: F

Question


For C++ 10 ) A. Write a function named addup which wil1 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 Erom 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.) 2 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.

int addup(int data[], int n){

    int sum = 0;
    for (int i = 0; i<n; i++){
        sum = sum + data[i];
    }
    return sum;
}

B
int addup(int data[], int n);


C) int fill(int *data){

        ifstream fin("input.txt");
        if (!fin){
           cout << "Error opening file ";
           return -1;
        }
        int count = 0;
        while(!fin.eof()){
             fin >> data[count];
             count++;
         }
        fin.close();
        return count;
    }


D) int fill(int *data);


E)

   int data[10000];
   int n;
   n = fill(data);
   if (n < 0)
      cout << "Error in opening file ";
   if (n == 0)
      cout << "No data in the file ";
   if (n > 0){
      int *data1 = new int[n];
      for (int i = 0; i<n; i++){
          data1[i] = data[i];
      }
      printf("Sum : %d ",addup(data1,n));
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote