Question

In: Computer Science

Define an array named PeopleTypes that can store a maximum of 50 integer values that will be entered at the keyboard.


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.


Solutions

Expert Solution

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:


Related Solutions

Write a C program that allows: Three integer values to be entered (read) from the keyboard,...
Write a C program that allows: Three integer values to be entered (read) from the keyboard, Display the sum of the three values on the computer screen as follows: The integers that you have entered are: a b c The sum of a , b & c is ______ Thank you! C Code: Output screen:
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable...
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable (like ptr) which points to this array. Use this pointer variable to initialize the myNums array from 2 to 200 and then display the array elements. Delete the dynamic array myNums at the end. You just need to write part of the program.
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
write a python program that inputs 10 integer values from the keyboard and then displays their...
write a python program that inputs 10 integer values from the keyboard and then displays their sum. use for loop
Java programming language Array - Single identifier which can store multiple values Example initialized with values:...
Java programming language Array - Single identifier which can store multiple values Example initialized with values: int[] mySimpleArray = {4, 17, 99}; Example with fixed length but no values: int[] myFixedArray = new int[11]; The brackets identify the index. Each value has its own index number. NOTE: First element is the zeroth element: mySimpleArray[0] is 4, [1] is 17 Make a new Netbeans project called ArraysAndLoops Create the simple array with 4, 17, 99. Use System.out.println with the variable with...
Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
Suppose you are provided with an array of integer values. Write pseudocode for a program to...
Suppose you are provided with an array of integer values. Write pseudocode for a program to determine how many unique values are in the array. Your solution should require time that is linearithmic in the size of the array. (For example, if the array contains the values {1,3,4,1,2}, the answer is "4", because the array contains the values 1, 2, 3, and 4)
Write a method with the following header to return an array of integer values which are...
Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if the 2D array passed to the method is: 8 5 5 6 6 3 6 5 4 5 2 5 4 5 2 8 8 5 1 6 The method returns an array of integers which are the largest values from each row...
Finding duplicate values of an array of integer values. Example 1 Input: arr[] = {5, 2,...
Finding duplicate values of an array of integer values. Example 1 Input: arr[] = {5, 2, 7, 7, 5}, N = 5 Output: Duplicate Element: 5 Duplicate Element: 7 Example 2 Input: arr[] = {1, 2, 5, 5, 6, 6, 7, 2}, N = 8 Output: Duplicate Element: 2 Duplicate Element: 5 Duplicate Element: 6 CODE:: class Main {    public static void FindDuplicate(int[] arr){ // write your code to find duplicates here and print those values    }   ...
Write a function maxFun() that returns the maximum value in any given integer array. Determine the...
Write a function maxFun() that returns the maximum value in any given integer array. Determine the function parameters (complete the area shown in ___________. ___________maxFun(____________________________) { }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT