How would I do the printtranspose function and the bonus function? I\'ve complet
ID: 3724640 • Letter: H
Question
How would I do the printtranspose function and the bonus function? I've completed the rest of the program. Thank you. weeks function. void printTranspose(int IIIMAXI, int): Takes the 2D Array and the size as the parameter and prints the contents of the array in a transposed manner, i.e prints the rows as columns and columns as rows. int main(void): Like the prelab, first read the size of the 2D array in the main if the size is0 or> 20 display an error message and ask for a new input from the user. Using this size to initialize the 2D array with the random numbers. Print the array content as shown in the sample output below. Use findZeros to display the number of zero's as shown in the sample output below. Use findEvenOdd to display the number of even and odd numbers in the 2D array. Call the loadDiagonal function by sending the 2D array, size and a 1D array initialized in main0. inside the function load the contents of the 1D array with the primary diagonal elements from the 2D array. Then call printIDArray function on the diagonal array you created to print its contents. PrintIDArray is the exact same as the print_array function from last week's lab. Next, call the printTranspose function on the 2D array to print the rows and columns in a transposed fashion. Sample Output Characters in bold are input from the user Enter the size of the 2-D array: -1 Please enter a value between 1-20 only: 21 Please enter a value between 1-20 only: 6 THE FIRST 2-D ARRAY is: 555011 779219 723672 894703 494074 641162Explanation / Answer
Hi, below is the working code as per your requirement. Since there is no restriction of what could the array elements, we need a dynamic solution to find the frequency of the array elements. Hence I had used hashmap. You need to have a basic understanding about hashmap functioning.
#include <bits/stdc++.h>
using namespace std;
void printTranspose(int size,int *a)
{
cout<<"The transpose of the matrix is: ";
for(int j=0;j<size;j++)
{
for(int i=0;i<size;i++)
{
cout<<*((a+i*size) + j)<<" ";
}
cout<<" ";
}
}
unordered_map<int, int> hm;
void countFreq(int a[], int n)
{
// Insert elements and their
// frequencies in hash map.
for (int i=0; i<n; i++)
hm[a[i]]++;
}
void findMaxNumber(int size,int *a)
{
int max,count;
cout<<"*****BONUS***** ";
for(int i=0;i<size;i++)
{
countFreq(a+i*size,size);
for(int j=0;j<size;j++)
{
max=-1;
count=0;
if(hm[*((a+i*size) + j)]>count)
{
count=hm[*((a+i*size) + j)];
max=*((a+i*size) + j);
}
}
cout<<"Row "<<i<<": -- "<<max<<" occurs "<<count<<" times. ";
hm.clear();
}
}
int main()
{
int size=3;
int a[][size]={{1,2,2},{4,5,4},{7,7,7}};
cout<<"The given matrix is : ";
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
cout<<a[i][j]<<" ";
}
cout<<" ";
}
printTranspose(size,(int *)a);
findMaxNumber(size,(int *)a);
}
OUTPUT : The code is compiled on GeeksforGeeks C++ IDE
The given matrix is :
1 2 2
4 5 4
7 7 7
The transpose of the matrix is:
1 4 7
2 5 7
2 4 7
*****BONUS*****
Row 0: -- 2 occurs 2 times.
Row 1: -- 4 occurs 2 times.
Row 2: -- 7 occurs 3 times.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.