C++ Programming Help

I'm a beginner and I need help with a homework I have for my C++ class. I understand most of the basic concepts but I just have trouble conceptualizing and putting the code together. My homework needs to do a few things:

1. Allow the user to input as many numbers as he wants with cin, but the numbers cannot exceed 300 as well as needing a way to break the sequence (maybe with a n==0; break;?).

2. Then the user has to choose whether he wants to A) Get the Average, or B) Get the Sum of these numbers that the user inputted.

3. The results of the average function or the sum function show up with cout.

I know about declaring functions and probably the necessity of using arrays, but I can't put it all together.

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
#include "stdafx.h"
#include <iostream>
#include <cmath>  
using namespace std;

int sum (int n);
int average (int n);
int inputtednumbers[100]

int main()
{
int n, i;

cout << "Input any amount of numbers. Each number can not exceed 500, entering 0 will end the program: ";

cin >> n;
if (n == 0)
break;

cout << "The sum of the numbers you entered is: " << endl;

cout << "The average of the numbers you entered is: " << endl;





return 0;
}

int sum (int n) 
{}

int average (int n){
{ n /2 };

system ("PAUSE");
return 0;
}



I know this is bare bones and filled with errors but I have no idea how to code it so that it takes all the numbers inputted from cin and using it in a function.
Last edited on
closed account (9wqjE3v7)
Have you learnt about dynamically allocating memory? If so then you can request the user to input the amount of numbers he/she wishes. Then loop it. You can loop it so that the user is asked to input a value for n[x] unless n==0. If n==0, then break the loop.

For instance:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
cout << "How many numbers do you wish to enter? ";
cin >> x;
int * numbers = new int [x]; //point to the first address of an array of x elements of the type int.

for(int y=0; y<x; y++) {
cout <<  "Enter Value: ";
cin >> numbers[y]; // Value pointed by numbers +  y.
if (numbers[y]==0) {
break;
}
else if(numbers[y]>300) {
//do blah
}
}


After that create and call the functions. You can determine what the user wishes to do with the values by requesting user input once again.
Last edited on
No I haven't learned that yet. Been learning out of C++: Without Fear 2nd Edition. How would the functions for sum and average look like for those user inputted values? Normally with just two values you can do like sum = x + y.
For this assignment are you allowed to use static variables? or global variables?
Well, since you are new, I decided to get the basics done for you so it would at least run, you might want to clean it up, but for the most part it is a good start.


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
#include <iostream>

using namespace std;

int sum (int n[],int times);
int average (int n[] ,int times);

int main(){
    
    int n, i,times;
    int inputtednumbers[100];

    do{
    cout << "Input any amount of numbers. Each number can not exceed 500,\nentering 0 will end the program: ";
    cout << "\n\nHow many numbers would you like to enter?";
    cin >> i;
    
    times = 0;
    
    if ( i == 0)
        return 0;

       do{
        cout << "Enter a number 1 - 500: ";
        cin >> n;
        inputtednumbers[times] = n;

          times++;
        }while( times < i);

        cout << "\nThe sum of the numbers you entered is: " << sum(inputtednumbers,i)<< endl;

        cout << "The average of the numbers you entered is: " <<average(inputtednumbers,i)<< endl;
    }while(i != 0);

    return 0;
}

int sum (int n[],int times){
    int sumTotal = 0;

    for(int x = 0; x < times; x++)
        sumTotal += n[x];

return sumTotal;
}

int average (int n[],int times){
    int total = sum(n,times);
    return total/times;
}
Integer division in the Ssturges code will give unexpected results.
How would the functions for sum and average look like for those user inputted values? Normally with just two values you can do like sum = x + y.

Basic math.
1
2
3
4
5
6
7
8
9
10
11
12
13
sum = x + y + z + w
    = (((x) + y) + z) + w

Let:
a0 = 0  + x
a1 = a0 + y
a2 = a1 + z
a3 = a2 + w

sum = (((x) + y) + z) + w
    = a0 + y + z + w
    = a1 + z + w
    = a2 + w

Therefore, one can add to a running sum one element at a time.
Last edited on
Thanks, @Ssturges it worked well, but how do I make it so that the user has to choose between the sum or average to be calculated?

Also, @keskiverto does that function work for as many values as the user inputs? Sorry but I'm new and a bit clueless, but doesn't that code just allow for 4 integers?
That was no code. It was just math to remind that you can do additions one at a time, just like the actual code by Ssturges does.


Lines 16 and 25 in that code read a number. Similarly, you could read a character. Then you can test whether the character is what you expect, and if yes, then act accordingly.
I'm having trouble trying to make the part where after the person enters his integers where he chooses to either have the Sum displayed or the Average displayed.
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
#include "stdafx.h"
#include <iostream>
#include <cmath>  
using namespace std;

double sum (double n[],int times);
double average (double n[] ,int times);

int main(){
   
   char choice;
   double n, i;
   int times;
   double inputtednumbers[100];

   do{
   cout << "Input any amount of numbers. Each number can not exceed 500,\nentering 0 will end the program: ";
   cout << "\n\nHow many numbers would you like to enter?";
   cin >> i;
   
   times = 0;
   
   if ( i == 0)
       return 0;

      do{
       cout << "Enter a number 1 - 500: ";
       cin >> n;
       inputtednumbers[times] = n;

         times++;
       }while( times < i);

      cout << endl << endl << "Table of Operations - Enter either A or B:" << endl;
cout << "A -- Addition" << endl;
cout << "B -- Average" << endl;

cin >> choice; 

switch(choice) // Switch function allows user to choose the calculation to execute
{
case 'A':
case 'a': sum(inputtednumbers, i);
	cout << " The Sum is: " << sum (inputtednumbers, i) << endl << endl;
break;
case 'B':
case 'b': average(inputtednumbers, i);
	cout << " The Average is: " << average (inputtednumbers, i) << endl << endl;
break;
}
   }while(i != 0);

   return 0;
}



double sum (double n[],int times){
   double sumTotal = 0;

   for(int x = 0; x < times; x++)
       sumTotal += n[x];

return sumTotal;
}

double average (double n[],int times){
   double total = sum(n,times);
   return total/times;
}


edit: nevermind, I got it to work.

However, I'm having trouble understanding the dynamic array. At the moment the array size is 100, but I want it to be the user inputted amount.
Last edited on
So Animus, I'm not sure if you have gone over pointers yet, but to create a proper dynamic array you will need to use them.

The syntax for a dynamic array would look similar to something like this:

1
2
3
4
5
6
7
8
int amtOfNums; //just an integer;
int  *sizeOfArr;  //an variable that holds the address location of an integer

cout << "What  would you like the size of your array to be?: "; // asking the user to set the
cin >> amtOfNums;                                                                     //size of the array

sizeOfArr = new int[amtOfNums]; // now you have allocated memory for your array 
                   


this is real rough explanation, but here is a link to better explanation
http://www.cplusplus.com/doc/tutorial/dynamic/
Topic archived. No new replies allowed.