7

If I have an array, e.g.

10, 4, 7, 8

The value of the maximum element is

10

How can I find this value?


Here's my attempt:

int highNum = 0;
int m;
int list[4] = {10, 4, 7, 8};
 for (m = 0 ; m < size ; m++);
 {
 if (list[m] > highNum)
 highNum = list[m];
 cout << list[m];
 }
cout << highNum;

I am trying to write a simple loop to store a max value from an array, and I wrote this thinking it would work, but for some reason at the beginning of the for loop it stores the m variable as 4 and exits the loop.

cigien
60.9k11 gold badges85 silver badges122 bronze badges
asked Oct 10, 2011 at 23:55
3
  • 1
    what's size? Did you set it as 4? Also, your indentation is misleading, you do cout << list[m]; at every passage in the loop. Commented Oct 10, 2011 at 23:56
  • 1
    Try using braces for all if statements, even trivial ones. It makes maintainability easier, so that if you add an extra line in the future, you won't accidently add it outside the scope of the if statement. Commented Oct 10, 2011 at 23:59
  • en.cppreference.com/w/cpp/algorithm/max_element Commented Dec 28, 2023 at 13:43

5 Answers 5

30

Unless you're doing this for homework and have to write the loop, just use std::max_element, as in:

int list[4] = {10, 4, 7, 8};
std::cout << *std::max_element(list, list+4);

...or better, avoid hard-coding the length:

int list[] = {10, 4, 7, 8};
std::cout << *std::max_element(std::begin(list), std::end(list));
answered Oct 11, 2011 at 0:03
4

You have a semicolon after your for statement:

for (m = 0 ; m < size ; m++);
{

This should be:

for (m = 0 ; m < size ; m++)
{
answered Oct 10, 2011 at 23:58
4
int highNum = 0;
int m;
int list[4] = {10, 4, 7, 8};
 for (m = 0 ; m < size ; m++); // <-- semicolon?
 {
 if (list[m] > highNum)
 highNum = list[m];
 cout << list[m];
 }
cout << highNum;

Looking at your indentation, you may have missed a pair of { ... } for the if statement as well.

answered Oct 10, 2011 at 23:58
1

There is a ; right after the closing parentheses of your for loop: for (m = 0 ; m < size ; m++);

The statements inside the block (inside the curly braces) gets executed only after the loop do nothing for size number of times and that too only once.

You have also missed a pair of { ... } for the if statement as well.

Adam
1,0301 gold badge10 silver badges26 bronze badges
answered May 11, 2017 at 17:16
0

You put a superfluous at the end ; in :

for (m = 0 ; m < size ; m++);

Edit : Working code with some additional << endl;

int size = 4;
int highNum = 0;
int m;
int list[4] = {10, 4, 7, 8};
for (m = 0 ; m < size ; m++)
{
 if (list[m] > highNum)
 highNum = list[m];
 cout << list[m] << endl;
}
cout << highNum << endl;
answered Oct 11, 2011 at 0:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.