Write a C++ program that prompts the user to enter 1000 integer values

QUESTION

Write a C++ program that prompts the user to enter 1000 integer values then performs three tasks. Each value is positive and represents the time in seconds for one run of a scientific experiment. Thus, 1000 values represent seconds for 1000 runs.

Include a sub-function called GetData, which takes two arguments and inputs the 1000 integer values from the user.

Include a sub-function called CalcTotalSecs, which takes two arguments and returns the total number of seconds taken by all 1000 runs.

Include another sub-function, called Convert, which takes three arguments and uses the total number of seconds to determine the equivalent in minutes and remaining seconds.

Include a sub-function, called Print, which takes three arguments and outputs to the user the total number of seconds and the equivalent amount of time in hours and seconds.

For example, if the first value entered by the user is 2 and all the remaining 999 values are 1, then the output from the Print sub-function would be: 1001 total seconds is equivalent to 16 minutes and 41 seconds.

 

SUMMARY

Assumptions to be made : 

1) you do not have to check the input values i.e all input values can be stored as int

2) the user will enter only positive integers

3) only the main algorithm is required for header documentation.

For every run, the time taken in seconds is stored in an array for 1000 runs. Particular functions are defined to find the equivalent total time taken for these 1000 runs.

 

EXPLANATION

The GetData method takes in an array called runs and gets input for that array. The CalcTotalSecs() function is used to find the sum of all the elements in the array ‘runs’ and returns the sum. This sum is then passed to a function called Convert which to converts seconds into minutes and seconds. The total time taken for 1000 runs in minutes and seconds is then printed as the output.

 

CODE

#include <iostream>

using namespace std;

/*To get the data of the runs */
void GetData(int runs[1000])

{
    runs[0]=2;

    for(int j=1;j<1000;j++)
    runs[j]=1;
}
int CalTotalSecs(int runs[],int n)
{
    int s=0;
    for(int i=0;i<n;i++)
    s=s+runs[i];
    return s;
}
/* Converting seconds into minutes and seconds */
int Convert(int totsecs, int &min, int &sec)
{
    min=min+totsecs/60;
    sec=totsecs%60;
}
void Print(int totsecs,int min,int sec)

{

    cout<<"Total time in seconds = "<<totsecs<<endl;

    cout<<"Equivalent time in minutes and seconds: "<<endl;

    cout<<min<<" minutes and "<<sec<<" seconds"<<endl;

}

int main()

{
    int runs[1000];

    GetData(runs);

    int totsecs=CalTotalSecs(runs,1000);

    int min=0,sec=0;

    Convert(totsecs,min,sec);

    Print(totsecs,min,sec);

}

 

 

OUTPUT

 

 

Also Read: Which command will you use to call a function?

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *