Arrays and functions..please help!

so i have tried running my program and i keep getting errors.

this is what im trying to do with my 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
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
  
#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(numberArray[]);
    
    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 letter;
    
  cout << "please make a selection, will it be choice A,B or C?\n";
    cin >> letter;
}

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;
	}
}

void getValues(double numberArray[])
{
    
    cout << "please enter five numbers\n";
    
    for ( int i = 0; i < 5; i++)
    {
        cin >> numberArray[i];
    }
    
}
Save errors in a text file and show it on the forum (This possibility is realized in Builder). And it will be better that you use "case" in your program:
http://www.cplusplus.com/forum/beginner/5152/
http://www.cprogramming.com/tutorial/lesson5.html
This is a very well constructed program for a beginner. You have a good grasp of how to divide up problem and solve it. All of the mistakes are little things. I hope you continue to pursue programming.

Now for the errors.

On line 7 you declare getChoice() as taking a char but the definition on line 49 takes a reference to a char. Change line 7 to void getChoice(char &choice);

Line 28: When you pass an array to a function, you just pass the name, so it should be getvalues(numberArray);.

The same thing applies to line 34. Also on line 34, when you pass a variable by reference, you pass it with the name only. Finally, doTheMath doesn't return anything and there's no need for it to do so. So all together, line 34 should be doTheMath(choice,numberArray);.

You also need to change the declaration and definition of doTheMath to return void instead of double on lines 8 and 57.

At line 87, add a newline to the output: cout << sum << '\n';

getChoice() should put the choice into the "choice" parameter that you pass into the function. So that should be :
1
2
3
4
5
void getChoice(char &choice)
{
   cout << "please make a selection, will it be choice A,B or C?\n";
    cin >> choice;
}

Your code that prints the square roots needs to separate the printed values. Also you're printing "here are the numbers square rooted" inside the loop, so it prints 5 times instead of once. So the loop should be:
1
2
3
4
5
cout << "here are the numbers square rooted\n";
for ( int i=0; i < 5; i++)
{
   cout << sqrt (numberArray[i]) << '\n';
}


Here is your code with the changes I mentioned. It seems to work fine. Normally I'd make you do the edits yourself but you've demonstrated enough knowledge - doing the edits wouldn't teach you anything.
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
#include <cstdlib>
#include <iostream>
#include <cmath>

void showMenu();
void getChoice(char &choice);
void 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(numberArray);
    showMenu();
    getChoice(choice);
    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)
{
    cout << "please make a selection, will it be choice A,B or C?\n";
    cin >> choice;
}

void doTheMath(char &choice, double numberArray[])
{
    double sum=0;
    
    if (choice == 'A')
	{
         
	    cout << "here are the numbers square rooted\n";
	    for ( int i=0; i < 5; i++)
		{
		    cout << sqrt (numberArray[i]) << '\n';
		}
	}
    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;
	}
}

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


In exchange for this gift, I ask that you change doTheMath() to use a switch statement as Auroch has mentioned instead of if/then/else statements. I think you'll get it easily. But one note of caution! With a switch statement, the program flow will drop from one case right into the next (and the next) unless you put in a "break" statement.

Good luck, and keep up the good work. I'd hire you in a few years.
@dhayden so i took your advice and i changed the if/else statements to a switch statement.. the problem im having now is even when i have a break statement it is still going from one case to another here is the code. let me know if i should make another correction as well please and thank you.
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 <cstdlib>
#include <iostream>
#include <cmath>

void getValues(  double Array[], int size); // no calculation, user input
void showMenu (); // shows the menu
void getChoice( char input); // desired option from the menu
void doTheMath(double Array[],double size, char input);
void printArray( double array[], int size);


const int size = 5;
using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    double Array[size];
    char input;
    
    
    cout << "insert five numbers please"<< endl; 
    getValues(Array);
    
    showMenu();
    getChoice(input);
    
    doTheMath(Array,size,input);
    
    printArray(Array,size); //optional
    
    
    return 0;
}
void getValues( double Array[], int size)
{
    for( int i = 0; i < size; i++ )
    {
        cin >> Array[i];
    }
}
void printArray( double Array[], int size)
{
    for( int i = 0; i < size; i++ )
    {
        cout << Array[i]<< endl;
    }
}
void showMenu ()
{
    cout << "A. Display the square root of each number\n"
         << "B. Display the sum of all numbers\n"
         << "C. Display the average of all numbers\n";
}
void getChoice( char &input)
{
    cout << "which option would you like?"<< endl;
    cin >> input;
}
double doTheMath(double Array[],double size, char input)
{
    switch (toupper(input))
    {
        case 'A': 
            for( int i = 0; i < size; i++ )
    {
            cout << sqrt(Array[i])<< endl;
    }
        break;
        
        case 'B': for( int i = 0; i < size; i++ )
    {
            double sum;
            sum += Array[i];
            cout <<" the sum is: "<< sum << endl;
    }
        
    }
}


Are you sure that if case 'A' is being executed that 'B' is also being executed? It really shouldn't be since you have your breakpoint at the end of case 'A'.
Last edited on
@dhayden im confused now.. my code wont compile...
@pindrought well both shouldnt execute. i dont see why both will...and even with a break they are both being executed
I made the necessary changes to your code so it would execute.

First: Line 9: You need (char & choice) if it is going to be changing the variable being passed in remember?

Line 63: For some reason you changed doTheMath to be a double function in the definition, even though the prototype says that it is a void, and it indeed should be a void since it doesn't return anything. Prototype in line 10.

Line 26: getValues requires that you pass an array for the first parameter which will be filled, and for the second parameter you pass the size of the array. For some reason you removed the parameter where you were passing the size.

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

void getValues(  double Array[], int size); // no calculation, user input
void showMenu (); // shows the menu
void getChoice( char & input); // desired option from the menu
void doTheMath(double Array[],double size, char input);
void printArray( double array[], int size);


const int size = 5;
using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    double Array[size];
    char input;
    
    
    cout << "insert five numbers please"<< endl; 
    getValues(Array,size);
    
    showMenu();
    getChoice(input);
    
    doTheMath(Array,size,input);
    
    printArray(Array,size); //optional
    
    
    return 0;
}
void getValues( double Array[], int size)
{
    for( int i = 0; i < size; i++ )
    {
        cin >> Array[i];
    }
}
void printArray( double Array[], int size)
{
    for( int i = 0; i < size; i++ )
    {
        cout << Array[i]<< endl;
    }
}
void showMenu ()
{
    cout << "A. Display the square root of each number\n"
         << "B. Display the sum of all numbers\n"
         << "C. Display the average of all numbers\n";
}
void getChoice( char &input)
{
    cout << "which option would you like?"<< endl;
    cin >> input;
}
void doTheMath(double Array[],double size, char input)
{
    switch (toupper(input))
    {
        case 'A': 
		for( int i = 0; i < size; i++ )
		{
				cout << sqrt(Array[i])<< endl;
		}
		break;
        
        case 'B': 
		for( int i = 0; i < size; i++ )
		{
			double sum;
			sum += Array[i];
			cout <<" the sum is: "<< sum << endl;
		}
		break;
        
    }//End of switch
}//End of function 



Also added a break at the end of case 'B'
Last edited on
Don't forget to put back the code for case 'C' also.
thank you both for all the help!
Topic archived. No new replies allowed.