program in C! 4S. In main do the following: Declare a 2-dimensional integer arra
ID: 3721494 • Letter: P
Question
program in C!
4S. In main do the following: Declare a 2-dimensional integer array that has 10 rows and 7 columns Seed the random number generator with time Fill the 2D array with random numbers from the interval [-40, 80] Prompt the user to enter an integer between-40 and 80 (-40 and 80 included) After the user has entered the integer, use a while input validation loop to prompt the user to re-enter the integer if the integer is less than-40 or greater than 80 Call function print2DArray that prints the 2D array to the screen Call function lessThan NumAndOdd that will return the number of elements in the 2D array that are less than the integer the user entered and the number of elements in the 2D array that are odd. Print the number of elements in the 2D array that are less than the integer the user entered Print the number of elements in the 2D array that are odd Test by entering: 120 e e e . o . . 42 45 (You may do this problem entirely in main with a substantial deduction of points) Function print2DArray: This function receives a 2D integer array the number of rows and the number columns. This function has no return value. The function will print the 2D array to the screen Function lessThanNumAndOdd: This function receives a 2D integer array, the number of rows, the number columns, an integer (num) and two pointers to integers. The function will return the number of elements in the 2D array that are less than num using the first pointer and the number of elements in the 2D array that are odd using the second pointer.Explanation / Answer
Below is your code: -
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void print2DArray(int array[][7], int row, int col);
void lessThanNumAndOdd(int array[][7], int row, int col,int num, int *p, int *q);
int main(int argc, char* argv[]){
int array[10][7], num, lowerValue = -40, higherValue = 80;
srand( time(NULL) );
int i,j;
for(i = 0; i<10; i++){
for(j=0;j<7;j++){
array[i][j] = lowerValue + rand() % (( higherValue + 1 ) - lowerValue);
}
}
do{
printf("Enter a Number in the range [-40 and 80]: ");
scanf("%d",&num);
}while (num > 80 || num < -40);
print2DArray(array, 10, 7);
int p, q;
lessThanNumAndOdd(array, 10, 7, num, &p, &q);
printf("Number of Array Elements That are Less Than %d is: %d ",num,p);
printf("Number of Array Elements That are Odd is: %d ",q);
system("pause");
return 0;
}
void print2DArray(int array[][7], int row, int col){
printf("The 2D Array is: ");
int i,j;
for( i=0;i<row;i++){
for( j=0;j<col;j++)
printf("%d ",array[i][j]);
}
printf(" ");
}
void lessThanNumAndOdd(int array[][7], int row, int col,int num, int *p, int *q){
int lessThanNumCount = 0, oddCount = 0;
int i,j;
for (i=0;i<row;i++){
for(j=0;j<col;j++){
if(array[i][j] < num){
lessThanNumCount = lessThanNumCount + 1;
}
if (array[i][j] % 2 !=0){
oddCount = oddCount + 1;
}
}
}
*p = lessThanNumCount;
*q = oddCount;
}
Output
Enter a Number in the range [-40 and 80]: 120
Enter a Number in the range [-40 and 80]: -45
Enter a Number in the range [-40 and 80]: 45
The 2D Array is:
0 19 -4 37 -28 49 -6 12 48 25
25 68 -12 66 6 49 55 -18 34 38
59 5 36 40 40 -13 46 -37 -30 -35
24 27 49 -23 -23 -32 27 -5 -31 69
44 57 8 -22 13 32 31 -3 -37 23
-11 62 33 53 47 -36 76 80 70 56
-2 78 26 44 20 2 18 45 -25 75
Number of Array Elements That are Less Than 45 is: 49
Number of Array Elements That are Odd is: 33
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.