Help in C function with loop statement

This is a discussion on Help in C function with loop statement within the C Programming forums, part of the General Programming Boards category; .............

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

    Help in C function with loop statement

    ..........
    Last edited by [Student]; 02-15-2011 at 03:56 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First you need to learn to use code tags when posting...
    << !! Posting Code? Read this First !! >>

    Next.. you are using the variable a for your inputs, you should sit down with a pen and paper and follow the value of a through your code, step by step... your program doesn't do what you think it does...
    (Hint: How many values can a variable have?)

  3. #3
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    arrarys! learn live love em

    since it's just 5 numbers you were testing i kinda cheated xD. what i did was declare an array for 5 integers and made the do while loop execute 5 times. (x < 5). it starts at 0 so that you can access the first element of the array. (a[0]). then i passed the address of the first element of the array to a pointer in add and average. since we know it's 5 elements we could simply use a[0], a[1], ... a[4].

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    void add(int * a){
         int sum=0;
         printf("\nSum is : %d",sum=a[0]+a[1]+a[2]+a[3]+a[4]);
    }
    void average(int * a){
         double average;
         printf("\nAverage is : %d",(a[0]+a[1]+a[2]+a[3]+a[4])/5);
         }
    main(){
           printf("Enter 5 #s: ");
           int a[5];
           int x=0;
           do{
                   scanf("%d",&a[x]);
                   x++;
                   }
                   while(x<5);
                   add(&a[0]);
                   average(&a[0]);
                   int choice;
                   printf("\n\nRepeat the process?Press 1 for yes,other #s to exit: ");
                   scanf("%d",&choice);
                   if(choice==1){
                                 system("cls");
                                 return main();
                                 }
                                 else
                                 {
                                 return 0;
                                 }
                                 getch();
                                 }
    Last edited by codeprada; 02-15-2011 at 07:27 AM.
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  4. #4
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    tnx alot codeprada!.... ;D i'll study about this...what did you do to this code!....

  5. #5
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    one last question Mr. Codeprada.....how to change my average into float...so i can know the decimal values!....if i convert it'll show syntax error!...

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Code:
    do{
       scanf("%d",&a);
       x++;
    }
    while(x<=5);
    Here's the loop where I think you think everything happens. In this loop, you will change a with scanf() five times. But the problem is, nothing besides that happens. By the time you get to

    Code:
    add(a);
    
    /* pasted your implementation of it here */
    void add(int a){
         int sum=0;
         printf("\nSum is : %d",sum=a+a+a+a+a);
    }
    You add the last a in printf's argument five times. If a is 5 then that is the same as 5*5.

    So I hope your error makes sense. You should rewrite add() so that you can include it in the loop, after the scanf line. Mainly, add's job would be to accept a couple of arguments, add them, and return the result.

  7. #7
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    ahh...ok2x... tnx

  8. #8
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    How to convert it to float!...?....
    so i can know the decimal values!.....plz help!...

    Code:
    void average(int * a){
         double average;
         printf("\nAverage is : %d",(a[0]+a[1]+a[2]+a[3]+a[4])/5);
    }

  9. #9
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    and so i can know the average! with decimal places!....

  10. #10
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    never mind.....i got it!...tnx to all of you for help!...

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Quote Originally Posted by blanditz View Post
    How to convert it to float!...?....
    so i can know the decimal values!.....plz help!...

    Code:
    void average(int * a){
         double average;
         printf("\nAverage is : %d",(a[0]+a[1]+a[2]+a[3]+a[4])/5);
    }
    C supports two types of division. One is integer division, where both dividend and divisor are (or converted to) integer type. The other is floating point division, where one of, or both divisor and dividend are (or converted to) floating point type.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by codeprada View Post
    arrarys! learn live love em

    since it's just 5 numbers you were testing i kinda cheated xD. what i did was declare an array for 5 integers and made the do while loop execute 5 times. (x < 5). it starts at 0 so that you can access the first element of the array. (a[0]). then i passed the address of the first element of the array to a pointer in add and average. since we know it's 5 elements we could simply use a[0], a[1], ... a[4].
    Way to go codeprada...
    How much do you think our friend learned from you simply handing him the answer?

    You didn't help this person at all... In fact you delivered exactly the wrong message: "Don't worry there's always someone out there who's stupid enough to do it for you"...

    http://cboard.cprogramming.com/c-pro...-homework.html
    Last edited by CommonTater; 02-15-2011 at 08:53 AM.

  13. #13
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    Hey!.... don't fight plz!.......ok i'm sorry!....but don't worry i'll study it!....this is not a home work...I'm just practicing!.....and i have problem on that line....so i need help!....i didn't copy paste the code!...i just analyze the code what codeprada do to this problem!.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  4. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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