Max value question

(Question)Write a C++ program that declares an integer array of size 10, then uses three separate functions, one to do each of the following:

Ask user to enter 10 numbers for the array
Print out the array (all the array elements)
Find the maximum value of the array and return it to be printed in main()

(what I have tried)
#include <iostream>
using namespace std;

void getMax( int );

int main (){


int day[10];
cout << " Please enter 10 numbers: ";

for (int i=0; i<10; i++)
cin >> day[i];
max = getMax(day[i]);


return 0;
}
void getMax( int day[])
{
int max;
for (int i=0; i<10; i++)
max = day[i];

return;
}


First, please use code tags and sensible indentation when posting code. See: https://www.cplusplus.com/articles/jEywvCM9/

Your code (with some whitespace edits):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

void getMax( int );

int main (){
  int day[10];
  cout << " Please enter 10 numbers: ";
  for ( int i=0; i<10; i++ )
    cin >> day[i];

  max = getMax( day[i] );

  return 0;
}

void getMax( int day[] )
{
  int max;
  for ( int i=0; i<10; i++ )
    max = day[i];

  return;
}

There are some issues.

Line 4 and line 17 are two different functions. The first takes one int. The second takes array of int.

return it to be printed in main()

The getMax does not return any value.

The getMax (lines 17-24) assumes that the array 'day' has 10 elements.
You need a conditional in that loop too.

Line 12 attempts to use value of 'i'. There is no such variable. The 'i' in for loop exists only within the for loop, lines 9 and 10.

See the tutorials:
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/control/
http://www.cplusplus.com/doc/tutorial/arrays/
your_tutor wrote:
Write a C++ program that declares an integer array of size 10, then uses three separate functions, one to do each of the following:

Ask user to enter 10 numbers for the array
Print out the array (all the array elements)
Find the maximum value of the array and return it to be printed in main()


This should be the outline of the program you start with.
Copy/paste the imperative statements as comments into your program source.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Ask user to enter 10 numbers for the array
void inputArray(int arr[10]) {
}

// Print out the array (all the array elements)
void printArray(int arr[10]) {
}

// Find the maximum value of the array and return it to be printed in main()
int findMaxInArray(int arr[10]) {
  int max = 0;
  return max;
}

int main ( ) {
  //  declares an integer array of size 10
  int array[10] = { 0 };
}

It doesn't do anything, but at least it compiles and is a foundation on which to build.

The next step is fill out ONE of the functions. I would suggest printArray first, because having that working makes it so much easier to see if inputArray works in the next step.
1
2
3
4
5
int main ( ) {
  //  declares an integer array of size 10
  int array[10] = { 0 };
  printArray(array);
}

MAKE sure this
a) compiles
b) works
before rushing off to implement the next function.

The point of all this is to make your edit / compile / test cycles short enough that you're not totally confused by the outcome.

Topic archived. No new replies allowed.