i have a program in c that finds the first and second max in an array, but i want to get the indexes of these elements. Here is my code:
#include <stdio.h>
int main(){
int max1,max2,n;
scanf("%d",&n);
int a[n],i;
int i_m1,i_m2;
i_m1 = i_m2=0;
for(i = 0;i < n;i++){
scanf("%d",&a[i]);
}
max1 = max2 = 0;
for(i = 0;i < n;i++){
if(a[i]>max1){
max1=a[i];
//i_m1++;
} else if(a[i]>max2 && a[i]<max1){
max2=a[i];
//i_m2++;
}
}
printf("%d %d\n",max1,max2);
//printf("%d %d\n",i_m1,i_m2);
for(i = 0;i < n;i++){
printf("%d ",a[i]);
}
return 0;
}
I first enter the number of elements in the array then the array.
For example if i have an array
number of elements: 5
elements in array:
3 4 2 5 1
indexes:
0 1 2 3 4
max1=5, max2=4
i_m1=3, i_m2=1
How can i get the indexes i_m1 and i_m2 ?
i_m1++;
, you want to doi_m1 = i;