Hello ! I have this code about finding the max from a vector , i do understand it , but in another code , i have to find the max in vector and his index , i really dont understand from where the index coming when it print me the result , can you explain me from where the Index come:
Here Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std;
int main(){
int N;
cout<<"N=";cin>>N;
int a[1000];
int i;
for(i=0;i<N;i++){
cout<<"nr=";cin>>a[i];
}
int max=0;
int indexMax=0;
for(i=1;i<N;i++)
if(a[i]>max){
max=a[i];
indexMax=i;
}
cout<<"Maxim= "<<max<<" index= "<<indexMax;
return 0;
}
Edit & Run
I cant understand from where the index come.
Sry my english
#include<iostream>
int main() {
using std::cout; using std::cin;
int N;
cout << "N=";
cin >> N;
if ( N < 1 ) N = 1;
constexprint M {1000};
if ( M < N ) N = M;
int a[M];
int i = 0;
while ( i=0; i<N; ++i ) {
cout << "nr=";
cin >> a[i];
}
int indexMax = 0;
for ( i=1; i<N; ++i ) {
if ( a[indexMax] < a[i] ) {
indexMax=i;
}
}
cout << "Maxim= " << a[indexMax] << " index= " << indexMax;
}
Was MikeyBoy's hint enough for you to understand both versions?