Problem with Function/Arrays

So, I have to take the user input of 10 numbers, put them into an array, compute the max/min/sum/average of the array, and then use a void function called Output to print the results.

I'm having a problem with this last function, Output.

Here are the exact instructions for this part:

The function Output should take as input parameters the largest and smallest values and the sum and average. It need not return
a value and should output the appropriate values as shown below.
From your list of 10 numbers:
Smallest: -8
Largest: 14
Sum: 24
Average: 2.4


It's probably simple, but I can't figure out how to get the return values from all the other functions into this output function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
using namespace std;

void Instruct();
void GetNumbers(int [],int);
int FindLargest(int [],int);
int FindSmallest(int [],int);
int Sum(int [],int);
float Avg(int,int);
void Output(int,int,int,float);

int main()
{
    int array[10];
    Instruct();
    GetNumbers(array,10);
    FindLargest(array,10);
    FindSmallest(array,10);
    Sum(array,10);
    Avg(Sum(array,10),10);
    Output(larg,small,sum,avg);
    
    
    
    return 0;
}
void Instruct(void) //This function provides an introduction to the program.
{
    cout<<"This program will determine the smallest and largest value in a set of ten numbers."<<endl<<"It will also compute the sum and average of these numbers."<<endl;
}
void GetNumbers (int a[],int n) //This function takes the the user input and places it in an array
{
    cout<<endl<<"Please enter a set of ten numbers each separated by a space:  "<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    cout<<endl<<"Your array has these values:  ";
    for(int i=0;i<n;i++)
    {cout<<a[i]<<" ";}
    cout<<endl;
}
int FindLargest (int a[],int n) //This function finds the largest value in the array.
{
    int larg=a[0];
    for (int i=0;i<n;i++)
    { if (a[i]>larg)
            larg=a[i];
    }
    return larg;
}   
int FindSmallest (int a[],int n) //This function finds the smallest value in the array.
{
    int small=a[0];
    for (int i=0;i<n;i++)
    {  if (a[i]<small)
            small=a[i];
    }
    return small;
}
int Sum (int a[],int n) //This function finds the sum of the array.
{
    int sum=0;
    for (int i=0;i<n;i++)
        sum+=a[i];
    return sum;
    
}
float Avg (int s,int n) //This function finds the average of the array.
{
    float avg=0;
    avg=s/n;
    
    return avg;
}

void Output(int l,int sm,int sum, float avg) 

{
    cout<<endl<<"From your list of ten numbers: "<<endl;
    cout<<"\t Largest:\t"<<l<<endl;
    cout<<"\t Smallest:\t"<<sm<<endl;
    cout<<"\t Sum:\t"<<sum<<endl;
    cout<<"\t Average:\t"<<avg<<endl;
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    int array[10];
    Instruct();
    GetNumbers(array,10);
    int larg = FindLargest(array,10);
    int small = FindSmallest(array,10);
    int sum = Sum(array,10);
    float avg = Avg(sum,10);
    Output(larg,small,sum,avg);
    return 0;
}

Or I suppose you could do this:
1
2
3
4
5
6
7
8
int main()
{
    int array[10];
    Instruct();
    GetNumbers(array,10);
    Output(FindLargest(array,10),FindSmallest(array,10),Sum(array,10),Avg(Sum(array,10),10));
    return 0;
}
Cool - I had written something similar to the second one while waiting for a reply.

I hadn't considered that we could declare a variable and assign it to a function at the same time. Thanks!
Topic archived. No new replies allowed.