IN C LANGUAGE. PLEASE READ CAREFULLY AND MAKE SURE TO USE C-STRINGS AND FGETS IN
ID: 3734661 • Letter: I
Question
IN C LANGUAGE. PLEASE READ CAREFULLY AND MAKE SURE TO USE C-STRINGS AND FGETS IN YOUR CODE. ALSO, COMMENST ARE APPRECIATED. THANKS.
* Write a program that will populate a 2d array of integer values using C-String, print out 2d array you just created, and then calculate some values of the 2d array.
* Your main should read in a min and max value for the values in the array as well as the number of rows and columns in the array.
* The code should make sure that the number of rows or number of columns requested is not larger than the 15x15 array and looping until values less than 15 are
* entered (10 points). You should have code in the main of your program that
* calls and display any results from the three functions in an appropriate manner
* The 2d array and any other information MUST be passed to the functions.
* No global variables should be used.
* The first function createGrid will populate the 2d array
* with integer values. The function will have arguments for the number of
* rows and number of columns in the 2d grid as well as the min and max
* for the values in the array. The function will then create the appropriate
* integer values for each entry in the 2d array, we do not need separate
* min/max for each row, a single min and a single max value will be used for all the rows.
* The next function, printGrid, prints the 2d array of numbers nicely as a table, making sure columns line up .
Explanation / Answer
C PROGRAM :
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int cols = 0;
// function to populate the array
void createGrid(int A[][cols],int
rows,int cols,int min,int max)
{
int i,j;
srand(time(NULL)); // to generate
random number different in every run
of code
int size = max - min+1;
for(i = 0;i < rows;i++){
for(j =0;j < cols;j++){
int temp = rand()%size;
temp = temp+min; // to generate
random number from min to max
A[i][j] = temp;
}
}
}
// function to print the 2d array
void print(int A[][cols],int rows,int cols)
{
int i,j;
for(i =0;i < rows;i++){
for(j = 0;j < cols;j++){
printf("%d ",A[i][j]);
}
printf(" ");
}
}
int main()
{
// to take min and max input form
user
int min,max;
printf("Enter the value of min: ");
scanf("%d",&min);
printf("Enter the value of max: ");
scanf("%d",&max);
// to take input rows and columns
form user
int row,column;
printf("Enter the value of rows: ");
scanf("%d",&row);
printf("Enter the value of columns: ");
scanf("%d",&column);
// to check whether rows and columns
in less than 15 or not
if(row <= 15 && column <= 15){
cols = column;
int A[row][column];
createGrid(A,row,column,min,max); //
function call to populate 2d array
print(A,row,column); //function call to
print the array
}
else{
printf("Rows and columns should be
less than 15");
}
return 0;
}