I need a C PROGRAM that uses simple functions that initializes a 2D array with r
ID: 3544377 • Letter: I
Question
I need a C PROGRAM that uses simple functions that initializes a 2D array with random numbers (between 0-100) and displays the content of the 2D array. Read the size of the input from the user. If user provides a number less than zero or greater than 100 display an error message and ask for new input from the user do this until user provides with the valid input. Use check_error function to perform the error checking.
Function prototypes (5):
int check_error(int): This function takes an integer number and checks if the number is between 0-100 or not if it is it returns 1 otherwise 0.
void initialize_2Darray(int [][MAX], int):
This function takes an integer number which is the size of the input and initialize the global 2D array with random numbers between 0-100.
int smallest_number(int [][MAX], int):
This function finds the smallest number present in the 2D array and returns that number.
void print_2Darray(int [][MAX], int);This function prints the content of the 2D array as shown in the sample output below.
main():
Call the above functions in the main. Implementation should generate the output as shown in the sample output below.
Enter the size of the array: -1
Invalid input enter the size of the array again: 111
Invalid input enter the size of the array again: -35
Invalid input enter the size of the array again: 0
Invalid input enter the size of the array again: 5
Input 2D array
34 71 79 86 12
50 14 84 39 49
19 48 96 5 54
79 64 84 63 39
22 64 85 82 65
The smallest number in the array is 5
Explanation / Answer
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 100
int array[MAX][MAX];
//This function takes an integer number and checks if the number
//is between 0-100 or not if it is it returns 1 otherwise 0.
int check_error(int k)
{
if(k<=0 || k>=100) return 0;
return 1;
}
void initialize_2Darray(int array[][MAX], int size)
// This function takes an integer number which is the size of the input and
// initialize the global 2D array with random numbers between 0-100.
{
int i,j;
for(i=0; i<size; i++)
{
for(j=0; j<size; j++)
{
array[i][j] = rand()%101;
}
}
}
//This function finds the smallest number present in the 2D array and returns that number.
int smallest_number(int array[][MAX], int size)
{
int small = array[0][0];
int i,j;
for(i=0; i<size; i++)
{
for(j=0; j<size; j++)
{
if(array[i][j]<small) small = array[i][j];
}
}
return small;
}
void print_2Darray(int array[][MAX], int size)
//This function prints the content of the 2D array as shown in the sample output below
{
int i,j;
printf(" Input 2D array ");
for(i=0; i<size; i++)
{
for(j=0; j<size; j++)
{
printf("%2d ",array[i][j]);
}
printf(" ");
}
}
int main()
{
int size;
srand (time(NULL));
printf("Enter the size of the array: ");
scanf("%d",&size);
while(!(check_error(size)))
{
printf(" Invalid input enter the size of the array again: ");
scanf("%d",&size);
}
initialize_2Darray(array,size);
print_2Darray(array,size);
printf("The smallest number in the array is %d",smallest_number(array,size));
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.