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

Write a program that declares an array holding five float values in the main fun

ID: 3573825 • Letter: W

Question

Write a program that declares an array holding five float values in the main function. The program should also have three other functions:  InitArray,  PrintArray, and SumArray :

// Initialize array with input from the user
// Pre: numArray should point at a valid integer array and num should hold the number of array elements
// Post: ary should contain integers entered by the user
void InitArray(/* OUT */ float numArray
[], /* IN */ int numEntries)

// Print out the contents of an array
// Pre: Valid array and number of elements in the array
// Post: Prints array contents
void PrintArray(/* IN */ const float numArray[], /* IN */ int numEntries);

// Calculate the sum of all elements in the array and return the result
// Pre: numArray should contain valid values
// Post: Return the sum of all values in the array. numArray is unchanged.
float SumArray(/* IN */ const int numArray[], /* IN */ int numEntries)

Explanation / Answer

#include <iostream>

using namespace std;
void InitArray( float numArray[], int numEntries) {
cout<<"Enter "<<numEntries<<" array elements: "<<endl;
for(int i=0; i<numEntries; i++){
cin >> numArray[i];
}
}
void PrintArray(const float numArray[], int numEntries){
cout<<numEntries<<" array elements are: "<<endl;
for(int i=0; i<numEntries; i++){
cout<< numArray[i]<<" ";
}
cout<<endl;
}
float SumArray(const float numArray[], int numEntries) {
float sum = 0;
for(int i=0; i<numEntries; i++){
sum = sum + numArray[i];
}
return sum;
}
int main()
{
float a[5];
InitArray(a,5);
PrintArray(a,5);
cout<<"Sum is "<<SumArray(a,5)<<endl;
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                           

Enter 5 array elements:                                                                                                                                                                                                                                

1.1 2.2 3.3 4.4 5.5                                                                                                                                                                                                                                    

5 array elements are:                                                                                                                                                                                                                                  

1.1 2.2 3.3 4.4 5.5                                                                                                                                                                                                                                    

Sum is 16.5

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