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

C++ a. Implement a function named avg that takes an int array and its length as

ID: 3814265 • Letter: C

Question

C++

a. Implement a function named avg that takes an int array and its length as parameters and returns the average of the values in the array.
Call the function with a sample array and print the average value.

b. Implement a function named min_max that takes an int array, its length, and two int parameters min and max. The function sets min to the min value in the array and max to the max value in the array. The function returns nothing. Call the function and print the min and max values found.

c. Implement a function named getScores that takes an int array, its length, and an int parameter scores_entered. The function repeatedly asks the user to enter scores until the user enters -1 or the array capacity has been reached. The function stores the scores entered in the array and sets the scores_entered parameter to the number of scores entered by the user. The function returns nothing.

d. Implement a function named scoreStatistics that gets up to 100 scores from the user and prints the min, the max and the average score.
The function takes no parameters and returns nothing. It does all its work by relying on the functions in a, b, and c implemented above.

e. Write a simple program that creates a multiplication table (10x10 two dimensional array). Once the table has been populated with values write two loops that print the values along the two diagonals of the the multiplication table.

Explanation / Answer

a.

#include <iostream>

#include <string>

using namespace std;

double avg(int array[5],int length)
{
     double sum = 0;
     int i;
     for(i=0;i<length;i++)
     {
         sum = sum + array[i];
     }
   
     return sum/length;
}

int main( )
{

       int array[5] = {23,54,65,76,33};
     
       cout<<"Average = "<<avg(array,5);// function call

       return 0;

}

Output:

Average = 50.2

b

#include <iostream>

#include <string>

using namespace std;

void min_max(int a[5],int length,int *min,int *max)
{
   
     int i;
     *min= *max = a[0]; //assign first element to be min and max
     for(i=1;i<length;i++)
     {
         if(*min > a[i]) //compare elements with min
         *min = a[i];
         if(*max < a[i]) //compare elements with max
         *max = a[i];
       
     }
   
   
}

int main( )
{

       int array[5] = {23,54,65,76,33};
       int min ,max;
       min = max = 0;
       min_max(array,5,&min,&max);// function call
     
       cout<<" Minimum = "<<min;
       cout<<" Maximum = "<<max;
     
     
       return 0;

}

Output:

Minimum = 23

Maximum = 76

c.

#include <iostream>

#include <string>

using namespace std;

void getScores(int a[10],int length,int scores_entered)
{
   
     int i;
   
     cout<<" Enter -1 to quit";
     for(i=1;i<length;i++)
     {
        cout<<" Enter score : ";
        cin>>a[i];
        if(a[i] == -1)
        break;
        else
        scores_entered++;
       
     }
   
     cout<<" Scores Entered = "<<scores_entered;
}

int main( )
{

       int array[10];
       getScores(array,10,0);
     
     
       return 0;

}

Output:

Enter score :90

Enter score :62

Enter score :49

Enter score :87

Enter score :67

Enter score : 45

Enter score : 56

Enter score : -1

Scores Entered = 7

d

void ScoreStatistics()
{
     int array[100];
     int min,max;
   
     min = max = 0;
   
     getScores(array,100,0);
    
     double average = avg(array,100);
   
     cout<<" Average = "<<average;
   
     min_max(array,100,&min,&max);
   
     cout<<" Minimum = "<<min;
     cout<<" Maximum = "<<max;
}

e.

#include <iomanip>

#include <string>

using namespace std;

int main( )
{
    int i,j,prod;
     for(i=1;i<=10;i++)
     {
         for(j=1;j<=10;j++)
         {
             prod = i*j;
             cout<<prod<<" ";
           
         }
        cout<<" ";
     }
     for(i=1;i<=10;i++)
     {
         for(j=1;j<=10;j++)
         {
             if(i==j || (i+j)== 11) //diagonals
             cout<<i*j;
             else
             cout<<" ";
         }
         cout<<" ";
     }
       return 0;

}

Output:

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