1 Add a function at a time to your program it will be easie

1. Add a function at a time to your program -- it will be easier to find syntax errors

2. Another time saver: add a function to read the data from a file

Exercise 7.1 roduce the use of arrays and This is a simple exercise to introduce the use of arrays and some of the common errors that occur when programming with them » Write a program that allows the user to enter 25 integers into an array, then prints the array and calculates, stores and prints the sum and average of the values in the array. Print this progran to be turned in along with sample output NOTE: Your program must have separate functions to: NOTE: Your program in 1. Print the array contents 2. Calculate the sum of the array contents 3. Calculate the average of the array content:s » Add a line to the end of the program that prints the 16th element of the array. What gets printed when you run the program? Try running th consistency in what gets printed e program a few times. Is there any for the 11th element? » Add a line at the end of the program to print the 10,000th element of the array. What happens now? What is the exact error message you get? » \"Turn in vou r original program and the results of the last two experl- ments

Solution

Hi friend, please find my C++ code:

#include <iostream>

using namespace std;

// function prototype
void printArray(int arr[], int size); // function to print array content
int sum(int arr[], int size); // finction to get sum of array content
double average(int arr[], int size); // function to get average of array elements

int main(){

   // decalaring int array of size 25
   int size = 2;
   int arr[size];

   cout<<\"Enter \"<<size<<\" integers: \"<<endl;
   for(int i=0; i<size; i++)
       cin>>arr[i];

   printArray(arr, size);

   cout<<\"Sum: \"<<sum(arr, size)<<endl;
   cout<<\"Average: \"<<average(arr, size)<<endl;

   // getiing 16th element
   cout<<\"16th element: \"<<arr[15]<<endl;

   cout<<\"10000th element: \"<<arr[10000]<<endl;


   return 0;
}


void printArray(int arr[], int size){

   for(int i=0; i<size; i++){
       cout<<arr[i]<<\" \";
   }

   cout<<endl;
}

int sum(int arr[], int size){

   int total = 0;
   for(int i=0; i<size; i++)
       total += arr[i];

   return total;
}

double average(int arr[], int size){

   double total = sum(arr, size); // getting sum of all elements

   return total/size;
}

// 10000th element is out of bound, so either it will print garbage value or it throws sigmentation fault error

1. Add a function at a time to your program -- it will be easier to find syntax errors 2. Another time saver: add a function to read the data from a file Exerci
1. Add a function at a time to your program -- it will be easier to find syntax errors 2. Another time saver: add a function to read the data from a file Exerci

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site