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
Bytes IT Community
+ 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
  1. int main() 
  2.         int j[10],max=0,k; 
  3. for(k=0;k<=9;k++) 
  4.         { 
  5.         printf("Enter the number:"); 
  6.         scanf("%d", &j[k]); 
  7.         }     
  8.         for(k=0;k<=9;k++) 
  9.     { 
  10.         printf("x[%d]=%d\n", k, j[k]); 
  11.         if(max<j[k]) 
  12.         { 
  13.             max=j[k]; 
  14.         } 
  15.     } 
  16.         printf("The maximum number is %d\n",max); 
  17. }
  18.  
May 8 '10 #1
Share this Question
Share on Google+
6 Replies


Dheeraj Joshi
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
  1. for(k=0;k<=9;k++) 
  2.     if(max<j[k]) 
  3.     {  
  4.        max=j[k]; 
  5.        iMaxPosition = k;
  6.     } 
  7.  
Regards
Dheeraj Joshi
May 8 '10 #2

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
  1. #include <stdlib.h>
  2. #include<iostream>
  3. #include<string>
  4. using namespace std;
  5.  
  6. int main()
  7. char a[30];
  8.     string s1;
  9.     int b[5];
  10.     cout<<"Enter desired string\n";
  11.     cin.getline(a,30);
  12.     s1=a;
  13.     int n=0,i;
  14.     for(i=0;i<30;i++)
  15.     {
  16.         if(a[i]==' '&& a[i+1]==' ')
  17.         {
  18.             n++;
  19.             b[i]=i;
  20.         }
  21.  
  22.    }
  23.     cout<<"The white spaces in given string are "<<n;
  24.     cout<<"\n";
  25.     i=0;
  26. while(i<n)
  27.     {
  28.         s1.erase(b[i],1);
  29.         i++;
  30.     }
  31.  
  32.  
  33.    cout<<"\nThe new formatted string is \n";
  34.    cout<<s1;
  35.    return 0;
  36. }
  37.  
Any suggestions,hints?
Thanks in advance.
Cheers.
Jun 19 '10 #3

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

Joseph Martell
Expert 100+
P: 196
This loop is guaranteed to run over the bounds of a:


Expand|Select|Wrap|Line Numbers
  1. for(i=0;i<30;i++) 
  2.     { 
  3.         //when i = 29, (i+1) = 30!
  4.         if(a[i]==' '&& a[i+1]==' ') 
  5.         { 
  6.             n++; 
  7.             b[i]=i; 
  8.         } 
  9.  
  10.    } 
Also, I belive you should to use

Expand|Select|Wrap|Line Numbers
  1. b[n]=i;
  2. n++;
instead of

Expand|Select|Wrap|Line Numbers
  1. n++;
  2. 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[0], not 0. Likewise, the initial value of iMaxPosition should be 0.
Jun 21 '10 #6

P: 8
@ashiela
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,j,k,l,m;
char a[20];
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.