- 1
why my FOR loop won't work
when i don't use the FOR loop in the comment, my code can work correctly... but when i use the FOR loop in the comment, my code won't do the loop correctly. is there anyone who can help me? TT the FOR loop in the comment is for looping the input for k (k in the scanf in the comment) times. and i used devc++ to make this code so i don't know if my code can work here https://code.sololearn.com/c1U2YHXlHQmm/?ref=app https://code.sololearn.com/c1U2YHXlHQmm/?ref=app https://code.sololearn.com/c1U2YHXlHQmm/?ref=app
6 odpowiedzi
+ 8
#include <stdio.h>
int main() {
int rows = 0, cols = 101;
//Inputting numbers of rows for array
scanf(" %d", &rows);
printf("Number of rows: %d\nNumber of columns: %d\n\n", rows, cols);
//Creating dynamic array with n number of rows
char **arr = (char *)malloc(rows * sizeof(char *));
//Declaring numbers of columns for each row of the array
for(int i = 0; i < rows; i++) {
arr[i] = (char *)malloc(cols * sizeof(char));
}
//Taking inputs in each line of the array
for(int i = 0; i < rows; i++) {
fgets(arr[i], cols, stdin);
}
//Outputting each line of array
for(int i = 0; i < rows; i++) {
printf("Line %d: %s\n", i+1, arr[i]);
}
//Deleting each row of the dynamic array
for(int i = 0; i < rows; i++) {
free(arr[i]);
}
//Deleting the array
free(arr);
return 0;
}
This code will create 2D dynamic array, then it will take inputs and then delete the array
+ 6
For some reason it isn't taking the input of first row, I don't know why you need to debug it. I am on android so I can't debug. Sorry for that. If you have any queries you can ask freely
+ 5
lines or characters?
because if you want to input lines then you have to create 2D array if you want to input each line separately
+ 4
What does this program meant to do?
- 1
i want to sort 3 operator in one line and before i input the operator i want to input how many line i want to input
- 1
i want to input lines thats why i use [\n]... i already try 2D array but the code become error and give me other input that i dont want