Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. To find biggest of n numbers ?

To find biggest of n numbers ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J jschell

    Tarun Jha wrote:

    num[i] > num[j];

    I suspect that you think that line is doing something that it is not. Put some curly braces around that line then print out the values before and after it.

    T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #3

    do you mean inside he inner for loop, to print value of i ?

    J 1 Reply Last reply
    0
    • T Tarun Jha

      i need to find the biggest number, but it doesn't print the biggest number.

      #include
      #include

      int main(){

      int num\[50\], i=0, j, count =0;
      //int k;
      
      printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n");
      
      do{
      
      	scanf("%d", &num\[i\]);
      	i++;
      	count++;
      
      }while(num\[i-1\]!=0);
      
      for(i=0; i<=count -1; i++){
      
          for(j=0; j<=count-1; j++){
      
              if(i != j)
                  num\[i\] > num\[j\];
              else
                  break;
          }
      
          printf("%d is the biggest number", num\[i\]);
      }
      
      //printf("%d is the biggest number", num\[j\]);
      
      return 0;
      

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      Most of that code is redundant, all you need is something like:

      int num = -1, largest = 0;

      do{
      printf("Enter an integer, 0 to exit:\n");

      scanf("%d", &num);
      if (num > largest)
          largest = num;
      

      }while(num != 0);
      printf("%d is the biggest number", largest);

      T 1 Reply Last reply
      0
      • L Lost User

        Most of that code is redundant, all you need is something like:

        int num = -1, largest = 0;

        do{
        printf("Enter an integer, 0 to exit:\n");

        scanf("%d", &num);
        if (num > largest)
            largest = num;
        

        }while(num != 0);
        printf("%d is the biggest number", largest);

        T Offline
        T Offline
        Tarun Jha
        wrote on last edited by
        #5

        I know that method but i need to know what is wrong with the one i am working with.

        J L 2 Replies Last reply
        0
        • T Tarun Jha

          I know that method but i need to know what is wrong with the one i am working with.

          J Offline
          J Offline
          jeron1
          wrote on last edited by
          #6

          Start with the hint that jschell gave, look at that line of code and see what it is doing if anything, or better yet step though each line of code with a debugger.

          "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

          1 Reply Last reply
          0
          • T Tarun Jha

            i need to find the biggest number, but it doesn't print the biggest number.

            #include
            #include

            int main(){

            int num\[50\], i=0, j, count =0;
            //int k;
            
            printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n");
            
            do{
            
            	scanf("%d", &num\[i\]);
            	i++;
            	count++;
            
            }while(num\[i-1\]!=0);
            
            for(i=0; i<=count -1; i++){
            
                for(j=0; j<=count-1; j++){
            
                    if(i != j)
                        num\[i\] > num\[j\];
                    else
                        break;
                }
            
                printf("%d is the biggest number", num\[i\]);
            }
            
            //printf("%d is the biggest number", num\[j\]);
            
            return 0;
            

            }

            L Offline
            L Offline
            leon de boer
            wrote on last edited by
            #7

            The problem is obvious

            if(i != j)
            num[i] > num[j];
            else
            break;

            Start with i=0 j = 0 what does the break do ... i==j doesn't it so it will call the break .. single step debug and watch :-)

            In vino veritas

            1 Reply Last reply
            0
            • T Tarun Jha

              I know that method but i need to know what is wrong with the one i am working with.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              Tarun Jha wrote:

              what is wrong with the one i am working with.

              Your second loop makes no sense as you are using two index varaibles when you only need one, and you never compare the values to see which is largest, or keep a note of the largest value you have found so far.

              T 1 Reply Last reply
              0
              • L Lost User

                Tarun Jha wrote:

                what is wrong with the one i am working with.

                Your second loop makes no sense as you are using two index varaibles when you only need one, and you never compare the values to see which is largest, or keep a note of the largest value you have found so far.

                T Offline
                T Offline
                Tarun Jha
                wrote on last edited by
                #9

                ohh! :omg: Thank you. but the code written below only works some time.. :sigh:

                #include
                #include

                int main(){

                int num\[50\], i=0, j, count =0;
                int k, b;
                
                printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n");
                
                do{
                
                	scanf("%d", &num\[i\]);
                	i++;
                	count++;
                
                }while(num\[i-1\]!=0);
                
                for(k=0; k<=count-2; k++){
                
                    for(j=0; j<=count-2; j++){
                
                        if(k != j){
                            num\[k\] > num\[j\];
                            b = k;
                        	//printf("%d", num\[k\]);
                        	continue;
                			}
                        else
                            break;
                    }
                    //printf("%d is the biggest number \\n", num\[k\]);
                }
                
                printf("%d is the biggest number", num\[b\]);
                
                return 0;
                

                }

                D L 2 Replies Last reply
                0
                • T Tarun Jha

                  ohh! :omg: Thank you. but the code written below only works some time.. :sigh:

                  #include
                  #include

                  int main(){

                  int num\[50\], i=0, j, count =0;
                  int k, b;
                  
                  printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n");
                  
                  do{
                  
                  	scanf("%d", &num\[i\]);
                  	i++;
                  	count++;
                  
                  }while(num\[i-1\]!=0);
                  
                  for(k=0; k<=count-2; k++){
                  
                      for(j=0; j<=count-2; j++){
                  
                          if(k != j){
                              num\[k\] > num\[j\];
                              b = k;
                          	//printf("%d", num\[k\]);
                          	continue;
                  			}
                          else
                              break;
                      }
                      //printf("%d is the biggest number \\n", num\[k\]);
                  }
                  
                  printf("%d is the biggest number", num\[b\]);
                  
                  return 0;
                  

                  }

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #10

                  Tarun Jha wrote:

                  ...only works some time

                  Which technically means it does not work. If your algorithm is dependent upon its input in order to produce the correct output, it is wrong, period. You should be able to do this entire exercise using pencil and paper before ever committing anything to code. Short of that and you are just guessing (hoping) at best.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                  1 Reply Last reply
                  0
                  • T Tarun Jha

                    ohh! :omg: Thank you. but the code written below only works some time.. :sigh:

                    #include
                    #include

                    int main(){

                    int num\[50\], i=0, j, count =0;
                    int k, b;
                    
                    printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n");
                    
                    do{
                    
                    	scanf("%d", &num\[i\]);
                    	i++;
                    	count++;
                    
                    }while(num\[i-1\]!=0);
                    
                    for(k=0; k<=count-2; k++){
                    
                        for(j=0; j<=count-2; j++){
                    
                            if(k != j){
                                num\[k\] > num\[j\];
                                b = k;
                            	//printf("%d", num\[k\]);
                            	continue;
                    			}
                            else
                                break;
                        }
                        //printf("%d is the biggest number \\n", num\[k\]);
                    }
                    
                    printf("%d is the biggest number", num\[b\]);
                    
                    return 0;
                    

                    }

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #11

                    Yes, because your code is still wrong, mainly because there is no logic to it. I gave you the answer in a previous comment, which should at least give you an idea of the logic involved in such a simple task.

                    1 Reply Last reply
                    0
                    • T Tarun Jha

                      do you mean inside he inner for loop, to print value of i ?

                      J Offline
                      J Offline
                      jschell
                      wrote on last edited by
                      #12

                      Print i,j and the array values of each before and after that line.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups