Need help with my program please! Arrays, functions, etc

so far this is what i have.. i pretty much tried to do as much as i could on my own..and here are the instruction i have to follow when making the program

i have to make a program that
- uses an array to hold 5 numbers
- asks the user to enter 5 numbers which are read and assigned to an array
- then a menu of choices is displayed
( A. Display the square root of each number
B. Display the sum of all numbers
C. Display the average of all numbers

- then the user is asked to enter a choice of A,B,C
- depending on the choice, a math operation is performed on all the values in the array

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
  void showMenu();
void getChoice(char choice);
float doTheMath(choice,numbers);
void getValues(double numbers);



using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    double numbers[5];
    double result;
    char choice;
    double working;
    
    
    
    result = getValues();
    showMenu();
    getChoice(choice);
    working= doTheMath (choice,numbers);
    
    
    
    return 0;
}

void showMenu()
{
    
    cout << " A. Display the square root of each number.\n";
    cout << " B. Display the sum of all numbers\n";
    cout << " C. Display the average of all numbers\n";
    
}
void getChoice(char choice)
{
    char choice
  cout << "please make a selection, will it be choice A,B or C?\n";
    cin >> choice;
}
double doTheMath(char choice, double numbers)
{
    if (choice == 'A')
    {
         
           for ( int i; i < 5; i++)
        {
               cout << pow(i);
        }
    }
}
void getValues(double numbers)
{
    numbers[5];
    
    cout << "please enter five numbers\n";
    
    for ( int i; i < 5; i++)
    {
        cin >> numbers[5];
    }
    return numbers;
}
Last edited on
Honestly most of this is wrong.

I'll start by telling you what is wrong/why it's wrong so hopefully you can try to get an idea of how to fix it.

result = getValues();

This is wrong, because of two reasons.
1) getValues is a void function, it doesn't return anything. If you want to set a double to be equal to the return value of a function, that function should be double. The function could be a float, or another type of integer, but it can't be void.

2) Even if this function was of type double, it's not like return numbers would return the array, it would only return 1 double.

1
2
3
4
5
6
7
8
9
10
11
12
void getValues(double numbers)
{
    numbers[5];
    
    cout << "please enter five numbers\n";
    
    for ( int i; i < 5; i++)
    {
        cin >> numbers[5];
    }
    return numbers;
}

This also is wrong.
numbers[5]; means nothing by itself. Your function parameter has a double number passed. This is 1 number, not an array of numbers. Just because you put numbers[5]; doesn't make it so you're passing an array, that line doesn't mean anything and shouldn't even compile.

Also keep in mind, even if you had successfully passed the array, you still wouldn't be able to access numbers[5]. Here is the reason why.

When you declare double Numbers[5]; it creates
Numbers[0]
Numbers[1]
Numbers[2]
Numbers[3]
Numbers[4]

However, Numbers[5] doesn't exist.
1
2
3
4
for ( int i; i < 5; i++)
    {
        cin >> numbers[5];
    }


This code isn't doing what you think it's doing. If numbers was an array that was properly passed through the function, you would change your for loop to

1
2
3
4
for (int i=0; i<5; i++)
{
   cin >> numbers[i];
}


By doing cin >> numbers[5] you are just inputting 5 different values and overwriting them with the last value to the same array index rather than filling in the array. It would just accessing one element of the array.


1
2
3
4
5
6
7
8
9
10
11
double doTheMath(char choice, double numbers)
{
    if (choice == 'A')
    {
         
           for ( int i; i < 5; i++)
        {
               cout << pow(i);
        }
    }
}


Also, pow doesn't work like that.

If you're just finding the sqrt, make it simple. There's a reason there is a sqrt(double number) function. You just need to #include <math.h>

Also you're missing your #include <iostream> for the input and output.

For void getchoice(char choice), you need to realize that if you want that variable to save after you exit that function, you need to pass a reference instead of the variable char choice. This is easy to change, all you need to do is modify that function like so in the prototype and definition change the beginning to

double doTheMath(char &choice, double numbers)

By putting that &, the variable will be changed after you exit that function. Otherwise it wont.

Let me know what you don't understand about this.
I'd help you out by retyping up your code, but the issue is there's so many things wrong with it that I want to make sure you understand what's going on.
@pindrought okay so i have made some correction with the code.. i think what is confusing me the most in passing the array into the function and getting the user input into the array. so just to make things clear when one is passing by reference it is holding a value right? for example the choice variable is holding the char value...im a bit confused.
1
2
3
4
5
6
7

void getChoice(char &choice)
{
    char choice
  cout << "please make a selection, will it be choice A,B or C?\n";
    cin >> choice;
}




and also in this function when returning the value is just numbers or is it numbers [i]?
double getValues(int numbers[ ], int size)
1
2
3
4
5
6
7
8
9
10
11

{
    
    cout << "please enter five numbers\n";
    
    for ( int i; i < 5; i++)
    {
        cin >> numbers[i];
    }
    return numbers;
}

When you pass an array through, you don't need a reference. That being said, you don't need that function to return anything where you fill in the array.

It could be set up like so.

1
2
3
4
5
6
7
8
9
10
void getValues(double NumberArray[])
{
    
    cout << "Please enter five numbers\n";
    
    for ( int i = 0; i < 5; i++)
    {
        cin >> NumberArray[i];
    }
}



For explaining the reference thing.
Think of it like this.

Let's say I have a function where I pass in an integer, and this function will print out double the value of that integer.
1
2
3
4
5
void PrintDoubleInteger(int MyInteger)
{
    MyInteger *= 2; //Multiplies integer by 2
    cout << "Your integer doubled is:" << MyInteger << endl; //Print integer
}



So if my main was set up this way
1
2
3
4
5
6
int main()
{
     int NumberToPrint=5;
     PrintDoubleInteger( NumberToPrint );
     return 0;
}


Then this program would print out that the number is 10. However, after it prints that out, in the main function, NumberToPrint is still = to 5 since we didn't pass by reference. What we did was we passed the value. What we did would theoretically be the same as calling PrintDoubleInteger( 5 ); since that variable isn't changed.


However, if we were to approach it this way

1
2
3
4
5
6
7
8
9
10
11
12
void PrintAndDoubleInteger( int & NumberToDoubleAndPrint )
{
     NumberToDoubleAndPrint*=2;
     cout << "Your number doubled is" << NumberToDoubleAndPrint << endl;
}

int main()
{
      int MyNumber=5;
      PrintAndDoubleInteger( MyNumber );
     return 0;
}


This piece of code will print out double what the integer is, but it will also double that integer, so when we get back into our main function MyNumber will be = to 10 after that function is ran.


Make sense?
@pindrought oh okay i get it now..

so i made some changes to the code but it seems i just made it worse. there are too many errors..sorry for being annoying. im just trying to understand what is going on.
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


#include <cstdlib>
#include <iostream>
#include <cmath>

void showMenu();
void getChoice(char &choice);
double doTheMath(char &choice, double numbers);
void getValues(double numberArray[])



using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    
    double numberArray[];
    double result;
    char choice;
    double working;
    
    
    
    
    getValues();
    showMenu();
    getChoice(choice);
    working = doTheMath (choice,numbers);
    
    
    
    return 0;
}

void showMenu()
{
    
    cout << " A. Display the square root of each number.\n";
    cout << " B. Display the sum of all numbers\n";
    cout << " C. Display the average of all numbers\n";
    
}
void getChoice(char &choice)
{
    char choice;
    
  cout << "please make a selection, will it be choice A,B or C?\n";
    cin >> choice;
}

double doTheMath(char &choice, double numbers)
{
    double sum;
    
    if (choice == 'A')
    {
         
           for ( int i; i < 5; i++)
        {
               cout << sqrt (double numbers);
        }
           else if (choice == 'B')
           {
               sum +=
           }
    }
}

void getValues(double numberArray[])
{
    
    cout << "please enter five numbers\n";
    
    for ( int i = 0; i < 5; i++)
    {
        cin >> numberArray[i];
    }
    
}
Line 10
void getValues(double numberArray[])
Missing semicolon after function prototype.

Line 21
double numberArray[];
Declared an array, but there is no size for your array.
Change it to
double numberArray[5];


double sum;
This isn't causing an error, but change this to
double sum=0;

cout << sqrt (double numbers);
When I said sqrt(double numbers) I didn't mean you'd put the type, I was just saying it accepts a double variable.

Change this to
cout << sqrt (numbers[i]);



double doTheMath(char &choice, double numbers)

Make sure that in your prototype and in your function, you change your parameters to

double doTheMath(char &choice, double numbers[])

Remember, you're passing an array of doubles, you're not passing 1 double.

Line 68
 
sum +=


First problem, you're missing a semicolon.
Second problem, you're not adding anything you just have +=

You need to have += something. Not the word something, but i'm saying you can't have += with no number or variable after it.

I don't see the other errors if there are any at a first glance, make these changes and repost your code if you're still having problems.

Edit:
Line 62
for ( int i; i < 5; i++)

Should be
for ( int i=0; i < 5; i++)

You need to initialize your counter which in this case is int i

Last edited on
@pindrought alright so the only thing i have now im getting errors through out the if/else if statements.
i cant thank you enough for helping me this far...i appreciate it.

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

#include <cstdlib>
#include <iostream>
#include <cmath>

void showMenu();
void getChoice(char &choice);
double doTheMath(char &choice, double numbers[]);
void getValues(double numberArray[]);



using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    
    double numberArray[5];
    double result;
    char choice;
    double working;
    
    
    
    
    getValues();
    showMenu();
    getChoice(choice);
    working = doTheMath (choice,numberArray[]);
    
    
    
    return 0;
}

void showMenu()
{
    
    cout << " A. Display the square root of each number.\n";
    cout << " B. Display the sum of all numbers\n";
    cout << " C. Display the average of all numbers\n";
    
}
void getChoice(char &choice)
{
    char choice;
    
  cout << "please make a selection, will it be choice A,B or C?\n";
    cin >> choice;
}

double doTheMath(char &choice, double numberArray[])
{
    double sum=0;
    
    if (choice == 'A')
    {
         
           for ( int i; i < 5; i++)
           {
               cout << "here are the numbers square rooted\n";
               cout << sqrt (numberArray[i]);
           }
           else if (choice == 'B')
           {
               sum += numberArray[i];
               cout << "the numbers added together would be\n"
                    << sum << endl;
           }
           else if (choice == 'C')
           {
               cout << "here is the average of all numbers\n";
               sum = numberArray[i]/5;   
           }
    }
}

void getValues(double numberArray[])
{
    
    cout << "please enter five numbers\n";
    
    for ( int i = 0; i < 5; i++)
    {
        cin >> numberArray[i];
    }
    
}
Try this
Edit: made a fix

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
double doTheMath(char &choice, double numberArray[])
{
    double sum=0;
    
    if (choice == 'A')
    {
         
           for ( int i=0; i < 5; i++)
           {
               cout << "here are the numbers square rooted\n";
               cout << sqrt (numberArray[i]);
           }
    }
    else if (choice == 'B')
    {
		for (int i=0;i<5; i++)
		{
			sum += numberArray[i]; //For each item in array, add them to the sum
		}
        cout << "the numbers added together would be\n"
            << sum << endl;
    }
    else if (choice == 'C')
	{
		for (int i=0;i<5;i++)
		{
			sum+=numberArray[i]; //first find sum
		}
		sum /= 5; //Divide sum by 5 to get average
		cout << "here is the average of all numbers\n";
		cout << sum;
	}
}
Last edited on
@pindrought alright man i think i can manage trying to figuring out the rest. again thank you for your time and patience, i really appreciate it.
Topic archived. No new replies allowed.