help with logic please

This is a discussion on help with logic please within the C Programming forums, part of the General Programming Boards category; Hey all, i'm just starting out with c. I purchased a good book by K.N.KING called C Programming: A Modern ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    4

    Unhappy help with logic please

    Hey all, i'm just starting out with c. I purchased a good book by K.N.KING called C Programming: A Modern Approach.

    Working through the exercises are fun, but without the answers, challenging for sure.

    This code is supposed to simply run through arr[][] by way of rand() for the direction. I've included some code to help me debug the logic ( to no avail ).

    Any help would be greatly appreciated !!

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<time.h>
    #define MIN 0
    #define MAX 9
    #define N 10
    #define D 4
    #define TRUE 1
    #define FALSE 0
    #define OPEN (arr[i][j] < 'A') || (arr[i][j] > 'Z')
    #define WITHIN_ARR0 ((i > MIN) && (i <= MAX)) && ((j >= MIN) && (j <= MAX))
    #define WITHIN_ARR1 ((i >= MIN) && (i <= MAX)) && ((j >= MIN) && (j < MAX))
    #define WITHIN_ARR2 ((i >= MIN) && (i < MAX)) && ((j >= MIN) && (j <= MAX))
    #define WITHIN_ARR3 ((i >= MIN) && (i <= MAX)) && ((j > MIN) && (j <= MAX))
    main()
    {
    char arr[N][N]={{'.','.','.','.','.','.','.','.','.','.'},
                    {'.','.','.','.','.','.','.','.','.','.'},
                    {'.','.','.','.','.','.','.','.','.','.'},
                    {'.','.','.','.','.','.','.','.','.','.'},
                    {'.','.','.','.','.','.','.','.','.','.'},
                    {'.','.','.','.','.','.','.','.','.','.'},
                    {'.','.','.','.','.','.','.','.','.','.'},
                    {'.','.','.','.','.','.','.','.','.','.'},
                    {'.','.','.','.','.','.','.','.','.','.'},
                    {'.','.','.','.','.','.','.','.','.','.'}};
    int i=0, j=0, x=0, d=4, direction=0, flag=TRUE, arr2[]={0,0,0,0};
    char alph='A';
    srand((unsigned) time(NULL));
    
    while (alph <= 'Z'){
    
    if ((arr2[0]>0) && (arr2[1]>0) && (arr2[2]>0) && (arr2[3]>0)){
                    for (i=0;i<N;i++){
                    for (j=0;j<N;j++){
                    printf("%c", arr[i][j]);}
                    printf("\n");}
    printf("\narr2[0]=%d", arr2[0]);
    printf("\narr2[1]=%d", arr2[1]);
    printf("\narr2[2]=%d", arr2[2]);
    printf("\narr2[3]=%d\n", arr2[3]);
    system("pause");
    return 0;}
    
    direction = (rand() % D);
    
    
    switch (direction){
        case 0:
            if (OPEN){
                 arr[i][j]=alph;
                 printf("assign %c to arr[%d][%d]\n", alph, i, j);
                 printf("direction is %d\n", direction);
                 alph++;
                              for (x=0;x<3;x++){
                              arr2[x]=FALSE;
                              printf("arr2[%d]=%d ",x,arr[x]);}          
                  if (WITHIN_ARR0) i--;
            break;}
            else {arr2[direction] = TRUE; printf("\narr2[%d]=%d\n", direction, arr2[0]); break;}
                    
        case 1:
            if (OPEN){
                    arr[i][j]=alph;
                     printf("assign %c to arr[%d][%d]\n", alph, i, j);
                     printf("direction is %d\n", direction);
                    alph++;
                                     for (x=0;x<3;x++){
                                     arr2[x]=0;}
                     if (WITHIN_ARR1) j++;
            break;}
            else {arr2[direction] = TRUE; printf("\narr2[%d]=%d\n", direction, arr2[1]); break;}
    
        case 2:
            if (OPEN){
                    arr[i][j]=alph;
                     printf("assign %c to arr[%d][%d]\n", alph, i, j);
                     printf("direction is %d\n", direction);
                    alph++;
                                     for (x=0;x<3;x++){
                                     arr2[x]=0;}
                    if (WITHIN_ARR2) i++;
            break;}
            else {arr2[direction] = TRUE; printf("\narr2[%d]=%d\n", direction,arr2[2]);break;}
    
        case 3:
            if (OPEN){
                arr[i][j]=alph;
                 printf("assign %c to arr[%d][%d]\n", alph, i, j);
                 printf("direction is %d\n", direction);
                alph++;
                             for (x=0;x<3;x++){
                             arr2[x]=0;}
                if (WITHIN_ARR3) j--;
            break;}
            else {arr2[direction] = TRUE; printf("\narr2[%d]=%d\n", direction,arr2[3]);break;}
    
        default: {
            printf("direction is: %d, and not correct", direction);
            break;}
    }
    }
        for (i=0;i<N;i++){
            for (j=0;j<N;j++){
            printf("%c", arr[i][j]);}
        printf("\n");}
    
    
    
    system("pause");
    return 0;
    }
    ps. i have not worked with functions yet (at this point in the book), so i'd rather not for this program.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Care to expand on what your problem actually is?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    4

    o sorry :P

    Sorry about that,

    Anyway, the program is supposed to output like this:

    A.........
    BC........
    ..D........

    and so on, until alphabet is complete.
    The problem i'm having, is if the random direction leads me back into a previous letter (eg the next direction after D is up), the program craps out.

    At this point, ive tried printing out the contents of arr2[1]-arr2[3], and therein lies my problem (i think).

    The contents are numbers which are like 5 digits long, instead of the single digits i'm expecting !

    Again, thx in advance.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    42

    Cool OOPS !!!!

    Why don't you go to some simpler book like the one by Herbert Schildt ?. That would (probably ) give you a better (and easier!!!)learning experience.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: OOPS !!!!

    Originally posted by CodeJerk
    Why don't you go to some simpler book like the one by Herbert Schildt ?. That would (probably ) give you a better (and easier!!!)learning experience.
    Which one of his not recommended books are you refering to?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    27
    K.N.KING called C Programming: A Modern Approach

    Actually its a GREAT book!

    I remember doing the same program then.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    42

    Why don't people read what the thread is abt....before posting

    Bah!!!!!
    Hey Hammer....you could do well by reading the entire thread before posting. That way, you could have known what book I was referring to.

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    4
    Ack, what are the chances of getting jacked by a Jerk AND a Mod ?

    yikes !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Digital Logic
    By strokebow in forum Tech Board
    Replies: 3
    Last Post: 12-09-2006, 01:05 PM
  2. Logic
    By LordBronz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2006, 06:41 PM
  3. Actors, cues, event based logic.
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 04-27-2006, 11:58 PM
  4. Circular Logic
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-15-2001, 09:10 PM

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