Thread: C functions using loops

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    20

    C functions using loops

    can someone help me

    *Create a C program that will use the application of function in the program. The program will not accept 0 and above 20 as the number of inputs, the program will display "Invalid Input" when encounter these numbers. The program will ask again for a valid number of inputs.
    Sample I/O:
    Enter number of inputs: 3
    Num1: 1
    Num2: 2
    Num3: 4
    Sum is 7 and the product is 8
    Highest value is 4 and the lowest value is 1
    Repeat the process? [Press 1 for yes, other #s for exit]
    Note: You are asked to create a function prototypes and definitions for computing the sum, computing the product, finding the highest, and finding the lowest. The inputs should be done using a repetition statement

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Noobpoint View Post
    can someone help me
    We can help you if you help us, it's a give-take scenario.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    ok then.... i will help you if you need help

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Noobpoint View Post
    ok then.... i will help you if you need help
    Good! In this thread you can help me by posting your effort/code so far.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    
    int x , y, input;
    int sum(int x, int y);
    int prod(int x, int y);
    
    
    
    
    int main ()
    {
    int a, b;
    printf("Enter Number of inputs:\n");
    scanf("%d", &x);
    scanf("%d", &y);
    scanf("%d", &input);
    
    
    
    
    int add(int x,int y);
    int mult(int x,int y);
    
    
    printf("%d\n", add);
    printf("%d\n", mult);
    system("pause");
    return 0;
    }
    
    
    
    
    int sum(int x, int y)
    {
        return x + y;
    
    
    }
    
    
    
    
    
    
    int prod(int x, int y)
    {
        return x * y;
    }

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    And the problem/question is?
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    and here is for getting the largest and smallest number

    Code:
    #include <stdio.h>
    #include <conio.h>
    main()
    {
    int a[5],i,big,low;
    
    
    printf("Enter 5 Number\n\n");
    for(i=0;i<5;i++)
    scanf("%d",&a[i]);
    big=a[0];
    for(i=0;i<5;i++)
    {
    if(a[i]>big)
    big=a[i];
    else
    if(a[i]<low)
    low=a[i];
    }
    printf("\nLargest Number is=%d\nSmallest Number is=%d ",big,low);
    getch();
    }

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    the question is if the user inputs any number the program will ask for the sum, product, the largest and the smallest number

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Noobpoint View Post
    its there at my first post
    You don't ask anything, you just dubbed your exercise here. What is it that troubles you? Errors, Warnings, Crashes, Wrong results? If you can't tell me about your program what can I tell you?
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    my problem is how to arrange the codes.. its very confusing

  11. #11
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    Quote Originally Posted by Noobpoint View Post
    the question is if the user inputs any number the program will ask for the sum, product, the largest and the smallest number
    and here is my other problem.....

  12. #12
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Try to solve your first problem by studying this link, and then come back for the second one.
    Devoted my life to programming...

  13. #13
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    i almost got it but how can i insert the codes for getting the largest and smallest number?

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    
    int add();
    int multiply();
    int hv=0;
    int lv=1;
    int exit();
    
    
    main()
    {
    int a, b, c, d,e,f;
    system("color F0");
    f=0;
    e=1;
    d=1;
    
    
    printf("Enter number of inputs: ");
    scanf("%d", &a);
    
    
    if((a>19)||(a==0))
    {
    printf("\nINVALID INPUT..");
    system("cls");
    getch();
    return main();
    }
    
    
    for(b=0; b<a; b++)
    {
    printf("\nEnter a number: ");
    scanf("%d", &c);
    f=f+c;
    e=e*c;
    }
    
    
    printf("\nThe sum is %d and the product is %d", f, e);
    printf("\n\nWant to try again? [1] Yes [other Digits] No: ");
    scanf("%d", &d);
    
    
    if(d==1){system("cls");return main();}
    if(d!=1){system("cls");exit();}
    
    
    getch();
    
    
    }
    
    
    int exit()
    {
    return 0;
    }

  14. #14
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Your min-max code is correct with a tiny mistake: set both "big" and "low" to "a[0]" at the beginning of your search loop. ( From post #7 )
    Devoted my life to programming...

  15. #15
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Read as much as you can at the link I posted, it should be sufficient.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with functions and for loops
    By slava in forum C Programming
    Replies: 1
    Last Post: 11-08-2008, 01:30 PM
  2. Loops or recursive functions?
    By mariano_donati in forum C Programming
    Replies: 5
    Last Post: 05-12-2008, 12:41 PM
  3. Loops in functions?
    By AmbliKai in forum C Programming
    Replies: 16
    Last Post: 11-29-2007, 06:33 AM
  4. new to Void functions and loops
    By Loraine13 in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2001, 10:00 AM
  5. Loops OR Functions!! HELP WITH THIS CODE!!!!!!!!!!
    By WIshIwasGooD in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2001, 12:49 PM