#include <stdio.h> #include <stdlib.h> int** make_zeros(int rows, int cols){ return 0; }…
Private research annotation
Sign in to add a private research label or note.
- User
#include <stdio.h> #include <stdlib.h>
int** make_zeros(int rows, int cols){ return 0; }
int main(){ int rows; int cols; printf("\nEnter the number of columns: "); while(scanf("%d", &cols) != 1){
codeprintf("\nTry again: "); } printf("\nEnter the number of rows: "); return 0;}
how to fix when I enter a character the whileloop repeats infinitely'
- Assistant
You can fix the issue by clearing the input buffer after receiving a non-integer input. You can do this by using a loop to consume all characters in the input buffer until a new line character '\n' is encountered. Here's the updated
mainfunction with this fix:cint main(){ int rows; int cols; char c; printf("\nEnter the number of columns: "); while(scanf("%d", &cols) != 1){ while((c = getchar()) != '\n' && c != EOF); // Clear input buffer printf("\nTry again: "); } printf("\nEnter the number of rows: "); while(scanf("%d", &rows) != 1){ while((c = getchar()) != '\n' && c != EOF); // Clear input buffer printf("\nTry again: "); } return 0; }With this update, the code will clear the input buffer after receiving a non-integer input, allowing the user to retry without getting stuck in an infinite loop.
- User
#include <stdio.h> #include <stdlib.h>
int** make_zeros(int rows, int cols){ return 0; }
int main(){ int rows; int cols; printf("\nEnter the number of columns: "); while(scanf("%d", &cols) != 1){ printf("\nTry again: "); } printf("\nEnter the number of rows: "); while(scanf("%d", &rows) != 1){ printf("\nTry again: "); }
codeint* row_indexes = malloc(sizeof(int*) * rows); for(int i = 0; i < rows; i++){ row_indexes[i] = malloc(sizeof(int)*cols); } for(int i = 0; i < rows; i++){ for(int j = 0; i < cols; i++){ row_indexes[i] } } // define an array to store the indexes of all the rows // allocate memory for each row, and store the allocated memory's address in the row return 0;}
how to fix this
- Assistant
There are a few issues in your code that need to be addressed:
-
In the nested loop where you are allocating memory for each row, the loop condition
for(int j = 0; i < cols; i++)is usingiinstead ofjfor the inner loop. It should befor(int j = 0; j < cols; j++). -
The line
row_indexes[i]in the nested loop does not have any assignment operation. You need to assign a value to each element of the array.
-
- User
#include <stdio.h> #include <stdlib.h>
int** make_zeros(int rows, int cols){ return 0; }
int main(){ int rows; int cols; printf("\nEnter the number of columns: "); while(scanf("%d", &cols) != 1){ printf("\nTry again: "); } printf("\nEnter the number of rows: "); while(scanf("%d", &rows) != 1){ printf("\nTry again: "); }
codeint* row_indexes = malloc(sizeof(int*) * rows); int** two_d_array = &row_indexes; for(int i = 0; i < rows; i++){ row_indexes[i] = malloc(sizeof(int) * cols); } for(int i = 0; i < rows; i++){ for(int j = 0; i < cols; i++){ two_d_array[i][j] = 0; } } for(int i = 0; i < rows; i++){ for(int j = 0; i < cols; i++){ printf("%d", two_d_array[i][j]); } } return 0;}
// why do I get segmentation fault
- Assistant
There are multiple issues in your code that need to be addressed. Below are the changes required to fix the segmentation fault and other errors:
- In the
make_zerosfunction, you need to allocate memory properly for a 2D array and initialize it with zeros. Here's the correctedmake_zerosfunction:
cint** make_zeros(int rows, int cols){ int** arr = (int**)malloc(sizeof(int*) * rows); for(int i = 0; i < rows; i++){ arr[i] = (int*)malloc(sizeof(int) * cols); for(int j = 0; j < cols; j++){ arr[i][j] = 0; } } return arr; } - In the