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

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...
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    }   ...
The Encrypt class has an integer private data member 8 element array named digits. The first four elements (0 ~ 3) are to store the original 4 digits integer and the next four (4 ~ 7) are to store the encrypted data
C++ Please Modify the code and create a new header fileA company wants to transmit data over the telephone, but is concerned that its phones could be tapped. All of the data are transmitted as four-digit integers. The company has asked you to write a program that encrypts the data so that it can be transmitted more securely. Your program should read a four-digit integer and encrypt it as follows: Replace each digit by (the sum of that digit plus...
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
Complete the class code below and include a method named partition which takes an integer array...
Complete the class code below and include a method named partition which takes an integer array and a partition integer as parameters and then partitions the original array into two new arrays where elements of the first array are all less than or equal to the partition integer and the second array are all greater than the partition integer. The method should print the original array, followed by the lower partition and finally the upper partition. Nothing should be returned...
2. Define a function max_n(arr, n) that takes in an array and an integer as arguments....
2. Define a function max_n(arr, n) that takes in an array and an integer as arguments. Your function will then return the n largest values from that array as an array containing n elements. It is safe to assume that arr will have at least n elements. The resulting array should have the largest number on the end and the smallest number at the beginning. For Example: max_n(np.array([1,2,3,4,5]), 3) returns np.array([3,4,5]) max_n(np.array([10,9,8,7,6,5]), 4) returns np.array([7,8,9,10]) max_n(np.array([1,1,1]), 2) returns np.array([1,1])
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT