classISover classISover - 2 months ago 4
C++ Question

Finding max value in an array

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 find 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. Can someone help me out?

Answer

You have a semicolon after your for statement:

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

This should be:

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