This is my initial board. The first problem I'm having is that when the user enters the board size, it always prints the same 10 by 10 rather than what the user asked for. Secondly, I have a function below which is supposed to check each cell and convert the cells which match the condition to a whitespace or dash. I'm having trouble printing the board. Can anyone please tell me where I'm going wrong?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 10
int createBoard();
int main(int argc, char *argv[])
{
createBoard();
}
int createBoard()
{
char myArray[MAX][MAX];
char letter[3] = {'a', 'b', 'c'};
int i,j,row,col;
printf("Please enter your grid size: ");
scanf("%d %d", &row, &col);
if(row < 10 && col < 10){
for(i=0; i < MAX; i++){
for(j=0; j < MAX; j++){
myArray[i][j] = letter[rand()%3];
}
}
for(i=0; i < MAX; i++){
for(j=0; j < MAX; j++){
printf("%c ", myArray[i][j]);
}
printf("\n");
}
}
else{
printf("Board is too big\n");
createBoard();
}
int var;
var = move(myArray);
}
//to check each cell
int newBoard(char myArray[MAX][MAX])
{
int i,j;
for(i=0; i < MAX; i++){
for(j=0; j< MAX; j++){
if(myArray[i][j] == 'c' && myArray[i+1][j] == 'c'){
myArray[i][j] == ' ';
myArray[i+1][j] == ' ';
}
else{
//no update
}
}
}
}
For 1st problem change your for loops
for(i=0; i < MAX; i++){
for(j=0; j < MAX; j++){
...
}
}
to use row and col variables
for(i=0; i < row; i++){
for(j=0; j < col; j++){
...
}
}
first problem solotion :
because in your for loop you go through MAX
change it to :
for(i=0; i < row; i++){
for(j=0; j < col; j++){
myArray[i][j] = letter[rand()%3];
}
}
second problem solution :
first problem with check function is that you should pass my Array by reference not by value
change it by adding'&' sign
second problem is you have to say if(myArray[i][j] == 'c' && myArray[i+1][j] == 'c')
this cause problem when i is equal to MAX-1 (last for step) and myArray[i+1][j] does not exist !!
because the size of array is [MAX][MAX] and when i = MAX-1 , you size would be [MAX+1][MAX]
Related
Being new to C, and this website, I'm unfamiliar with this problem I'm having. I have a 2 dimensional array with [8][8] elements. I'm trying to get the user to enter numbers into the array until finished. The program is far from finished, but I'm stuck on this problem before I can move on. Basically I use a for loop to let the user enter into each element. However, when the first row is complete, it overwrites it's last value onto the first column second row element spot. How can I prevent this from happening: Here's my code:
#include <stdio.h>
#include <string.h>
int Check_rules();
void Print_Array(int array[][8], int size)
{
int i, j;
for (i = 0; i <= size; i++)
{
printf("\n");
for (j = 0; j <= size; j++)
{
printf("%d ",array[i][j]);
}
}
printf("\n\n");
}
int main()
{
int size = 8;
int i, j;
int fullArray[size][size];
int grid1[3][3];
int grid2[3][3];
int grid3[3][3];
int grid4[3][3];
int grid5[3][3];
int grid6[3][3];
int grid7[3][3];
int grid8[3][3];
int grid9[3][3];
for (i = 0; i <= size; i++)
{
for (j = 0; j <= size; j++)
fullArray[i][j] = 0;
}
printf("Want to play a game? Enter values 1-9 starting in row 1 column 1, \nand we will work our way from there. Here's the playing board.\nIt's Sudoku, so follow the rules of the game.\n\n");
for (i = 0; i <= size; i++)
{
printf("\n");
for (j = 0; j <= size; j++)
printf("%d ",fullArray[i][j]);
}
printf("\n\n");
int tmp;
char *keeper = (" ");//space for marker
for (i = 0; i <= size; i++)
{
for (j = 0; j <= size; j++)
{
printf("Enter first value(press 0 and ENTER to skip a box, \nand -1 to cancel game): ");
scanf("%d", &tmp);
if(tmp == -1)
return 0;
fullArray[i][j] = tmp;
Print_Array(fullArray,size);
}
}
return 0;
}
If you run this you'll see my problem when you enter the last value in row 1. It overwrites the second row first column element spot?
Everywhere you have <= size, you actually want < size. This is because C uses 0-based indexes. That means if you have an array with 5 elements, the indexes are 0, 1, 2, 3, 4. In a loop like for (int i = 0; i <= 5; i++), i would get the values 0, 1, 2, 3, 4, 5. That last one is an invalid index into the array. Using i < 5 fixes the problem (ensures i stops before it reaches 5).
Fixed and cleaned up version of your code:
#include <stdio.h>
#include <string.h>
void printArray(int size, int array[][size]) {
for (int i = 0; i < size; i++) {
printf("\n");
for (int j = 0; j < size; j++) {
printf("%d ", array[i][j]);
}
}
printf("\n\n");
}
int main() {
int size = 8;
int fullArray[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
fullArray[i][j] = 0;
}
}
printf("Enter values in row 1 column 1, and we will work our way from there. Here's the playing board. \n\n");
printArray(size, fullArray);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("Enter first value (press 0 and ENTER to skip a box, or -1 to cancel game): ");
int number;
scanf("%d", &number);
if(number == -1) {
return 0;
}
fullArray[i][j] = number;
printArray(size, fullArray);
}
}
return 0;
}
EDIT
To clarify, this is fixed version of the original code in the question. The new code is a bit different, but I think the issue is the same.
I am trying to build a dynamic maze i got to the part where i get the size and get the chars that i need to build the maze from them.
but the function thats build the maze prints it really asymmetrical how can i fix that?
my code:
#include <stdio.h>
#include <stdlib.h>
char **board;
int size = 0;
void print_Board();
void initialize_Board();
int main()
{
initialize_Board();
print_Board();
return 0;
}
/*initialize the board*/
void initialize_Board()
{
int i, j;//indexs
char s;
scanf("%d", &size);
board = (char**)malloc(sizeof(char*)* (size));
if (!board) { printf("ERROR - memroy allocation.\n"); exit(1); }
for (i = 0; i < size; i++)//for loops to build the board for the game
{
board[i] = (char*)malloc(sizeof(char)*(size));
if (!board[i]) { printf("ERROR - memroy allocation, for loop\n");
exit(1);
}
for (j = 0; j < size; j++)
{
scanf("%c", &s);
board[i][j] = s;
}//for col
printf("\n");
}//for row
}
//print the board
void print_Board()
{
int i, j;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("%c ", board[i][j]); //print the value in the [i][j] place.
}//for col
printf("\n");
}//for row
}
Change:
for (j = 0; j < size; j++)
{
scanf("%c", &s);
board[i][j] = s;
}//for col
To:
for (j = 0; j < size; j++) {
scanf("%c ", &s);
board[i][j] = s;
}//for col
board[i][j] = '\n'; // Add new line to end of row making it a string.
This ensures that each character is read and the return char is discarded.
and change:
int i, j;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("%c ", board[i][j]); //print the value in the [i][j] place.
}//for col
printf("\n");
}//for row
to:
int i;
for (i = 0; i < size; i++) {
printf("%s", board[i]); //print the values in the [i] row.
}
This prints each row with a newline at the end.
I had searched so much on the forum, but I didn't find a solution for my problem.
How do I print a matrix that is given like this matrix[2][2]={{1,2},{3,4}} and give it back like:
1 2
3 4
#include <stdio.h>
int main(){
int i,j;
int x [2][2]={{1,2},{3,4}};
for(i=0; i<2; i++) {
for(j=0; j<2; j++) {
printf(" %d", x[i][j]);
}
printf("\n");
}
}
This program support not only 2x2 matrix but also int matrix[2][3]={{1,2,3},{4,5,6}}; or other size.
#include <stdio.h>
int main(void) {
int matrix[2][2]={{1,2},{3,4}};
size_t i, j;
for (i = 0; i < sizeof(matrix)/sizeof(matrix[0]); i++) {
for (j = 0; j < sizeof(matrix[i])/sizeof(matrix[i][0]); j++) {
if (j > 0) putchar(' ');
printf("%d", matrix[i][j]);
}
putchar('\n');
}
return 0;
}
Simple nested for loop:
for (int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
for (int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
printf("%d\t", matrix[i][j]); // tab-separated. Did you want a space?
}
if (i < 2 - 1) printf("\n"); // newline except at the end
}
is one way.
I am trying to write a C code that will print a pyramid structure on screen, something like this.
The corresponding code I've written is something like this.
#include <stdio.h>
#include <stdlib.h>
void printArrayFunc(char arr[9][5]) {
int i, j;
printf("=========================================\nprinting the values\n");
for (i = 0; i < 5; i++) {
for (j = 0; j < 9; j++) {
//printf("arr[%d][%d] = %d\n", i,j, arr[i][j]);
if (arr[i][j] == 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
int main() {
int i, j;
char arr[9][5] = {
0
};
printf("============================\nfilling the values\n");
for (i = 0; i < 5; i++) {
for (j = 4 - i; j <= 4 + i; j++) {
arr[i][j] = 1;
// printf("arr[%d][%d]= %d\n",i,j,arr[i][j]);
}
//printf("\n");
}
printArrayFunc(arr);
return 0;
}
It is giving an output like
I know I'm doing some silly mistake but at this moment, I'm not able to find what is going wrong. Let me hear your comments on this.
In the function argument:
char arr[9][5]
In the loop:
for (i = 0; i<5; i++) {
for (j = 0; j<9;j++) {
if (arr[i][j] == 1)
You flipped the position of i and j. i should go from 0 to 9, j from 0 to 5.
if (arr[i][j] == 1)
printf("*");
else
printf(" ");
This statement is giving the garbage value in this statement if if condition is true then it print else statement and when else comes true it prints the garbage value.
I'm trying to print a 2d array with a function, but I keep getting the error "pointer expected"
I'm trying to make a battleship-type grid. I'm fine with printing out the co-ordinate row and column, but I can't actually get the 2d array (which contains "." in every element) to print at all.
Any help would be appreciated, I'm very new to this. Thanks! :)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int length;
int width;
int i;
int j;
char invisible_board;
void board_setup(int *rows, int *columns){
char *invisible_board[*rows][*columns];
char *player_board[*rows][*columns];
for (i = 0; i < *rows; i++){
for (j = 0; j < *columns; j++){
invisible_board[i][j] = "."; //Sets all elements in hidden board to water
}
}
for (i = 0; i < *rows; i++){
for (j = 0; j < *columns; j++){
player_board[i][j] = ".";
}
}
}
void display(int *rows, int *columns, char *invisible_board){
printf(" ");
for (i=1; i < *rows +1;i++){
printf("%d ",i);
}
printf("\n"); //Prints top row of co-ordinates
for (i=1; i < *columns+1;i++){
printf("%d ",i);
for (j=0;j < *columns;j++){ //Prints left column of co- ordinates and rows of game board
printf(" %c ",invisible_board[i-1][j]);
}
printf("\n");
}
}
int main(void){
printf("Please enter the amount of rows in your board\n");
scanf("%d",&length);
printf("Please enter the amount of columns in your board\n");
scanf("%d",&width);
board_setup(&length,&width);
display(&length,&width,&invisible_board);
return (0);
}
this is the simplest changes I could make to your code to get you to working code.... now.... this isn't good code yet. But gets you started.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int length;
int width;
int i;
int j;
char invisible_board[100][100]; // dynamically allocate....
char player_board[100][100]; // dynamically allocate....
void board_setup(int *rows, int *columns){
for (i = 0; i < *rows; i++){
for (j = 0; j < *columns; j++){
invisible_board[i][j] = '.'; //Sets all elements in hidden board to water
}
}
for (i = 0; i < *rows; i++){
for (j = 0; j < *columns; j++){
player_board[i][j] = '.';
}
}
}
void display(int *rows, int *columns){
printf(" ");
for (i=1; i < *rows +1;i++){
printf("%d ",i);
}
printf("\n"); //Prints top row of co-ordinates
for (i=1; i < *columns+1;i++){
printf("%d ",i);
for (j=0;j < *columns;j++){ //Prints left column of co- ordinates and rows of game board
printf(" %c ",invisible_board[i-1][j]);
}
printf("\n");
}
}
int main(void){
printf("Please enter the amount of rows in your board\n");
scanf("%d",&length);
printf("Please enter the amount of columns in your board\n");
scanf("%d",&width);
board_setup(&length,&width);
display(&length,&width);
return (0);
}