Question

In: Computer Science

Write a C function that finds and displays the maximum value ina two-dimensional array of...

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 the address of the array should be passed to the function. Modify the function so that it also displays the rows and columns number of the element with the maximum value

Solutions

Expert Solution

#include

void printMax(int arr[10][20]){
   int i,j,maxIndexI = 0,maxIndexJ = 0,maxVal = arr[0][0];
   for(i = 0;i<10;i++){
       for(j = 0;j<20;j++){
           if(maxVal<=arr[i][j]){
               maxIndexI = i;
               maxIndexJ = j;
               maxVal = arr[i][j];
           }
       }
   }
   printf("Max value is %d at position (%d,%d)\n",maxVal,maxIndexI,maxIndexJ);
}

int main() {
   int a[10][20];
   int i,j;
   printf("Enter array a:\n");
   for(i = 0;i<10;i++){
       for(j =0;j<20;j++){
           scanf("%d",&a[i][j]);
       }
   }
   printf("\n");
   printMax(a);
   return 0;
}

21 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1 2 13 4 5 6 7 8 49 0 1 2 3 54 5 6 7 8 9 0
1 2 3 14 5 6 7 8 9 06 1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 38 9 0 1 2 3 4 5 56 7 8 9 0
1 2 23 4 15 6 7 8 9 0 1 2 3 4 5 6 7 85 9 0
1 2 3 4 5 6 7 8 9 0 11 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0 1 2 3 4 55 6 7 8 9 0
1 2 33 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0


Related Solutions

Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and...
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and returns true if the matrix is an upper matrix and returns false otherwise. Remark: A matrix M is an upper matrix if it is a square matrix (row size = column size) and every element M[i][j] = 0 for all i > j. That is all the elements of the matrix below the main diagonal are zero.
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Write a C program that selects and displays the maximum value of five numbers to be...
Write a C program that selects and displays the maximum value of five numbers to be entered when the program is executed. (Hint : Use a for loop with both a scan_s and if statement inside the loop.)
c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the travellingpointer(1stversion) notation to traverse the array. The function has the following prototype. int maximum ( int *p [ ], int n); 2. Implement the function psum( )that is passed an array of n floats and returns a pointer to the sum of such an array. Print the...
C++ Add a function that finds & displays all the words that begin with the letter...
C++ Add a function that finds & displays all the words that begin with the letter 's'. The text file "dickens.txt" is as follows: It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was...
Q in c++ Write a program that will find the inverse of any two dimensional array...
Q in c++ Write a program that will find the inverse of any two dimensional array without the usage of the built-in function.? plz answer in while or do while loop
(C++ only please) Write a function called maximum that takes an array of double values as...
(C++ only please) Write a function called maximum that takes an array of double values as a parameter and returns the largest value in the array. The length of the array will also be passed as a parameter. (Note that the contents of the array should NOT be modified.) Write a function called printReverse that takes an array of characters and the length of the array as parameters. It should print the elements of the array in reverse order. The...
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(____________________________) { }
Write program that pass unsorted one dimensional array and a key to function, the function will...
Write program that pass unsorted one dimensional array and a key to function, the function will search for the key(using linear search) and if it is found the function will sort the array from the beginning to the position of the key, and f it is not found the function will sort all the array Note: please solve it by c , not c++
in C++ In the main function, declare a two-dimensional matrix array 5 by 5, read data...
in C++ In the main function, declare a two-dimensional matrix array 5 by 5, read data from a file named “data.txt”. Suppose the data in the file are integers between 0 and 9. Write your main function to print out the left bottom below the diagonal of the matrix and keep the triangle shape. Note the numbers on the diagonal are not included in the output. An example is given as follows. (25 points) Suppose the original matrix is 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT