my code keeps stopping as ENTERED CHECK ROWS can some one tell me why? int main(
ID: 3662747 • Letter: M
Question
my code keeps stopping as ENTERED CHECK ROWS can some one tell me why?
int main(){
int n,R,count = 0;
int RESULT[n];//YES OR NO ARRAY
int** game;//GAME ANSWER
scanf("%d", &n);//NUMBER OF GAMES
printf("DEBUG 1 %d ", n);
while(count < n){//FOR EVERY GAME
printf("DEBUG 2 ");
game = makeapuzzle();//ALLOCATE THE RESPONSE
printf("DEBUG FINISHED MAKEAPUZZLE ");
R = checkrows(game);//CHECK THE ROW
printf(" FINISHED CHECK ROWS ");
//other stuff goes here
RESULT[count]=R;//RECORD RESPONSE
printf(" ENTERED R ");
}
//print-results here
printf(" FREE ");
free(game);
printf(" FREE2 ");
return 0;
}
// Returns a ptr to a 2D array of size rows x cols.
int** makeapuzzle() {
printf("DEBUG 4 ");
// Allocate top level of pointers.
int** array = malloc(sizeof(int*)*row);
int i,k, j;
printf("DEBUG 5 ");
// Allocate each individual array,
// BUT INITILIZE IT
for (i=0; i<row; i++) {
printf(" DEBUG 6 ");
array[i] = malloc(sizeof(int)*column);
for (j=0; j<column; j++){
printf(" DEBUG 7 ");
scanf("%d",&k) ;
array[i][j]=k;}
}
printf(" ");
// Return pointer to newly created array.
return array;
}
int checkrows(int** game){//CHECKS ROWS
printf(" ENTERED CHECK ROWS ");
int i,k,R=0,N,count=1;
while(count != row){//TILL WE CHECK EVERY ROW
int FrAr[9] = {0,0,0,0,0,0,0,0,0};//FREQUENCY ARRAY
for(i=0;i<column;i++){//FOR EVERY ELEMENT IN ROW
N = game[count][i];//TAKE THE INTEGER
FrAr[N-1]++;//COUNT IT
}
for(i=0;i<column;i++){//FOR EVERY ELEMENT
k = FrAr[i];
if(k != 1){//IF ITS REPEATED OR NOT THERE
R=1;//THE PUZZLE IS WRONG
break;
}
}
}
return R;//RETURN RESULTS
}//CHECKROWS
Explanation / Answer
There are few mistakes in your code:
RESULTS array declared before readin n.
count variable is not increased whenever its used in while loop;
#include <stdio.h>
int row = 2;
int column = 2;
int** makeapuzzle();
int checkrows(int** game);
int main(){
int n,R,count = 0;
int** game;//GAME ANSWER
printf(" Enter no of games:");
scanf("%d", &n);//NUMBER OF GAMES
int RESULT[n];//YES OR NO ARRAY
printf(" DEBUG 1 %d ", n);
while(count < n){//FOR EVERY GAME
printf("DEBUG 2 ");
game = makeapuzzle();//ALLOCATE THE RESPONSE
printf("DEBUG FINISHED MAKEAPUZZLE ");
R = checkrows(game);//CHECK THE ROW
printf(" FINISHED CHECK ROWS ");
//other stuff goes here
RESULT[count]=R;//RECORD RESPONSE
printf(" ENTERED R ");
count++;
}
//print-results here
printf(" FREE ");
free(game);
printf(" FREE2 ");
return 0;
}
// Returns a ptr to a 2D array of size rows x cols.
int** makeapuzzle() {
printf("DEBUG 4 ");
// Allocate top level of pointers.
int** array = malloc(sizeof(int*)*row);
int i,k=0, j;
printf("DEBUG 5 ");
// Allocate each individual array,
// BUT INITILIZE IT
for (i=0; i<row; i++) {
printf(" DEBUG 6 ");
array[i] = malloc(sizeof(int)*column);
for (j=0; j<column; j++){
printf(" DEBUG 7 ");
scanf("%d",&k) ;
array[i][j]=k;}
}
printf(" ");
// Return pointer to newly created array.
return array;
}
int checkrows(int** game){//CHECKS ROWS
printf(" ENTERED CHECK ROWS ");
int i,k,R=0,N,count=1;
while(count != row){//TILL WE CHECK EVERY ROW
int FrAr[9] = {0,0,0,0,0,0,0,0,0};//FREQUENCY ARRAY
for(i=0;i<column;i++){//FOR EVERY ELEMENT IN ROW
N = game[count][i];//TAKE THE INTEGER
FrAr[N-1]++;//COUNT IT
}
for(i=0;i<column;i++){//FOR EVERY ELEMENT
k = FrAr[i];
if(k != 1){//IF ITS REPEATED OR NOT THERE
R=1;//THE PUZZLE IS WRONG
break;
}
}
count++;
}
return R;//RETURN RESULTS
}//CHECKROWS
--ouputp---------
1
1
2
3
4
Enter no of games:
DEBUG 1 1
DEBUG 2
DEBUG 4
DEBUG 5
DEBUG 6
DEBUG 7 DEBUG 7 DEBUG 6
DEBUG 7 DEBUG 7
DEBUG FINISHED MAKEAPUZZLE
ENTERED CHECK ROWS FINISHED CHECK ROWS ENTERED R FREE FREE2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.