Need help on C programming

This is a discussion on Need help on C programming within the C Programming forums, part of the General Programming Boards category; Hi guys, is there anyway to have a program where it ask for three numbers and you shows which which ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    Need help on C programming

    Hi guys, is there anyway to have a program where it ask for three numbers and you shows which which number is the largest and which is the smallest, e.g.

    Enter 1st number: 30
    Enter 2nd number: 10
    Enter 3rd number: 30
    Largest = 30
    Smallest = 10

    Much help appreciated.

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    I'd imagine anything is possible with a bit of effort.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
       // declare variables
       // show prompt(s)
       // receive and validate input
       // compare input and assign output
       // show output
    
       return 0;
    }
    Give it a try and post your code if you get stuck.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    I already have a working code, but it does not look aesthetic due to using too many if() and else if(), I am learning it by myself and right now I am reading on swap() which I think it can be used to determine the largest and smallest numbers, but I can't get it to work

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    628
    What are you swapping? Have a largest variable and a smallest variable, no swapping involved.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    I was reading another thread; Determine the largest and smallest of three integers, and there is a guy suggested using swap() so I look it up on the book, anyway because I used too many if() to determine which number is largest and which is smallest, I am asking if there is a way to determine the largest and smallest integer entered?

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    628
    //Enter one number , make it equal to largest and smallest variable;
    //Enter second number (if second number bigger than largest, then make largest=second number...(same for smallest) if second less than smallest...
    //Repeat for 3rd number

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    I found this code
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int n,big,sml,i,totalNumber;
    clrscr();
    printf("\n How many number you will enter : ");
    scanf("%d",&totalNumber);
    for (i=0;i<totalNumber;i++)
    {
    printf("\n Enter number %d : ",i+1); 
    scanf("%d",&n);
    if(i==0)
    {
    big=sml=n;
    }
    if(big<n) big=n;
    if(sml>n) sml=n;
    }
    printf("\nBiggest number of all : %d",big); 
    printf("\nSmallest number of all : %d",sml);
    getch();}
    

    but cant get it to work in my program



  8. #8
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    628
    What do you mean by "cant get it to work in my program"?

  9. #9
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    if you're getting compiler errors with that code, get rid of the non-standard parts #include<conio.h>, clrscr(), and getch(). Get rid of void main regardless.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Code:
    float three_numbers(float num1, float num2, float num3){
    	printf("Sum = %.0f\n", num1+num2+num3);
        printf("Average = %.2f\n", (num1+num2+num3)/3);
    	printf("Product = %.0f\n",num1*num2*num3);
    }
    void a2question2()
    {
    int n,big,sml,i;
    for (i=0;i<totalNumber;i++)
    {
    printf("Enter three whole number: \n",i+1);
    scanf("%d",&n);
    
    
    if(i==0)
    {
    big=sml=n;
    }
    if(big<n) big=n;
    if(sml>n) sml=n;
    }
    printf("\nBiggest number of all : %d",big); 
    printf("\nSmallest number of all : %d",sml);
    	                 
    }
    this is part of my program. Its printing something like this:
    Enter three whole number: 3
    Enter three whole number: 4
    Enter three whole number: 5
    is there anyway i can ask it to say,
    Enter 1st number: 3
    Enter 2nd number:4
    Enter 3rd number: 5
    and how can I transfer this 3 numbers into my three_numbers() ?

  11. #11
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    you can pass them in as parameters.

    Code:
    void a2question2(float *n1, float *n2, float *n3)
    {
       int i;
    
       for(i=1; i <= 3; ++i)
       {
           printf("Enter %d%s", i, i == 1 ? "st", i == 2 ? "nd" : "rd");
           scanf("%f", i == 1 ? n1 : i == 2 ? n2 : n3);
       }
    
       ... // other bits of code
    }
    Code:
    float three_numbers(float num1, float num2, float num3){
    
        printf("Sum = %.0f\n", num1+num2+num3);
        printf("Average = %.2f\n", (num1+num2+num3)/3);
        printf("Product = %.0f\n",num1*num2*num3);
    
        // should be returning a float
    }
    in main call it

    Code:
    float num1, num2, num3;
    
    a2question2(&num1, &num2, &num3);
    float_result = three_numbers(num1, num2, num3);
    // function returns a float, but what value are you returning?
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  12. #12
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Code:
    voida2question2(float*n1, float*n2, float*n3){
       int i;
    
       for(i=1; i <= 3; ++i)
       {
           printf("Enter %d%s", i, i == 1 ? "st", i == 2 ? "nd" : "rd");
           scanf("%f", i == 1 ? n1 : i == 2 ? n2 : n3);
       }
    
       ... // other bits of code
    
    }

    this isn't working for me, can you just show me the fundamentals of what you just did instead of pasting the code? I will try to continue from there

  13. #13
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    I declared three variables in main and passed them by reference to a2question2 so that they could be modified within that function via scanf; I used the ? operator like an if/else if/else or switch of the loop.

    Code:
    if(i == 1)
       scanf("%f", n1);
    else if(i == 2)
       scanf("%f", n2); 
    else
       scanf("%f", n3);
    What isn't working for you?
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-11-2012, 01:03 AM
  2. Replies: 4
    Last Post: 12-11-2011, 03:25 PM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 10:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21