Greatest value in an array?

I'm not sure how to word this but how exactly do I find the greatest value in an array. I have some idea but help is needed.
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

#include <iostream>

using namespace std;

int main() {
    char response;
    int x = 0;
    int people[x];
    int greatest;
    while(true) {
        cout << "Enter in the amount of years 10 people have ";
        cin >> people[x];
        x++;
        
        if (x == 10) {
            for (int i = 0; i < x; i++) {
            cout << people[i] << endl;
            if (i < x) {
                greatest = i;
            }
            else {
                continue;
            }
          }
        }
        else
            continue;
        
        cout << "The oldest person is " << greatest << endl;
        
        cout << "Do you want to continue?" << endl << "Y for yes other wise anything. ";
        cin >> response;
        if (response == 'Y') { 
            x = 0;
            continue;
        }
        else
            break;
    }
}
Is there any other way?
1
2
3
if (i < x) {
       greatest = i;
}


this is wrong
imo
Yeah It is lol.
1-> enter a input and find out the gt value.
or
2-> enter all values and find out gt value?????

1st-> need to understand what is needed.
2nd-> is there any specific requirement.

because one program can be done many way.
@OP: so is your problem settled?
closed account (E0p9LyTq)
If you want to do the code yourself here is an example method for checking for the greatest value in an array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
   int numbers[] = { 12, 2, 34, 85, 4, 91, 29, 85, 98, 33 };
   int greatest = numbers[0];

   std::cout << "Current greatest number is " << greatest << std::endl;

   for (int i = 1; i < 10; i++)
   {
      if (greatest < numbers[i])
      {
         greatest = numbers[i];
      }
   }

   std::cout << "New greatest number is " << greatest << std::endl;

   return 0;
}


Using the C++ algorithm, as suggested by Zhuge, is a much better and safer thing to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <algorithm>

int main()
{
   int numbers[] = { 12, 2, 34, 85, 4, 91, 29, 85, 98, 33 };
   int greatest = numbers[0];

   std::cout << "Current greatest number is " << greatest << std::endl;

   greatest = *std::max_element(numbers, numbers + 10);

   std::cout << "New greatest number is " << greatest << std::endl;

   return 0;
}
Got it!

#include <iostream>

using namespace std;

int main() {
char response;
int x = 0;
int people[x];
bool answer = true;
while(answer == true) {
cout << "Enter in the amount of years 10 people have ";
cin >> people[x];
x++;
int greatest = people[x];
if (x == 10) {
for (int i = 0; i < x; i++) {
cout << people[i] << endl;
if (greatest < people[i]) {
greatest = people[i];
}
else {
continue;
}
}
}
else
continue;

cout << "The oldest person is " << greatest << "years old" << endl;

cout << "Do you want to continue?" << endl << "Y for yes other wise anything. ";
cin >> response;
if (response == 'Y') {
x = 0;
continue;
}
else
answer = false;
}
}
closed account (E0p9LyTq)
1
2
int x = 0;
int people[x];


That is a coding error awaiting a crash. You are creating an array of ZERO elements. Any indexed access is going outside the bounds of the array.

The program may compile under TDM-GCC 4.9.2, but it is crashes.

Visual C++ 2015 has compile errors. You have to create an array with a constant value, and x is not a constant.

You already know the number of elements your array needs (10), so either "hard-code" the array instantiation int people[10]; or create a constant variable:

1
2
const int num_people = 10;
int people[num_people];


You are also initializing your greatest variable to the second element, not the first.

A recap of the changes needed to make this work the way you expect:

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

int main()
{
   char response;
   int x = 0;
   const int num_people = 10;
   int people[num_people];
   bool answer = true;

   while (answer == true)
   {
      int greatest = people[0];

      std::cout << "Enter in the amount of years 10 people have ";
      std::cin >> people[x];
      x++;

      if (x == 10)
      {
         for (int i = 0; i < x; i++)
         {
            std::cout << people[i] << std::endl;
            if (greatest < people[i])
            {
               greatest = people[i];
            }
            else
            {
               continue;
            }
         }
      }
      else
      {
         continue;
      }

      std::cout << "The oldest person is " << greatest << " years old" << std::endl;

      std::cout << "Do you want to continue?" << std::endl << "Y for yes other wise anything. ";
      std::cin >> response;
      if (response == 'Y')
      {
         x = 0;
         continue;
      }
      else
      {
         answer = false;
      }
   }
}


You could do a simple loop to get the array contents, since you already know the number of elements:

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

int main()
{
   char response;
   const int num_people = 10;
   int people[num_people];
   bool answer = true;

   while (answer == true)
   {
      int greatest = people[0];

      for (int x = 0; x < num_people; x++)
      {
         std::cout << "Enter age #" << x + 1 << " ";
         std::cin >> people[x];
      }

      for (int i = 0; i < num_people; i++)
      {
         std::cout << people[i] << std::endl;
         if (greatest < people[i])
         {
            greatest = people[i];
         }
         else
         {
            continue;
         }
      }

      std::cout << "The oldest person is " << greatest << " years old" << std::endl;

      std::cout << "Do you want to continue?" << std::endl << "Y for yes other wise anything. ";
      std::cin >> response;
      if (response == 'Y')
      {
         continue;
      }
      else
      {
         answer = false;
      }
   }
}
Last edited on
Wow thanks man, I was using visual studio 2015 as well and it never gave me compiler errors? But still I understand thanks for teaching me i'll be sure to fix it.
Topic archived. No new replies allowed.