Thread: C functions using loops

  1. #16
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    wew.. when i try to change
    Code:
     big=a[i]
    and
    Code:
    low=a[0];
    to
    Code:
    big=a[0];
    and
    Code:
    low=a[0];
    the result was

    the smallest number is = 1
    the largest number is = 1


    [code]

    if(a[i]>big)
    big=a[i];
    else
    if(a[i]<low)
    low=a[i];

    [/code

  2. #17
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I meant you should replace line #11 of post #7 with "big = low = a[0];"
    Devoted my life to programming...

  3. #18
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Fix:

    Code:
    #include<stdio.h>
    #include<conio.h> /* outdated/nonportable */
    #include<stdlib.h>
    
    
    
    
    int add();
    int multiply();
    int hv=0; /* useless global */
    int lv=1; /* useless global with random "magic number" value */
    int exit(); /* bad name for a function, already included in std. C */
    
    
    
    
    main() /* no implicit main, make it int main instead */
    {
    int a, b, c, d,e,f; /* not descriptive/useful names */
    system("color F0"); /* non portable */
    f=0; 
    e=1; /* magic numbers again? */
    d=1;
    
    
    
    
    printf("Enter number of inputs: ");
    scanf("%d", &a);
    
    
    
    
    if((a>19)||(a==0))
    {
    printf("\nINVALID INPUT..");
    system("cls"); 
    getch(); /* get rid of this, and anything else from conio.h */
    return main(); /* just exit. or, use a while loop */
    }
    
    /* example of the above code with a while loop */
    a = 0;
    while ((a > 19) && (a != 0)) {
        printf("Enter number of inputs (1-19): ");
        scanf("%d", &a); 
    }
    
    
    
    
    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() /* already a function */
    {
    return 0; /* completely useless, this does literally nothing */
    }


  4. #19
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    ohh i see... the only problem i have now is how to combine these codes..


    this is for finding the sum and prod

    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;
    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\nRepeat the Process? [Press [1] for yes, other #'s for exit]");
    scanf("%d", &d);
     
     
    if(d==1){system("cls");return main();}
    if(d!=1){system("cls");exit();}
     
     
    getch();
     
     
    }
     
     
    int exit()
    {
    return 0;
    }

    and this is for 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();
    }

  5. #20
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    plss help me... i have really a big problem.. i dont know how to arrange the code..

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