Question

C Programming.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Explanation:

This program will give us the random genarated values in the out put. Please find the attached out put screen shot for one of the random generated out put pattern.

Program:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROWSOBJ 10
#define COLSOBJ 10
int main (void) {

int iObj, jObj, kObj, directionObj;
char boardObj[ROWSOBJ][COLSOBJ];
const char lettersObj[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

srand ((unsigned) time(NULL));

for (iObj = 0; iObj < ROWSOBJ; iObj++)
for (jObj = 0; jObj < COLSOBJ; jObj++)
boardObj[iObj][jObj] = '.';

iObj = 0;
jObj = 0;
kObj = 1;
boardObj[iObj][jObj] = lettersObj[0];
while (kObj < 26) {
directionObj = rand() % 4;
switch (directionObj) {
case 0: if (boardObj[iObj][jObj + 1] == '.' && jObj != ROWSOBJ - 1){
boardObj[iObj][jObj +1] = lettersObj[kObj];
kObj++; jObj++;}
if (boardObj[iObj][jObj + 1] != '.' && boardObj[iObj + 1][jObj] != '.' && boardObj[iObj - 1][jObj] != '.' && boardObj[iObj][jObj - 1] != '.')
kObj = 27;
break;   
case 1: if (boardObj[iObj + 1][jObj] == '.' && iObj != COLSOBJ -1) {
boardObj[iObj + 1][jObj] = lettersObj[kObj];
kObj++; iObj++; }
if (boardObj[iObj][jObj + 1] != '.' && boardObj[iObj + 1][jObj] != '.' && boardObj[iObj - 1][jObj] != '.' && boardObj[iObj][jObj - 1] != '.')
kObj = 27;
break;
case 2: if (boardObj[iObj - 1][jObj] == '.' && iObj != 0){
boardObj[iObj - 1][jObj] = lettersObj[kObj];
kObj++; iObj--; }
if (boardObj[iObj][jObj + 1] != '.' && boardObj[iObj + 1][jObj] != '.' && boardObj[iObj - 1][jObj] != '.' && boardObj[iObj][jObj - 1] != '.')
kObj = 27;
break;
case 3: if (boardObj[iObj][jObj - 1] == '.' && jObj != 0) {
boardObj[iObj][jObj - 1] = lettersObj[kObj];
kObj++; jObj--;}
if (boardObj[iObj][jObj + 1] != '.' && boardObj[iObj + 1][jObj] != '.' && boardObj[iObj - 1][jObj] != '.' && boardObj[iObj][jObj - 1] != '.')
kObj = 27;
break;
}
}
for (iObj = 0; iObj < ROWSOBJ; iObj++) {
for (jObj = 0; jObj < COLSOBJ; jObj++)
printf ("%4c", boardObj[iObj][jObj]);
printf ("\n");
}

return 0;

}

Output screenshot:

Add a comment
Know the answer?
Add Answer to:
C Programming. Write a prograrm that generates a "random walk" across a 10 × 10 array....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT