Having trouble with this program running, can anybody help solve my problem:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void draw_x(int width, int height);
int main(int argc, char *argv[]){
int width, height;
if(argc != 3){
printf("Syntax: this_program width height\n");
exit(1);
}
width = atoi(argv[1]);
height = atoi(argv[2]);
// make sure they're both positive non-zeros
if(height > 0 && width > 0){
draw_x(width, height);
}else{
printf("numbers must be naturals\n");
exit(1);
}
return 0;
}
void draw_x(int width, int height){
int tally = 0;
int drawx, drawy;
char grid[height][width + 1];
memset(grid, ' ', height * (width + 1) * sizeof(char));
if(width > height){
drawy = 0;
for(drawx = 0; drawx < width; drawx++){
tally += height;
if(tally > width){
tally -= width;
drawy++;
}
grid[drawy][drawx] = '*';
grid[drawy][width - drawx - 1] = '*';
}
}else{
drawx = 0;
for(drawy = 0; drawy < height; drawy++){
tally += width;
if(tally > height){
tally -= height;
drawx++;
}
grid[drawy][drawx] = '*';
grid[drawy][width - drawx - 1] = '*';
}
}
for(drawy = 0; drawy < height; drawy++){
grid[drawy][width] = 0;
printf("%s\n", grid[drawy]);
}
}
1 answer
The posted program compiles (Dev C++ V4.9.9.2) and executes without any compilation errors, nor execution errors within the few sets of random natural numbers that I supplied. It draws a cross with the given width and lengths.
However, since the original programming requirements have not been posted, it is not possible to judge if the output is correct.
If the problem persists, please post the supplementary information as suggested above.