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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashiela
    New Member
    • Apr 2010
    • 7

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

    i need to find the max position of my program. how can i do that?
    Code:
    int main() 
    { 
    		int j[10],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<j[k]) 
    		{ 
    			max=j[k]; 
    		} 
    	} 
    		printf("The maximum number is %d\n",max); 
    }
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    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.

    Code:
    for(k=0;k<=9;k++) 
    { 
        if(max<j[k]) 
        {  
           max=j[k]; 
           iMaxPosition = k;
        } 
    }
    Regards
    Dheeraj Joshi

    Comment

    • abhinuke
      New Member
      • May 2010
      • 23

      #3
      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.

      Code:
      #include <stdlib.h>
      #include<iostream>
      #include<string>
      using namespace std;
      
      int main()
      char a[30];
          string s1;
          int b[5];
          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 "<<n;
          cout<<"\n";
          i=0;
      while(i<n)
          {
              s1.erase(b[i],1);
              i++;
          }
      
      
         cout<<"\nThe new formatted string is \n";
         cout<<s1;
         return 0;
      }
      Any suggestions,hin ts?
      Thanks in advance.
      Cheers.

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        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.

        Comment

        • Joseph Martell
          Recognized Expert New Member
          • Jan 2010
          • 198

          #5
          This loop is guaranteed to run over the bounds of a:


          Code:
          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

          Code:
          b[n]=i;
          n++;
          instead of

          Code:
          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.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            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.

            Comment

            • santechselva
              New Member
              • May 2010
              • 8

              #7
              Originally posted by ashiela
              i need to find the max position of my program. how can i do that?
              Code:
              int main() 
              { 
              		int j[10],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<j[k]) 
              		{ 
              			max=j[k]; 
              		} 
              	} 
              		printf("The maximum number is %d\n",max); 
              }
              #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();
              }

              Comment

              Working...