CArrays functions output formatting Objectives Declare and u

C++:Arrays, functions, output formatting

Objectives: Declare and use a constant, create and use arrays, create and use functions, create prototypes for functions, create random numbers, use repetition, pass arrays to functions, format output.

Overview: Creating an array and three functions to process an array. The first function fills an array with random numbers. The second function calculates a sum for the elements of an array. The third function displays an array in a nice format.

Program requirements:

i)You must use prototypes for all of the functions other than main.

ii)You may not have any global variables.

iii)In main, use a constant to define the size of the array. That constant should be set to 40 (but will be changed during testing).

iv)In main, define an array of integers using the constant as the size, and pass it to the required functions to fill the array and then display the array. Remember that you are passing by reference.

v)A function that will load random integers into an array. The random numbers should range from 1 through 100. The function has 2 parameters, the array and the size of the array. This function should not return anything.

vi)A function that will add all the values in an array and return the total. The function has 2 parameters, the array and the size of the array. It should return the total. The array should be passed as a constant to prevent changes to the content.

vii)A function that will output all the values in the array in rows which have 10 numbers per row. Each number should be in a field 4 characters wide. This function should also output the total and the average on separate lines after the array is displayed. This function has 2 parameters, the array and the size of the array. This function returns nothing. The array should be passed as a constant to avoid any unintended changes to the array.

Sample Runs:

Sample run #1

Sample run #2

Solution

#include\"iostream\"
using namespace std;
#include<string.h>
#include<stdlib.h>
void first(int a[], const int n);
int second(int a[], const int n);
void third(int a[], const int n);
int main()
{
        const int n=40;
        int arr[n];
        int i;
        first(arr,n);   
        int sum = second(arr,n);
        third(arr,n);
        return 0;
}
void first(int a[], const int n)
{
        int i=0;
        for(i=0;i<n;i++)
        {
                a[i] = rand() % 100 + 1;
        }
}
int second(int a[], const int n)
{
        int i=0,sum=0;
        for(i=0;i<n;i++)
        {
                sum += a[i];
        }
        return sum;
}
void third(int a[], const int n)
{
        int i=0,sum=0;
        for(i=0;i<n;i++)
        {
                sum += a[i];
                cout<<a[i]<<\" \";
                if(i%10==9)
                        cout<<\"\ \";
        }     
        cout<<\"The Total is\"<<sum<<\"\ \";
        cout<<\"The average is\"<<(float)sum/n<<\"\ \";
}

C++:Arrays, functions, output formatting Objectives: Declare and use a constant, create and use arrays, create and use functions, create prototypes for function
C++:Arrays, functions, output formatting Objectives: Declare and use a constant, create and use arrays, create and use functions, create prototypes for function

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site