CODE:
#include <iostream>
using namespace std;
void MostFrequently(const int *nums,int size){
//int array to store the numbers in row 1 and their
//frequency in row 2
int freq[size][2];
//initializing the freq array
for(int i=0;i<size;i++){
freq[i][0] = 0;
freq[i][1] = 0;
}
//n is the number of unique numbers in the array
int n = 0;
//calculating the frequency of each number in the array
for(int i=0;i<size;i++){
int num = nums[i];
int f = 0;
for(int j = i;j<size;j++){
if(nums[j] == num){
f++;
}
}
//searching the freq array if num is present in it or not
int found = 0;
for(int k=0;k<n;k++){
if(num == freq[k][0]){
found = 1;
break;
}
}
//if it is not present already only then it will be added to
//the array freq to avoid duplication
if(found != 1){
freq[n][0] = nums[i];
freq[n][1] = f;
n++;
}
}
//sorting the freq array with respect to the frequency
for(int i=0;i<n;i++){
//sorting using selection sort in descending order
int max = freq[i][1];
int maxIndex = i;
for(int j=i;j<n;j++){
if(freq[j][1] >= max){
max = freq[j][1];
maxIndex = j;
}
}
//swapping the array numbers
int tempNum = freq[i][0];
int tempFreq = freq[i][1];
freq[i][0] = freq[maxIndex][0];
freq[i][1] = freq[maxIndex][1];
freq[maxIndex][0] = tempNum;
freq[maxIndex][1] = tempFreq;
}
//using the freq array
for(int i=0;i<n;i++){
//to print the numbers in the frequency times in descending
order
int num = freq[i][0];
for(int j=0;j<freq[i][1];j++)
cout<<num<<" ";
}
cout<<endl;
}
//main method
int main() {
//input array
int arr[] = {3,4,1,1,1};
//calling the function
cout<<"OUTPUT:"<<endl;
MostFrequently(arr,5);
return 0;
}
____________________________________________
CODE IMAGES:
__________________________________________________
OUTPUT:
________________________________________________
Feel free to ask any questions in the comments section
Thank You!
language: C++ creat a program that: Q5 10 Pts) output the numbers below using the one...
Question 5 10 pts Predict the output of the following C program: #include <stdio.h> void mainot double x[4] = (3.14, 1.62, 2.72,654.1); printf("%d %c %.f", (int)(100**(x+1)), (int)x[3]/10, x[2]); 12pt Paragraph BI UAB T2 : p O words
Consider the following C++code snippet and what is the output of this program? # include<iostream> using namespace std; void arraySome (int[), int, int); int main () const int arraysize = 10; int a[arraysize]-1,2,3,4,5, 6,7,8,9,10 cout << "The values in the array are:" << endl; arraySome (a, 0, arraySize) cout<< endl; system ("pause") return 0; void arraySome (int b[], int current, int size) if (current< size) arraySome (b, current+1, size); cout << b[current] <<""; a) Print the array values b) Double...
One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << ; cout << endl; return 0; }
Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...
Language C++ (Please include a short description & Screenshot of output) Implement a Priority queue using a SORTED list. Use Quick sort after adding a new node. Example of quick sort below. Adopt to your program the code below. #include <iostream> void quickSort(int a[ ], int first, int last); int pivot(int a[], int first, int last); void swap(int& a, int& b); void swapNoTemp(int& a, int& b); void print(int array[], const int& N); using namespace std; int main() { int test[]...
I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...
In C programming language: This program will output a right triangle based on user specified height triangleHeight and symbol triangleChar. (1) The given program outputs a fixed-height triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangleChar character. (1 pt) (2) Modify the program to use a nested loop to output a right triangle of height triangleHeight. The first line will have one user-specified character, such as % or *....
#include <iostream> using namespace std; - // Guess the output for the below program. // Assume address of a,b,c as 4004, 4008, 4016 respectively. int main() { float a = 12.5; int b = 10; double c = 3.1412; float aptr = &a; int *bptr = &b; double *cptr = &c; cout << "value of variable a,b,c:"<< a <<""<<b<<""<<<<<endl; cout << "Address of variable a,b,c:"<<&a<<""<<&b<<""<<&c << endl; cout << "value of aptr, bptr, cptr:" << aptr <<""<<bptr <<"" << cptr...
Concepts tested by the program: Working with one dimensional parallel arrays Use of functions Use of loops and conditional statements Description The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 – 9 exactly The sum of each row, each column and each diagonal all add up to the same number. s is shown below: Write a program that...
Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below : : Merge Sort algorithm using 2 processes a.) Define an integer array of 100 integers. Populate this array with random numbers. You can use int rand(void); function. Do not forget to initialize this function. You will sort the numbers in the array using merge-sort algorithm. In merge sort algorithm the half of the array will be sorted by one process and second...