By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 424,842 Members | 2,198 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,842 IT Pros & Developers. It's quick & easy.

# How to find index of a maximum number in an array?

 P: 7 i need to find the max position of my program. how can i do that? Expand|Select|Wrap|Line Numbers int main()  {          int j,max=0,k;  for(k=0;k<=9;k++)          {          printf("Enter the number:");          scanf("%d", &j[k]);          }              for(k=0;k<=9;k++)      {          printf("x[%d]=%d\n", k, j[k]);          if(max
Share this Question
6 Replies

 Expert 100+ P: 1,123 You mean to say, you need to figure out the index of a maximum number in an array? Whats the big deal in it? When you calculate the maximum you assign a position variable to index of the loop. When you are done executing the loop you will also have the position in the array where maximum number is present. Expand|Select|Wrap|Line Numbers for(k=0;k<=9;k++)  {      if(max

 P: 23 Slightly different query but I am using index of variable from array,thats why I am posting it here. I have this code by which I have to trim the successive two or more white spaces in a string. For ex.If I enter "India(2WS)wins(2WS)Soccer(2WS)World(2WS)Cup!! " Here 2WS refers to 2 white spaces. then output should be "India wins Soccer World Cup!!". Here is the code I have been working on.I couldn't do it in C.Thats why using library functions. Every time I run this thing its giving me segmentation fault. Expand|Select|Wrap|Line Numbers #include  #include #include using namespace std;   int main() char a;     string s1;     int b;     cout<<"Enter desired string\n";     cin.getline(a,30);     s1=a;     int n=0,i;     for(i=0;i<30;i++)     {         if(a[i]==' '&& a[i+1]==' ')         {             n++;             b[i]=i;         }      }     cout<<"The white spaces in given string are "<

 100+ P: 542 Since you do not know the size of the string, you will need to establish that, with the standard library function sizeof() and use this as the for loop ending condition. There may be more problems in your code which I have not looked for. Jun 20 '10 #4

 Expert 100+ P: 196 This loop is guaranteed to run over the bounds of a: Expand|Select|Wrap|Line Numbers for(i=0;i<30;i++)      {          //when i = 29, (i+1) = 30!         if(a[i]==' '&& a[i+1]==' ')          {              n++;              b[i]=i;          }       }  Also, I belive you should to use Expand|Select|Wrap|Line Numbers b[n]=i; n++; instead of Expand|Select|Wrap|Line Numbers n++; b[i]=i; using 'i' as your index means you are pretty much guaranteed to overrun the bounds of b as well. I dont' know if these would cause your segmentation fault, but they certainly won't help. Jun 21 '10 #5

 Expert 100+ P: 2,396 What if the array contains only negative numbers? The initial value of max should be j, not 0. Likewise, the initial value of iMaxPosition should be 0. Jun 21 '10 #6

 P: 8 @ashiela #include #include void main() { int i=0,j,k,l,m; char a; clrscr(); for(k=0;k<4;k++) { printf("enter value for sorting"); scanf("%s",&a[k]); } for(i=0;i<4;i++) { for(j=i+1;j<4;j++) { if(a[i]>a[j]) { l=a[i]; a[i]=a[j]; a[j]=l; } } } for(m=0;m<4;m++) { printf("\n%c",a[m]); } getch(); } Jun 23 '10 #7

### Post your reply

Sign in to post your reply or Sign up for a free account. 