Functions and Array Help!

So i'm honetly having trouble doing this program, I feel like it's simple, but I'm not sure how to even do the display part of the assignment.

the assignment is as follows:
Create a program that:

1. Prompts the user to input values for an array of five integers

2. Displays a menu, where the user may choose

a. Sum - Calculates and displays the sum of numbers in the array

b. Mean - Calculates and displays the average of numbers in the array

c. Display - Displays the current values held in the array

d. Edit - The user is re-prompted for input into the array

e. Exit - terminate the program

3. Loop back to step 2

a. There should be at least 3 functions used in this program



You should probably use a global const int for array size. Do not use global variables unless they’re constants!




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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const int SIZE = 5;
int array[SIZE];

int displayArray()
{
const int SIZE = 5;
int array[SIZE];

//////////////////
//Display of Array

cout << "The numbers you entered are as follows: ";
cout << " " << array[0];
cout << " " << array[1];
cout << " " << array[2];
cout << " " << array[3];
cout << " " << array[4];
cout << endl;

return array[4];
}



int main ()
{

const int SIZE = 5;
int array[SIZE];

/////////////////////
//User Input of Array

cout << "Enter first number: \n";
cin >> array[0];

cout << "Enter second number: \n";
cin >> array[1];

cout << "Enter third number: \n";
cin >> array[2];

cout << "Enter fourth number: \n";
cin >> array[3];

cout << "Enter fifth number: \n";
cin >> array[4];

/* //////////////////
//Display of Array

cout << "The numbers you entered are as follows: ";
cout << " " << array[0];
cout << " " << array[1];
cout << " " << array[2];
cout << " " << array[3];
cout << " " << array[4];
cout << endl; */

displayArray();

//////////////
//Sum of Array

float average, sum = 0;
for (int tnum = 0; tnum < SIZE; tnum++)
sum += array[tnum];


///////////////
//Mean of Array

average = sum/SIZE;


////////
//Output


cout << "sum: " << sum << endl;
cout << "average: " << average << endl;










system ("read");




return 0;

}
So i'm honetly having trouble doing this program, I feel like it's simple, but I'm not sure how to even do the display part of the assignment.


Use a char or std::string variable to store the user input, then either use switch or if to select what happens according to the given char.


1. Prompts the user to input values for an array of five integers


Better use a for for this, it makes the code cleaner
1
2
for(int i=0; i<5;+=i)
std::cin>>array[i];



2. Displays a menu, where the user may choose


Like I said user either switch or if according to an "option" variable that can be either int, char, std::string. User will input this variable to select.
1
2
3
4
5
6
7
8
9
10
11
12
13
while(true)
std::cout<<"Please select one operation from the following: \n 
\ Sum \n
\ Mean \n'"; introduce all operations here

std::string option;
std::cin>>option;

if(option=="Sum") // make sum;
else if(option=="Mean") //make mean
else if(option=="Exit") break; //exit loop
// continue with all



a. Sum - Calculates and displays the sum of numbers in the array


1
2
3
4
5
int sum=0;
for(int i=0; i<5;+=i)
sum+=array[i];

std::cout<<sum;



b. Mean - Calculates and displays the average of numbers in the array


1
2
3
4
5
6
int mean=0;
for(int i=0; i<5;+=i)
mean+=array[i];

mean/=5;
std::cout<<mean;



c. Display - Displays the current values held in the array


1
2
for(int i=0; i<5;+=i)
std::cout<<array[i];



d. Edit - The user is re-prompted for input into the array

Use the option variable again.


a. There should be at least 3 functions used in this program


use functions for sum, mean, display and so on
Last edited on
Topic archived. No new replies allowed.