Table Help

This is a discussion on Table Help within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main(void) { int i, sum = 0; for(i = 10; i <= 33; i++) { sum ...

  1. #1
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72

    Table Help

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
        int i,
            sum = 0;
    
        for(i = 10; i <= 33; i++)
        {
            sum += i;
                printf("%3d", i);
        }
    
    }
    trying to get:
    10 11 12 13 14 15
    16 17 18 19 20 21
    ......
    ....................... 33

    I dont know how to make new line when it reaches 15.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by BB89 View Post
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
        int i, j, sum;
        sum = 0;
    
        for(i = 10, j = 0; i <= 33; i++, j++;)
        {
            sum += i;
            printf("%3d", i);
            if(j == 5) {
              printf("\n");
              j = -1;
            }
        }
        return 0;
    }


    trying to get:
    10 11 12 13 14 15
    16 17 18 19 20 21
    ......
    ....................... 33

    I dont know how to make new line when it reaches 15.
    Something like that.

  3. #3
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    I have got it to print:

    10 11 12 13 14 15 16...........33

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i,
            k = 0,
            sum = 0;
    
        for(i = 10; i <= 33; i++)
        {
            sum += i;
                printf("%3d", i);
    
        if( k == 5 )
            {
                printf("\n");
            }
    
        }
    
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Did you try using the code I posted for you?

    Your code is not the same, and of course, it won't do what you want.

    I'm not enjoying your "let's act MR", bit.

  5. #5
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    I'm not enjoying your "let's act MR", bit. ..... <-- what this?

    Yes, I tried your code and all I got was errors.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by BB89 View Post
    I'm not enjoying your "let's act MR", bit. ..... <-- what this?

    Yes, I tried your code and all I got was errors.
    Then you need to improve your copy and paste skills. Highlight the code (all of it), right-click, choose "Copy", go to your IDE and open up a new file, click "Paste".

  7. #7
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    My Copy Paste skills are fine.

    This is what I get when I paste his code

    Code:
    C:\Users\Brent\Desktop\Untitled1.c||In function `main':|
    C:\Users\Brent\Desktop\Untitled1.c|9|error: syntax error before ';' token|
    C:\Users\Brent\Desktop\Untitled1.c|18|error: syntax error before "return"|
    ||=== Build finished: 2 errors, 0 warnings ===|

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Hey an error list! We like those, since at least we can follow along here (instead of "there are errors somewhere").

    I'm blushing, since I was trusting Adak to have compiled his own code :oops: There is an extra semicolon on line 9, right before the right parenthesis.

  9. #9
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    Ahh, I should have caught that.

    Now it works.

    How would I go about making it print out each lines average?

    10 11 12 13 14 15 12.50

    Make another if statement?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Where the printing-of-the-new-line happens, you need to print the average at the same point (but just before the printing-of-the-new-line).

  11. #11
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    Ok, how do I make it calculate the sum and the average it.

  12. #12
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
        int i, j, sum;
        sum = 0;
    
        for(i = 10, j = 0; i <= 33; i++, j++)
        {
            sum += i;
            printf("%3d", i);
    
        if(j == 5)
        {
            sum += j;
            printf("%3d\n", j);
            j = -1;
        }
        }
        return 0;
    }
    Not sure how to calculate an average.

    All I just did was add 5 where the average should be.
    10 11 12 13 14 15 5

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Then maybe you should look up the word "average".

  14. #14
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    I'm not to fond with the way you are helping me. I know how to average and i know the averages of each line. Your talking to me like I am a complete idiot. All I doing is trying to get help.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So if you know how to average, then do it. There's not anything here to help you with. (In case you were wondering, / divides things.)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 06:38 PM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 06:06 PM
  3. extra word printing
    By kashifk in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2003, 05:03 PM
  4. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 08:18 AM
  5. help with operator <
    By kashifk in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2003, 04:49 PM

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