Reputation: 1
After printing the 2nd printf the code exit and says Run Failed and says exit on level 2 ( whatever the input was )
void main()
{
int testcases;
int n_lines ,m_length;
int test, lines, length;
char n_m_matrix[n_lines][m_length];
printf("Enter the no. of Test Cases ");
scanf("%d",&testcases);
printf("%d",testcases);
for(test=0;test>testcases;test++)
{
printf("Enter the lines and length of the test cases ");
scanf("%d%d",&n_lines,&m_length);
for(lines=0;lines<n_lines;lines++)
{
for(length=0;length<m_length;length++)
{
scanf("%c",&n_m_matrix[lines][length]);
}
}
}
}
Upvotes: 0
Views: 48
Reputation: 30936
To clear confusion among the serveral problem the first one which invokes Undefined behavior in the code is, using uninitialized variable as VLA's size.
Also the for
loop logic is wrong and character input might create some problem the way it is handled. (when entered with \n
).
Correct code would be (considering that you don't want the whitespaces inputted into the array. Because if you do then there are other better choices like fgets
etc).
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 1024
int main(void)
{
int testcases;
int n_lines ,m_length;
int test, lines, length;
printf("Enter the no. of Test Cases ");
if( scanf("%d",&testcases) != 1){
fprintf(stderr, "%s\n","Error in input" );
exit(1);
}
if( testcases <= 0 ){
fprintf(stderr,"%s\n","Enter nonzero integer for testcases");
exit(1);
}
printf("%d",testcases);
for(test = 0; test < testcases; test++)
{
printf("Enter the lines and length of the test cases ");
if(scanf("%d%d",&n_lines,&m_length)!=1){
fprintf(stderr, "%s\n","Error in input" );
exit(1);
}
if( n_lines <= 0 || m_length <= 0 || !(n_lines <= MAXSIZE && m_length <= MAXSIZE)){
fprintf(stderr, "%s\n","Error in input n_lines and m_length" );
exit(1);
}
char n_m_matrix[n_lines][m_length];
for(lines = 0; lines < n_lines; lines++)
{
for(length = 0; length < m_length; length++)
{
if( scanf(" %c",&n_m_matrix[lines][length]) != 1)
{
fprintf(stderr, "%s\n","Eror in input" );
exit(1);
}
}
}
// work with VLA here.
}
return 0;
}
Here if you need much larger array sizes than this go for dynamic memory allocation. That will satisfy larger memory requirement in array.
Upvotes: 2
Reputation: 782148
You need to move the declaration of n_m_matrix
into the loop so that it's after you input the variables that hold the dimensions.
As others mentioned, you also have a typo in test > testcases
, it should be <
.
And you should read an extra character after entering the dimensions, to read the newline. Otherwise it will leave the newline after the dimensions in the input buffer, and this will be read as the first %c
input when reading the contents.
You might also consider using fgets()
to read each line, instead of doing it character by character. The way you've written it, if there are newlines at the end of each line, they will be inserted into the array. It's not clear whether that's wanted. If it is, make sure you include them in the line length input.
int main()
{
int testcases;
int n_lines ,m_length;
int test, lines, length;
printf("Enter the no. of Test Cases ");
scanf("%d",&testcases);
printf("%d\n",testcases);
for(test=0; test < testcases; test++)
{
printf("Enter the lines and length of the test cases ");
scanf("%d%d",&n_lines,&m_length);
getc(); // Skip over the newline
char n_m_matrix[n_lines][m_length];
for(lines=0;lines<n_lines;lines++)
{
for(length=0;length<m_length;length++)
{
scanf("%c",&n_m_matrix[lines][length]);
}
}
}
}
Upvotes: 2
Reputation: 2440
You need to use dynamic memory allocation, malloc
method. The matrix n_m_matrix
dimension is dynamically decided based on user input.
n_m_matrix
using malloctest>testcase
to test<testcase
printf("Enter the lines and length of the test cases ");
scanf("%d%d",&n_lines,&m_length);
Upvotes: 0