I would like to understand what is going on in this array within the for loop:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int max;
const int size = 10;
int a[size] = {7,12,2,1,26,35,17,13,6,21};
max=a[0];
for(int i=1;i<9;i++)
{
if( max < a[i])
{
max = a[i];
}
}
cout<<"Maximum number is: "<< max << endl;
return 0;
}
What is going on within the for loop statement? What is the purpose of the i?
Thanks!
Last edited on
Firstly, the loop is incorrect. Instead of
for(int i=1;i<9;i++)
shall be
for(int i=1;i<10;i++)
Secondly, open any book on C++ and read it!
I would prefer for(int i=1;i<size;i++)
When a constant is defined, it's sensible to make use of it.