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
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...
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...
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])...
Initialize and Print an Array Write a program that accepts two integer values, called "arraySize" and...
Initialize and Print an Array Write a program that accepts two integer values, called "arraySize" and "multiplier", as user input. Create an array of integers with arraySize elements. Set each array element to the value i*multiplier, where i is the element's index. Next create two functions, called PrintForward() and PrintBackward(), that each accept two parameters: (a) the array to print, (b) the size of the array. The PrintForward() function should print each integer in the array, beginning with index 0....
Write the code to create an array named movies and store three of your favorite movies...
Write the code to create an array named movies and store three of your favorite movies in the array. Only provide the array and code needed to put the movie names in the array. Do not include additional code
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT