IF within a FOR loop, continuing saga...

This is a discussion on IF within a FOR loop, continuing saga... within the C++ Programming forums, part of the General Programming Boards category; Dear CProgramming.com I am trying to get a program to print the following Cayley table on screen:- 12345678 23456781 34567812 ...

  1. #1
    Registered User Robert_Ingleby's Avatar
    Join Date
    Oct 2001
    Posts
    57

    IF within a FOR loop, continuing saga...

    Dear CProgramming.com

    I am trying to get a program to print the following Cayley table on screen:-

    12345678
    23456781
    34567812
    45678123
    56781234
    67812345
    78123456
    81234567

    The problem is that my code prints this:-

    12345678
    23456789
    345678910
    4567891011
    56789101112
    678910111213
    7891011121314
    89101112131415

    I can see why it is doing this. Its because the variable 'b' reaches 9 and at a this point it needs to be reset to 1.

    My code is as follows:-


    #include <stdio.h>

    int main (void)

    {
    int a = 1;
    int b = 1;
    int c = 1;
    int d = 1;

    printf("Cayley Table\n\r");

    while (a<=8)
    {
    b=d;
    for (c=1; c<=8; b=b+1,c=c+1) ***** printf("%d",b);
    printf("\n");
    d=d+1;
    a=a+1;
    }

    return 0;
    }

    At the point marked with 5 stars, the line "if(b>=8)b=1" needs to be added, the problem is the compiler comes up with "EXPRESSION SYNTAX" errors. I cannot use any semicolon near this bit of code or the program will not check to see if 'b' has reached 9 before resetting it back to 1 within the loop using variable 'c'.

    I did post a suggestion here the day b4 yesterday and have tried what was suggested but
    I could not get it to work. Please copy the code into a compiler and try it, you will see then what my problem is.

    I have tried for AGES many different ways of trying to get this program to work
    but without success. Whoever can help me complete this deserves a medal.

    Thank-you once again.

    Rob.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    Code:
    #include <stdio.h>
    int main(void) {
        int a = 1;
        int b = 1;
        int c = 1;
        int d = 1;
        printf( "Cayley Table\n\r" );
        while(a<=8) {
            b=d;
            for(c=1; c<=8; b=b+1,c=c+1) {
                if(b >= 8) b = 1;
                printf( "%d",b );
            }
            printf( "\n" );
            d=d+1;
            a=a+1;
        }
        return 0;
    }
    Oh well........................
    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
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    Salem, you beat me to it.

    I had a meeting at work.

    here is the code i came up with:

    #include <stdio.h>

    int main (void)

    {
    int a = 1;
    int b = 1;
    int c = 1;
    int d = 1;

    printf("Cayley Table\n\r");

    while (a <= 8)
    {
    b=d;
    for (c = 1; c <= 8; b++, c++){
    if(b == 9){
    b=1;
    printf("%d",b);
    }
    else
    printf("%d",b);
    }
    printf("\n");
    d=d+1;
    a=a+1;
    }

    return 0;
    }
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. Ignoring Lines and Continuing through Spaces,
    By Blurr in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2007, 10:31 AM
  3. Windows startup - must click user before continuing
    By ulillillia in forum Tech Board
    Replies: 2
    Last Post: 04-30-2007, 04:06 PM
  4. The saga continues: Algebraic simplification
    By Roule in forum C++ Programming
    Replies: 3
    Last Post: 10-17-2004, 09:56 PM
  5. Help with continuing to the next function.
    By loopy in forum C Programming
    Replies: 2
    Last Post: 04-25-2002, 08:52 AM

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