In: Computer Science
Define an array named PeopleTypes that can store a maximum of 50 integer values that will be entered at the keyboard. Enter a series of 1s, 2s,3s and 4s, into the array, where a represent an infant, a 2 represent a child, a 3 represents a teenager, and a 4 represents an adult who was present at a local school function
p.412 PE 4
Write a C function that finds and displays the maximum value in a two dimensional array of integers. The array should be declared as a 10-row-by-20-column array of integers in main (), and the starting address of the array should be passed to the function.
Modify the function written in Exercise 4a so that is also displays the row and column number of the element with the maximum value
Can the function you wrote for Exercise 4a be generalized to handle any size two dimensional array.
Part 1: Using an array named PeopleTypes . . .
#include int main() { int PeopleTypes[50]; for (int i = 0; i < 50; i++) { printf("Enter number %d: ", i + 1); scanf("%d", &PeopleTypes[i]); } return 0; }
Screenshot:
Part 2:Write a C function that finds and displays the maximum value in a two-dimensional array ...
Logic:
We define a global variable for the number of Columns and number of Rows since the question demands that we pass the starting address of the array to the function.
Our function then sets a variable named 'max' equal to the value of the starting element of the array. A nested-for-loop parses the 2d array, and compares every element to the max value, if it exceeds the max value, then the value of max is set to the value of that element. This program gets executed in O(n+m) where n = no. of rows, m= no. of columns.
Code:
#1: No. of rows = 10, no. of columns = 20, returns only the maximum value.
#include #define COLUMNS 2 #define ROWS 2 int maxOfArray2D(int (*array)[COLUMNS]) { int max = array[0][0]; int max_col = 0; int max_row = 0; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLUMNS; j++) { if (array[i][j] > max) { max = array[i][j]; max_col = j; max_row = i; } } } return max; } int main() { int numbers[ROWS][COLUMNS]; int i, j; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLUMNS; j++) { printf("Enter value for number at [%d],[%d]:", i, j); scanf("%d", &numbers[i][j]); } } int max = maxOfArray2D(numbers); printf("The max is %d", max); return 0; }
Screenshot:
#2: No. of rows = 10, no. of columns = 20, displays the maximum value, its row, and column number.
#include #define COLUMNS 2 #define ROWS 2 void printmaxOfArray2D(int (*array)[COLUMNS]) { int max = array[0][0]; int max_col = 0; int max_row = 0; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLUMNS; j++) { if (array[i][j] > max) { max = array[i][j]; max_col = j; max_row = i; } } } printf("maximum value = %d , row number = %d, column number = %d", max, max_row + 1, max_col + 1); } int main() { int numbers[ROWS][COLUMNS]; int i, j; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLUMNS; j++) { printf("Enter value for number at [%d],[%d]:", i, j); scanf("%d", &numbers[i][j]); } } printmaxOfArray2D(numbers); return 0; }
Screenshot:
(we simply used two extra variables to store the indices of the max value)
#3: generalized function to handle any size of 2d array:
#include int COLUMNS = 0; int ROWS = 0; void getrows(){ int rows; printf("Enter the number of rows: "); scanf("%d", &rows); ROWS = rows; } void getcolumns(){ int columns; printf("Enter the number of columns: "); scanf("%d", &columns); COLUMNS = columns; } void printmaxOfArray2D(int (*array)[COLUMNS]) { int max = array[0][0]; int max_col = 0; int max_row = 0; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLUMNS; j++) { if (array[i][j] > max) { max = array[i][j]; max_col = j; max_row = i; } } } printf("maximum value = %d , row number = %d, column number = %d", max, max_row + 1, max_col + 1); } int main() { getrows(); getcolumns(); int numbers[ROWS][COLUMNS]; int i, j; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLUMNS; j++) { printf("Enter value for number at [%d],[%d]:", i, j); scanf("%d", &numbers[i][j]); } } printmaxOfArray2D(numbers); return 0; }
Screenshot: