how do i make a mathematical table using loops.

This is a discussion on how do i make a mathematical table using loops. within the C Programming forums, part of the General Programming Boards category; How do i write a program that has only two nested do......while loops that produces: + 5 10 15 20 ...

  1. #1
    in the shadows
    Join Date
    Oct 2005
    Location
    nairobi,kenya
    Posts
    9

    Unhappy how do i make a mathematical table using loops.

    How do i write a program that has only two nested do......while loops that produces:
    + 5 10 15 20 25 30
    50 55 60 65 70 75 80
    40 45 50 55 60 65 70
    30 35 40 45 50 55 60
    20 25 30 35 40 45 50
    10 15 20 25 30 35 40

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Well can you produce a single while loop which can generate the first row of the output?
    Show us what you know so far, and don't forget the [code][/code] tags when posting code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    in the shadows
    Join Date
    Oct 2005
    Location
    nairobi,kenya
    Posts
    9
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int i,j;
    clrscr();
    printf("+ ");
    i=5;
    do
    {
    printf("%4d");
    i=i+1;
    }while(i<=30);
    }
    this is what i have so far i can output the first line but ican find away to output the rest.

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Remove conio.h/clrscr() from your code, they don't do anything useful and aren't portable.

    main needs to return int, not void.

    You do "printf("%4d");", but where is the variable you are printing?

    Why do you have i=i+1 when you are supposed to go in steps of 5?

    Try to indent your code so it's more readable.

    Suggest you re-post the code above so that it works, then we'll help with the "rest".

  5. #5
    in the shadows
    Join Date
    Oct 2005
    Location
    nairobi,kenya
    Posts
    9
    Code:
    int main()
    {
    int   i,j;
              printf("+");
      i=5;
     do
    {
            printf("%4d",i);
      i=i+5;
    }while(i<=30);
    }
    i=i+1 is for adding the value to the next value is added by 5.

  6. #6
    in the shadows
    Join Date
    Oct 2005
    Location
    nairobi,kenya
    Posts
    9
    ingnore i=i+1, it is suposed to be i=i+5

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    So now just wrap another loop (and another variable around that code
    Code:
    int row = ?;
    do {
        do {
        } while ( i <= 30 );
    } while ( row ? );
    Replace the ? with whatever you want to achieve the result you're after.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    in the shadows
    Join Date
    Oct 2005
    Location
    nairobi,kenya
    Posts
    9
    could you please post the whole code am still having problems

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    isn't it easier to use 'for'?

  10. #10
    ---
    Join Date
    May 2004
    Posts
    1,379
    Alastor: It's probably homework.

  11. #11
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I think the most effective way to do what you want is:

    Code:
    #include <stdio.h>
    
    int main()
    {
        printf(" +  5 10 15 20 25 30\n");
        for(int i = 50; i >= 10; i-=10)
        {
            for(int j = 0; j <= 30; j+=5)
            {
                printf("%d ",i+j);
            }
            printf("\n");
        }
        return 0;
    }
    That, in my opinion, is the most effective way to achieve the effect that you want. Notice that the first line is a heading and not part of the actual loop.

    If you must you two do...while loops then just set i and j ahead of time and increment them within the loop.
    Don't quote me on that... ...seriously

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, the most "effiecient" way to do it it:
    Code:
    do {
        while(0);
        puts(" +  5 10 15 20 25 30\n"
            50 55 60 65 70 75 80\n"
            40 45 50 55 60 65 70\n"
            30 35 40 45 50 55 60\n"
            20 25 30 35 40\n45 50\n"
            10 15 20 25 30 35 40");
        } while(0);
    Meeting their criteria of 2 loops.

    PS: There's a reason we don't simply DO THE WHOLE GOD DAMN THING FOR THEM. It's so they actually TRY.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    quzah,
    You are my role model.
    Woop?

  14. #14
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I didn't just give him the answer. I didn't use any do..while loops. He probably turned it in already any ways. I didn't think that it would be a completely new idea and he has probably already seen something like what I put up. I just figured he hadn't made the connection.
    I try to show things that the reader will see and go "Oh I see now." even though it isn't exactly what they need. I'm sorry that I went to far on that one.
    Don't quote me on that... ...seriously

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefile Problem: None rule to make target
    By chris24300 in forum Linux Programming
    Replies: 25
    Last Post: 06-17-2009, 10:45 AM
  2. trying to make a hash table......trouble w/ memory?
    By cuizy in forum C Programming
    Replies: 3
    Last Post: 05-11-2009, 05:47 PM
  3. Duplicate at Hash Table
    By DarrenY in forum C Programming
    Replies: 1
    Last Post: 05-10-2007, 03:31 AM
  4. How do I make an un-editable table?
    By Stan100 in forum Windows Programming
    Replies: 2
    Last Post: 06-24-2005, 12:30 PM
  5. HELP:Hasing Table
    By Amber_liam in forum C++ Programming
    Replies: 2
    Last Post: 06-20-2002, 05:25 AM

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