Code must be IN C PROGRAMMING. Please design a program that takes command line p
ID: 3571790 • Letter: C
Question
Code must be IN C PROGRAMMING.
Please design a program that takes command line parameters optionally to create a canvas of Number of row and collumns. Use the following requirements below!
1. Both rows and columns must be specified or both shouldn't be.
2. Your program should be able to run as either 1. ./maze.out numberRows num 2. ./maze.out
3. Number rows and columns have to be integers that are >= 1
4. In the event that there arent any argument inputed by the user or if the user makes an error in his/her inputs, then you should create a board that defaults to 20 x 20
HINT: You will want to use structs
THANKS in ADVANCE!!
Explanation / Answer
SOURCE CODE:
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[])
{
int i=0,j=0,val=0;
if(argc==3)
{
val=atoi(argv[2]);
if(val<1)
{
val=20;
}
int arr[val][val];
for(i=0;i<val;i++)
{
for(j=0;j<val;j++)
{
arr[i][j]=rand()%10;
printf("%d ",arr[i][j]);
}
printf(" ");
}
}
else
{
int arr[20][20];
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
arr[i][j]=rand()%10;
printf("%d ",arr[i][j]);
}
printf(" ");
}
}
}
SAMPLE OUTPUT:
$ gcc -o maze.c
$ ./maze.out numberRows -1 3 6 7 5 3 5 6 2 9 1 2 7 0 9 3 6 0 6 2 6 1 8 7 9 2 0 2 3 7 5 9 2 2 8 9 7 3 6 1 2 9 3 1 9 4 7 8 4 5 0 3 6 1 0 6 3 2 0 6 1 5 5 4 7 6 5 6 9 3 7 4 5 2 5 4 7 4 4 3 0 7 8 6 8 8 4 3 1 4 9 2 0 6 8 9 2 6 6 4 9 5 0 4 8 7 1 7 2 7 2 2 6 1 0 6 1 5 9 4 9 0 9 1 7 7 1 1 5 9 7 7 6 7 3 6 5 6 3 9 4 8 1 2 9 3 9 0 8 8 5 0 9 6 3 8 5 6 1 1 5 9 8 4 8 1 0 3 0 4 4 4 4 7 6 3 1 7 5 9 6 2 1 7 8 5 7 4 1 8 5 9 7 5 3 8 8 3 1 8 9 6 4 3 3 3 8 6 0 4 8 8 8 9 7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