Reputation: 71
This code is supposed to print out a square like this without the blank line between them. I am trying to use the command line argument to determine the size of the square. It seems to just no read in the command line argument and just counts from 1 until I hit CTRL-C to stop it.
012345
123456
234567
345678
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int row;
int colm;
// Check for a command line argument.
if(argc < 2){
printf("usage: box size");
}
else{
int n = atoi(argv[1]);
for (row = 0; row <= n; row++){
n = row;
for (colm = 0; colm <= n; colm++){
printf("%d ",n);
n++;
}
printf("\n");
}
}
return 0;
}
Upvotes: 0
Views: 254
Reputation: 75062
Doing n++;
in the loop for (colm = 0; colm <= n; colm++){
makes the upperbound escape from being reached and the loop will continue until overflow happens.
What you should do is printing the sum of row
and colm
without changing n
.
for (row = 0; row <= n; row++){
for (colm = 0; colm <= n; colm++){
printf("%d ",row + colm);
}
printf("\n");
}
Upvotes: 1