Thread: Help me again in C function.....

  1. #1
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59

    Help me again in C function.....

    Grrr!.....for me this is not a practice....because its hard to get it!........
    so help me for my practice!....

    my problem is i want to know how to get the lowest input of integers input by user with loop!....i get the highest integer...but in the lowest is the same with highest!.....
    like a enter number 1 2 3 4 5

    the highest is 5[correct]
    the lowest is 5[wrong] ... plz help how to solve it

    2nd problem is i want to know how to reverse that input of integers input by user with loop!..just like 1 2 3 4 5....and its same value like int a using loop!....scanf is the inside of the loop....looping 5 times...and i want to know how to reverse...that one value!....

    plz help me in my two problems!....(don't use array) because in that topic i can't understand...i'm the C functions topic

    here's my code!>..

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    #include<stdlib.h>
    
    int add(int sum1);
    int pro(int pro1);
    int highest(int a);
    int lowest(int a);
    double sq(int sum1);
    double sq2(int pro1);
    int rev(int a);
    
    main(){
      int a;
      int sum1=0;
      int pro1=1;
      printf("Enter 5 numbers: ");
      for(int b=1;b<=5;b++){
                 scanf("%d",&a);
                 sum1+=a;
                 pro1*=a;
                 }
               printf("\n[1] Sum and Product");
               printf("\n[2] Highest and Lowest input");
               printf("\n[3] Square Root of the Sum and Product");
               printf("\n[4] Reverse Order of the Inputs");
               printf("\n[5] Exit");
               
               int choice;
               printf("\n\nEnter Choice: ");
               scanf("%d",&choice);
               system("cls");
                             if(choice==1){                                                
                             printf("\nSum is %d",add(sum1));
                             printf("\nProduct is %d",pro(pro1));
                             char repeat;
                             printf("\n\nGo Back to Main?[y/n] ");
                             scanf("%s",&repeat);
                             if(repeat=='y'){
                                             system("cls");
                                             return main();
                                             }
                                             else
                                             {
                                                  return 0;
                                                  }
                                                  }
                             else 
                             if(choice==2){
                             printf("\nHighest Input is %d",highest(a));
                             printf("\nLowest Input is %d",lowest(a));                         
                             char repeat;
                             printf("\n\nGo Back to Main?[y/n] ");
                             scanf("%s",&repeat);
                             if(repeat=='y'){
                                             system("cls");
                                             return main();
                                             }
                                             else if(repeat=='n'){
                                                  return 0;
                                                  }
                                                  }
                             else
                             if(choice==3){                                                  
                             printf("\nSquare Root Value of the Sum is %.2f",sq(sum1));
                             printf("\nSquare Root Value of the Product is %.2f",sq2(pro1));
                             char repeat;
                             printf("\n\nGo Back to Main?[y/n] ");
                             scanf("%s",&repeat);
                             if(repeat=='y'){
                                             system("cls");
                                             return main();
                                             }
                                             else
                                             {
                                                  return 0;
                                                  }
                                                  }
                             if(choice==4){
                             printf("\nReverse Order is ");
                             printf("\n\n%d ",rev(a));                                          
                             char repeat;
                             printf("\n\nGo Back to Main?[y/n] ");
                             scanf("%s",&repeat);
                             if(repeat=='y'){
                                             system("cls");
                                             return main();
                                             }
                                             else if(repeat=='n'){
                                                  return 0;
                                                  }
                                                  }
                                                  else
                             if(choice==5);
                             return 0;
                                                  getch();
                                                  }                    
    int add(int sum1)
    {
         int add=0;
         add=sum1;
         return add=sum1;
    }
    
    int pro(int pro1)
    {
         int pro2=1;
         pro2=pro1;
         return pro2=pro1;    
    }
    
    int highest(int a)
    {
        int high=0;
            if(high < a)
             {
             high=a;
             } 
    return high;                             
    }                 
    int lowest(int a)
    {
        int low='a';
        if(low > a)
        {
                   low=a;
                   }
    return low;
                   }    
    double sq(int sum1){
        double two;
        two=sqrt(sum1);
        return two;
    }
    double sq2(int pro1){
           double one;
           one=sqrt(pro1);
           return one;
    }
    int rev(int a){
        int m=a;
        int b,c=0;
        b=a%10; 
        c=(c*10)+b; 
        a=a/10;
        return c;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    OK... what's wrong with this picture...
    Code:
        if(low > a)
        { low=a; }

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You will need to keep track of both the highest and lowest number when you input your numbers. Right now, 'a' contains the last number.

    Code:
      printf("Enter 5 numbers: ");
      for(int b=1;b<=5;b++){
                 scanf("%d",&a);
                 sum1+=a;
                 pro1*=a;
      }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    First things first, it's int main(void) and a return 0 at the end.

    Your highest and lowest functions don't work because you only pass them one of the 5 values entered, namely the last of those. They will only print out either the default value (0 or 'a') or the number you pass them (last number entered, 5 in your case). Try it with the input 5 4 3 2 1, or 1 5 2 4 3 to see what happens.

    This is because you try to store all 5 inputs in one variable, namely a. Arrays would be the normal way, but we can't use those, so you need to read them into 5 different inputs, say a, b, c, d and e respectively so that you know all 5 values when it comes time to do something with them. Then, you pass all 5 of those to the highest and lowest functions:
    Code:
    int a, b, c, d, e;
      int sum1=0;
      int pro1=1;
      printf("Enter 5 numbers: ");
      scanf("%d",&a);
      scanf("%d",&b);
      scanf("%d",&c);
      scanf("%d",&d);
      scanf("%d",&e);
    ...
                             printf("\nHighest Input is %d",highest(a, b, c, d, e));
                             printf("\nLowest Input is %d",lowest(a, b, c, d, e));
    You can pass those 5 variables to add, pro and reverse functions too. Hopefully that gets you going in the right direction.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    To just get the highest and lowest number you can do like this:

    Code:
            int highest = INT_MIN;
            int lowest = INT_MAX;
            int b, a = 0;
    
            printf("Enter 5 numbers: ");
    
            for(b = 0;b < 5;b++){
                    scanf("%d",&a);
    
                    if( a > highest )
                            highest = a;
    
                    if( a < lowest )
                            lowest = a;
            }
    
            printf("lowest: %d\nhighest: %d\n", lowest, highest);
    If you need all numbers later, just make 'a' an array, and access it through a[b] inside your loop.

  6. #6
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    Hey!...thanks a lot to all of you i will test all your suggestions!....

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Keep in mind that INT_MAX and INT_MIN are defined in limits.h, so you will need to include that if you plan to use them. (Forgot to include it in my example).

  8. #8
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    what's INT_MIN declared?
    int highest = INT_MIN;
    int lowest = INT_MAX;

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    limits.h


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    Hey Sir!.... how do i reverse the 5 inputs?...plz help me!

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read them in one way, print them out the other way. Give it a shot, and we'll give you a hand after you've tried it.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    then how!.....is it possible?

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. It is one of the great scientific mysteries of our time: How to reverse a series of numbers. We have dedicated supercomputers working 24-7 trying to solve this complex issue.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    hahahaha....... what about in palindrome?......it's very impossible!..... ;D

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Palindromes are by definition already reversed, so why would you need to ever reverse a palindrome?

    We aren't here to do your work for you. Put it into words, try it out on your own, then post your attempt, what it does, and where you are stuck. In short, you need to know How To Ask Questions The Smart Way.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM