# Thread: help with logic please

1. ## 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. Care to expand on what your problem actually is?

3. ## 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. ## 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. ## 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?

6. K.N.KING called C Programming: A Modern Approach

Actually its a GREAT book!

I remember doing the same program then.

7. ## 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. Ack, what are the chances of getting jacked by a Jerk AND a Mod ?

yikes !

Popular pages Recent additions