Thread: any alternatives??

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    2

    any alternatives??

    Hii guyz...im a new programmer and i was given a task to write a program which takes 3 variables as input from the user and prints them out in the descending order..ive made a program which is below, i wanna know if there can be any alternative to this program which uses LESS EXECUTION STEPS??? Please help me out guyz,..thax

    SOURCE CODE:

    #include<stdio.h>
    void main()
    {
    int num1,num2,num3,max,mid,min;
    printf("Enter first number:");
    scanf("%d",&num1);
    printf("\n\nEnter second number:");
    scanf("%d",&num2);
    printf("\n\nEnter third number:");
    scanf("%d",&num3);
    if(num1>num2 && num1>num3)
    {
    max=num1;
    if(num2>num3)
    {
    mid=num2;
    min=num3;
    }
    else
    {
    min=num2;
    mid=num3;
    }
    }



    else
    if(num2>num1 && num2>num3)

    {
    max=num2;
    if(num1>num3)
    {
    mid=num1;
    min=num3;
    }
    else
    {
    min=num1;
    mid=num3;
    }

    }

    else
    {
    max=num3;
    if(num1>num2)
    {
    mid=num1;
    min=num2;
    }
    else
    {
    min=num1;
    mid=num2;
    }

    }

    printf("\n\n%d\t%d\t%d\n\n", max,mid,min);


    }

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    7
    i think this program is better and faster executable

    #include<iostream>
    using namespace std;

    int main()
    {
    int a[3],i,j;
    for(i=0;i<3;i++)
    cin>>a[i];

    for(i=0;i<3;i++)
    {
    int pos=i;
    for(j=i+1;j<3;j++)
    if(a[j]>a[pos]) {
    int temp=a[pos];
    a[pos]=a[j];
    a[j]=temp;
    }
    }

    for(i=0;i<3;i++)
    cout<<a[i]<<" ";
    return 0;
    }

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Use code tags.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    I think there is a faster way to do this - see my notes on your tidied code (please learn to use code tags)
    Code:
    #include<stdio.h>
    
    int main()      /* -- note that main() must always return int */
    {
      int num1,num2,num3,max,mid,min;
    
      printf("Enter first number:");
      scanf("%d",&num1);
      printf("\n\nEnter second number:");
      scanf("%d",&num2);
      printf("\n\nEnter third number:");
      scanf("%d",&num3);
    
      if(num1>num2 && num1>num3)
      { 
        max=num1;
        if(num2>num3)  /*-- note that if this is true the num1 > num3 check was redundant */
        {
          mid=num2;  /* you could do away with these allocations steps and just printf() when you know the answer */
          min=num3;
        }
        else
        {
          min=num2;
          mid=num3;
        }
      }
      else
      {
        if(num2>num1 && num2>num3)    /* note that you already checked num1 > num2 */
        {
          max=num2;
          if(num1>num3)  /* -- if this is true then num2 > num3 check above is redundant */
          {
            mid=num1;
            min=num3;
          }
          else
          {
            min=num1;
            mid=num3;
          }
        }
        else
        {
          max=num3;
          if(num1>num2)   /* -- this is the third time you've done this comparison! */
          {
            mid=num1;
            min=num2;
          }
          else
          {
            min=num1;
            mid=num2;
          }
        }
      }
      
      printf("\n\n%d\t%d\t%d\n\n", max,mid,min);
    
      return 0;      /* -- return 0 if successful */
    }
    anil_beloved - you also need to learn to use code tags. Also, this is a C board, not C++. Also your code is more compact and extendable but it is not necessarily the fewest execution steps (I don't think) because your code always makes 3 comparisons (and that's not counting looping overhead). You could get away with 2.67 comparisons on average.
    DavT
    -----------------------------------------------

  5. #5
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    Try this out

    Code:
    #include <stdio.h>
    
    
    void swap(int *a, int *b)
    {
       int temp;
       temp=*a;
       *a=*b;
       *b=temp;
    }
    
    int main(void)
    {
       int num[3],i;
    
       printf("enter numbers \n");
    
       
       for (i=0;i<3;i++)
       scanf("%d  ",&num[i]);
    
       if(num[0]>num[1])
         swap(&num[0],&num[1]);
      if(num[1]>num[2])
         swap(&num[1],&num[2]);
      if(num[0]>num[1])
         swap(&num[0],&num[1]);
      
       for (i=0;i<3;i++)
       printf("%d    ",num[i]);
    
       return 0;
    }

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by anil_beloved
    i think this program is better and faster executable

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
            int a[3],i,j;
            for(i=0;i<3;i++)
                    cin>>a[i];
    
            for(i=0;i<3;i++)
            {
                    int pos=i;
                    for(j=i+1;j<3;j++)
                    if(a[j]>a[pos]) {
                                            int temp=a[pos];
                                            a[pos]=a[j];
                                            a[j]=temp;
                                    }
            }
    
            for(i=0;i<3;i++)
                    cout<<a[i]<<" ";
            return 0;
    }
    Doesn't seem much like a C solution to the problem, now does it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std::map Alternatives
    By EvilGuru in forum C++ Programming
    Replies: 8
    Last Post: 04-12-2008, 03:45 AM
  2. Alternatives for "if"
    By criticalerror in forum C++ Programming
    Replies: 7
    Last Post: 01-22-2004, 08:03 PM
  3. automake alternatives
    By bludstayne in forum Tech Board
    Replies: 1
    Last Post: 01-04-2004, 02:15 PM
  4. MFC alternatives?
    By Hubas in forum Windows Programming
    Replies: 6
    Last Post: 08-01-2002, 08:04 PM
  5. The 'delay()' function and its alternatives...
    By Nutshell in forum Game Programming
    Replies: 1
    Last Post: 04-27-2002, 01:45 AM