What Index is in Vectors C++

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;
}




I cant understand from where the index come.
Sry my english
Last edited on
Where does the Maxim come from then?

You set both at lines 16-17.

Btw, you line 12 has logical error. If all values in the array are negative, then the result is wrong.


There is an alternative to your code does not have "max" separately:
1
2
3
4
5
6
7
int indexMax=0;
for ( i=1; i<N; i++ ){
  if ( a[indexMax] < a[i] ){
    indexMax = i;
  }
}
cout << "Maxim= " << a[indexMax] << " index= " << indexMax;



Edit:
The standard library has max_element
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <algorithm>

int main(){
  int a[1000] { 1, 2, 5, 4, 7 };
  auto top = std::max_element( a, a+4 );
  std::cout << "Maxim= " << *top << " index= " << top - a;
  return 0;
}
Last edited on
Bro , I still dont understand what index is , your code for index is same for me , I still dont understand from where that value is ...after "index= "
What index meen as theory ?

Sorry for I disturb you !
An index is a number that we use to access elements in an array and vector.

The first element is stored at index 0,
the second element is stored at index 1,
the third element is stored at index 2,
and so on ...
Last edited on
Thank you soo much both , specially you mr. Peter , Now I know what a Index is , but can you run my Code and tell me from where that INDEX come .... For exemple:

I run my code:
1
2
3
4
5
6
7
8
9
N=4
nr=20
nr=532
nr=32
nr=6
// So Results are :
Maxim=532   index=1

// I dont understand From where that Index come? Why index is 1 ? 
Last edited on
You do have four elements in your array:
element index value
first     0     20
second    1    532
third     2     32
fourth    3      6

The index of the largest value is clearly 1.

Your loop iterates values 1..3
1
2
3
4
5
for(i=1;i<N;i++)
  if(a[i]>max){
    max=a[i];
    indexMax=i;
  }

We could unwind the loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  i = 1;
  if(a[i]>max){
    max=a[i];
    indexMax=i;
  }

  i = 2;
  if(a[i]>max){
    max=a[i];
    indexMax=i;
  }

  i = 3;
  if(a[i]>max){
    max=a[i];
    indexMax=i;
  }

And substitute the i with hard values:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  if(a[1]>max){
    max=a[1];
    indexMax=1;
  }

  if(a[2]>max){
    max=a[2];
    indexMax=2;
  }

  if(a[3]>max){
    max=a[3];
    indexMax=3;
  }

The a[1] is 532 and thus larger than the max (0) at that point. Therefore:
1
2
    max=532;
    indexMax=1;

The two later conditions are false.
THANK YOUU , I UNDERSTAND , THANK YOU SOO MUCH ,

Here shorter explication in case someone else will read this in future :
1
2
3
4
5
element index value
first     0     20
second    1    532
third     2     32
fourth    3      6
Topic archived. No new replies allowed.