Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
void printBoard(int board[8][8])
{
int i,j;
for(i = 0; i<8; i++)
{
for(j = 0; j<8; j++)
{
printf("%d ", board[i][j]);
}
printf("\n");
}
}
int main(){
int i, j;
int board[8][8];
for(i = 0; i<8; i++)
{
for(j = 0; j<8; j++)
{
scanf("%d", &board[i][j]);
}
}
printBoard(board);
Why does the following void function not print the two dimensional array? It just continues asking for input. I tried it without the function as well, and it still doesn't work.
We were meant to use this as part of our homework assignment. Can someone please explain how to get around this?
Your main is looping through scanf which is a function that asks for user input 64 times. You need to input 64 values before the printBoard() statement is reached.
First of all your printBoard function doesn't print all array.
When you fix this (first loop in function 8 instead of 2) you get what you want but i suggest try on smaller array, since you need to put 64 values.
In nested loops, the inner loop will execute to completion for every time that the outer loop executes. This means that scanf will be called 8 * 8 = 64 times. scanf takes an input from the console, so that means you will have to input 64 values into the console before they are printed.
Maybe try reducing the size of the array from 8 * 8 to something more manageable, like 3 * 3 or 4 * 4 -- that way you don't have to spend a minute or two punching in numbers just to test it.
#include<stdio.h>
void printBoard(int board[8][8]);
int main(){
int i, j;
int board[8][8];
for(i = 0; i<8; i++)
{
for(j = 0; j<8; j++)
{
scanf("%d", &board[i][j]);
}
}
return 0;
}
void printBoard(int board[8][8])
{
int i,j;
for(i = 0; i<8; i++)
{
for(j = 0; j<8; j++)
{
printf("%d ", board[i][j]);
}
printf("\n");
}
}
In your i for loop it is row and j loop is column so loop will 8 row and 8 columns so it will run 64 times, then it take 64 inputs and after scanf will complete.
So I was inputting my values like this to begin with:
00000000
00100000
00001000
00001010
01010000
00020000
00000000
00000000
Which didn't seem to work.
Then, I tried:
0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 1 0 1 0
0 1 0 1 0 0 0 0
0 0 0 2 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
and it worked. I guess it counted 64 individual values this way.
Thanks!
Related
I'm still a noob at programming and still don't know many of the functions that I'm able to use so I kept to the basics, I was trying to create the game 2048 in C, for that I decided to make a function to push numbers in certain directions, either upwards, downwards, left or right. My up function is working, but my down function isn't even though I did the opposite of what I did in my up function. In the push_down function the numbers actually do what they do in the push_up functions, they move up instead of down.
Would love to know why it isn't working if anyone can spot my mistake
//this one is working
void push_up(int game_size, int grid[game_size][game_size])
{
int replace;
int num=0;
for (int j = 0; j < game_size ; ++j)
{
for (int i = 0; i < game_size ; ++i)
{
replace = 100;
if(grid[i][j]==0)
{
for (int a = game_size - 1 ; a > 0 + i ; --a)
{
if(grid[a][j]!=0)
{
num=grid[a][j];
replace=a;
}
}
grid[i][j]=num;
num=0;
}
if(replace!=100){
grid[replace][j]=0;
}
}
}
}
// this one isn't working
void push_down(int game_size, int grid[game_size][game_size])
{
int replace;
int num=0;
for (int j = 0; j < game_size ; ++j)
{
for (int i = game_size - 1; i >= 0 ; --i)
{
replace = 100;
if(grid[i][j]==0)
{
for (int a = 0 ; a < game_size - i ; ++a)
{
if(grid[a][j]!=0)
{
num=grid[a][j];
replace=a;
}
}
grid[i][j]=num; //
num=0;
}
if(replace!=100){
grid[replace][j]=0;
}
}
}
}
What I did in both is mainly to run the column(either upwards or downwards depending on the function) trying to find a zero, lock the position of the zero and try to find the next number in that column by running the column in the opposite direction, the last number (if a number was even found) would be used to fill that zero and the position where the number was would be filled with 0.
So, let me give an example of what I wanted with these function:
In the first function (push_up) I wanted and was able to do a function that would transform this matrix :
4 2 4 0
0 0 0 0
0 4 2 2
2 0 4 2
into this one:
4 2 4 2
2 4 2 2
0 0 4 0
0 0 0 0
So, all the values would move to the highest position possible inside the column.
In the second function what I wanted was to transform this matrix:
4 2 4 0
0 0 0 0
0 4 2 2
2 0 4 2
into this one:
0 0 0 0
0 0 4 0
4 2 2 2
2 4 4 2
All the values would move to the lowest possible position inside the matrix. But I wasn't able to, by running the function what I get is:
This is the original matrix:
0 0 0 0
4 0 0 2
2 0 0 4
0 2 0 0
This is the matrix after using the function push_down:
2 2 0 4
4 0 0 2
0 0 0 0
0 0 0 0
There are probably easier ways to do this, I would love to hear them but I would also love to know where my logic went wrong in the push_down function since in the push_up function it's working fine.
Your example isn't reproducible and I'm not certain exactly what you're trying to do, but this is the obvious error:
for (int a = 0 ; a > game_size - i ; ++a)
{
if(grid[a][j]!=0)
{
num=grid[a][j];
replace=a;
}
}
This loop will never run, a is zero and will never be greater than game_size - 1.
I have some dificulties in creating the following array. My task is to fill using recursion a 2D array with all the possible combinations of 0 and 1 taken m times in lexical order. Mathematically speaking there are 2 ^ m combinations.My program just fills the first 3 rows of the array with the same order 0 1 0 1 and then just prints for the rest of the rows 0 0 0 0.
Example
m=4
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1
This is my code so far and I appreciate if someone could correct it and explain me what I am doing wrong as I can't spot the mistake myself
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
void *safeMalloc(int n) {
void *p = malloc(n);
if (p == NULL) {
printf("Error: malloc(%d) failed. Out of memory?\n", n);
exit(EXIT_FAILURE);
}
return p;
}
void combine(int** arrTF,int m,int n,int row,int col){
if(m==0){
if(row<pow(2,m)){
row++;
combine(arrTF,n,n,row,0);
}else{
return;
}
}else{
arrTF[row][col]=0;
col++;
combine(arrTF,m-1,n,row,col);
arrTF[row][col]=1;
col++;
combine(arrTF,m-1,n,row,col);
}
}
int main(int argc, char *argv[]) {
int m
scanf("%d",&m);
int** arrTF;
arrTF = safeMalloc(pow(2,m)*sizeof(int *));
for (int r=0; r < pow(2,m); r++) {
arrTF[r] = safeMalloc(m*sizeof(int));
}
for(int i=0;i<pow(2,m);i++){
for(int j=0;j<m;j++){
arrTF[i][j]=0;
}
}
combine(arrTF,m,m,0,0);
for(int i=0;i<pow(2,m);i++){
for(int j=0;j<m;j++){
printf("%d ",arrTF[i][j]);
}
printf("\n");
}
return 0;
}
You want all the possible (2^m) combinations of 0's and 1's taken m times in lexical order and you are using a 2D array to store the result.
Things would be very easy if you just want to print all the possible combination of 0's and 1's instead of storing it in 2D array and printing array later.
Storing a combination of 0's and 1's to 2D array is a little bit tricky as every combination is one element of your 2D array.
You want to generate the combination of 0's and 1's in accordance with the recursive algorithm.
So, let's say, at some stage if your algorithm generates the combination 0010 which is stored in an element in 2D array.
And the next combination would be 0011 which the recursive algorithm will generate just by changing the last number from 0 to 1 in the last combination (0010).
So, that means everytime when a combination is generated, you need to copy that combination to its successive location in 2D array.
For e.g. if 0010 is stored at index 2 in 2D array before the algorithm starts computing the next combination, we need to do two things:
Copy the elements of index 2 to index 3
Increase the row number so that last combination will be intact
(Say, this is 2D array)
|0|0|0|0| index 0
|0|0|0|1| index 1
|0|0|1|0| index 2 ---> copy this to its successive location (i.e. at index 3)
|0|0|1|1| index 3 ---> Last combination (index 2) and the last digit is changed from 0 to 1
.....
.....
.....
This we need to do for after every combination generated.
Now, I hope you got where you are making the mistake.
Few practice good to follow:
If you want to allocate memory as well as initialized it with 0, use calloc instead of malloc.
Any math function you are calling again and again for the same input, it's better to call it once and store the result in a variable and use that result where ever required.
Do not include any header file which is not required in your program.
Once done, make sure to free the dynamically allocated memory in your program.
I have made the corrections in your program:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void *safeMalloc(size_t n, size_t size) {
void *p = calloc(n, size);
if (p == NULL) {
printf("Error: calloc(%zu) failed. Out of memory!\n", n);
exit(EXIT_FAILURE);
}
return p;
}
void deallocate(int ** ptr, int row) {
for(int i = 0; i<row; i++)
free(ptr[i]);
free(ptr);
}
void combine(int **arrTF, int m, int max_col, int max_row) {
static int row;
if(m==0){
int i;
if (row<(max_row - 1))
{
for(i=0; i<max_col; i++)
arrTF[row+1][i] = arrTF[row][i];
}
row++;
return;
} else {
arrTF[row][max_col-m] = 0;
combine(arrTF, m-1, max_col, max_row);
arrTF[row][max_col-m] = 1;
combine(arrTF, m-1, max_col, max_row);
}
}
int main(int argc, char *argv[]) {
int** arrTF;
int m, max_row;
printf ("Enter number: \n");
scanf("%d", &m);
max_row = pow(2, m);
arrTF = safeMalloc(max_row, sizeof(int *));
for (int r=0; r<max_row; r++) {
arrTF[r] = safeMalloc(m, sizeof(int));
}
combine(arrTF, m, m, max_row);
for(int i=0; i<max_row; i++) {
for(int j=0; j<m; j++) {
printf("%d ", arrTF[i][j]);
}
printf("\n");
}
deallocate(arrTF, max_row);
return 0;
}
Output:
$ ./a.out
Enter number:
2
0 0
0 1
1 0
1 1
$ ./a.out
4
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1
Hope this helps.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I tried writing a program in C that should output something like this:
A B C D E F G H
1 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 0
8 0 0 0 0 0 0 0 0
The code:
#include <stdio.h>
#define FILA 8
#define COLUMNA 8
int main(void)
{
int nFila = 1;
int tablero[FILA][COLUMNA] = {};
int i, j;
printf(" A B C D E F G H\n");
for (i = 0; i < FILA; i++)
{
printf("%d ", nFila);
nFila++;
for (j = 0; i < COLUMNA; j++)
{
printf("%d ", tablero[i][j]);
}
printf("\n");
}
return 0;
}
However it outputs random numbers for each array index that appear to be memory adresses...
How can I accomplish that?
You should also initialize subarrays:
#include <stdio.h>
#define FILA 8
#define COLUMNA 8
int main(void)
{
int nFila = 1;
int tablero[FILA][COLUMNA] = {{},};
int i, j;
printf(" A B C D E F G H\n");
for (i = 0; i < FILA; i++)
{
printf("%d ", nFila);
nFila++;
for (j = 0; j < COLUMNA; j++)
{
printf("%d ", tablero[i][j]);
}
printf("\n");
}
return 0;
}
BTW I also fixed condition in inner loop.
You are getting junk values in your output. You can set all values in the array to 0 by partly initializing your array tablero with 0.Accordingly all the values in the array will be initalized to 0.
int tablero[FILA][COLUMNA] = {0};
Also, there is a bug in your code.
Please correct your line for (j = 0; i < COLUMNA; j++) to :
for (j = 0; j< COLUMNA; j++)
I am trying to set all the values in my 2D array to 0 and then print all the scores back to make sure they are correct.
struct Game {
int GameScoresHome[10][10];
};
int main() {
struct Game game;
memset(game.GameScoresHome, 0, sizeof game.GameScoresHome);
for (int x=0;x<100;x++) {
int y = floor(x/10);
printf("%d ",game.GameScoresHome[x][y]);
}
return 0;
}
The current output is:
0 0 0 0 0 0 0 0 0 0 0 0 822083893 32767 32767 32767 32767 32767 32767 1651067951 1634028652 1345283180 1702057263 1701080931 2054842477 1866870631 1885417061 1647262318 1146113364 896624241 1280918623 1919052108 1819042146 1818850626 1634956149 1852133983 1264923239 792545364 1666723698 1836345960 1163089152 1949263220 1919250021 1868774725 1213481296 796026224 1785230711 1650803759 792546380 1213481296 1868781615 1752003690 6780258 1497628720 778396783 1920232291 792545364 1666723698 1836345960 1735746149 796026224 1785230711 1650803759 0 0 3 0 7 0 -2147482624 0 0 0 0 0 0 0 0 0 0 233472 1869045599 48 1868783455 5312 1633967967 3480 1818320735 304 1919115103 0 1852796269 0 115 0 72 0 1227 0 0
The first 11 zeros are as they should be then it all goes wrong. What's going on?
You're accessing memory outside the array.
Your x variable iterates from 0 to 99.
for (int x=0;x<100;x++)
But your array has only 10 rows.
int GameScoresHome[10][10];
One fix would be to use simple nested loops.
for (int x=0;x<10;x++) {
for (int y=0;y<10;y++) {
printf("%d ",game.GameScoresHome[x][y]);
}
}
Nested loops would also make it easy to include a newline after each row.
for (int x=0;x<10;x++) {
for (int y=0;y<10;y++) {
printf("%d ",game.GameScoresHome[x][y]);
}
printf("\n");
}
In your code
for (int x=0;x<100;x++) {
int y = floor(x/10);
printf("%d ",game.GameScoresHome[x][y]);
}
the x value is going out of bound. Maybe you want to take a %10 value of x and change the index locations, like
printf("%d ",game.GameScoresHome[y][x%10]);
or, use a nested loop to maintain two indexes separately.
What about?
struct Game game = {.GameScoresHome = {0}};
for (int x=0;x<10;x++) {
for (int y=0;y<10;y++) {
printf("%d ",game.GameScoresHome[x][y]);
}
printf("\n");
}
Write the loop simpler
for ( int x = 0; x < 100; x++) {
printf("%d ",game.GameScoresHome[x / 10][x % 10]);
}
or
for ( int x = 0; x < 100; x++) {
printf("%d ",game.GameScoresHome[x / 10][x % 10]);
if ( x % 10 == 9 ) printf( "\n" );
}
As for your loop then x is being changed from 0 up to 100 and as result using x as the first index in the statement
printf("%d ",game.GameScoresHome[x][y]);
^^^
is invalid. And y is calculated incorrectly.
this program is "calculating" all subsets of the array source. I need to store the resulting values in another 2D filed named polje. If I just use the printf("%d %d %d ", source[i][0], source[i][1], source[i][2]); the code works fine but something fails when it is trying to copy everything into the resulting field. I suppose I am dogin something wrong in the indexing of the array polje.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
int f;
int i,j;
int source[2][3] = {{0,3,5},{3,4,2}};
int currentSubset = 3;
int polje[8][3];
for(i=0;i<8;i++){
for(j=0;j<3;j++){
polje[i][j]=0;
}}
int tmp;
while(currentSubset)
{
tmp = currentSubset;
for( i = 0; i<3; i++)
{
if (tmp & 1)
{
printf("%d %d %d ", source[i][0], source[i][1], source[i][2]); //writes out everything I want
polje[currentSubset][0]=source[i][0];
polje[currentSubset][1]=source[i][1];
polje[currentSubset][2]=source[i][2];
}
tmp >>= 1;
}
printf("\n");
currentSubset--;
}
for(i=0;i<8;i++){
for(j=0;j<3;j++){
printf("%d ", polje[i][j]);
}printf("\n");}
return (EXIT_SUCCESS);
}
The output field should be:
0 3 5
3 4 2
3 4 2
0 0 0
0 3 5
0 0 0
0 0 0
0 0 0
But instead it is:
0 3 5
3 4 2
3 4 2
0 0 0
*0 0 0*
0 0 0
0 0 0
0 0 0
tmp is a bit mask with only two bits, so the inner loop should be for ( i = 0; i < 2; i++ ).
Also the correct index into the polje array is polje[currentSubset * 2 + i][0] since each subset in polje takes two spaces and i is either 0 or 1.
I think you just have a logic error. Your loop's skeleton is:
currentSubset = 3;
while ( currentSubset )
{
// ...
polje[currentSubset][...] = ...;
// ...
currentSubset--;
}
So you never write to any rows except the first three.