Question
language: C++
creat a program that:

Q5 10 Pts) output the numbers below using the one that occurs must frequently first Most Frequently({3,4,1,1,1},5) => 1113 4
Q5 10 Pts) output the numbers below using the one that occurs must frequently first MostFrequently({3,4,1,1,1},5) => 11134 vo
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

- jhvhm.cpp - Code::Blocks 17.12 Eile Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Se- jhvhm.cpp - Code::Blocks 17.12 Eile Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Se- jhvhm.cpp - Code::Blocks 17.12 Eile Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Se
__________________________________________________

OUTPUT:

- х C:\Users\Win10\Documents\jhvhm.exe OUTPUT: 1 1 1 34 execution time : 0.141 s Process returned o (0x0) Press any key to co

________________________________________________

Feel free to ask any questions in the comments section

Thank You!

Add a comment
Know the answer?
Add Answer to:
language: C++ creat a program that: Q5 10 Pts) output the numbers below using the one...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 5 10 pts Predict the output of the following C program: #include <stdio.h> void mainot...

    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...

    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 () {...

    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; }

  • Can I get a C++ code and output for this program using classes instead of using...

    Can I get a C++ code and output for this program using classes instead of using struct. The following program implements a Last In First Out (LIFO) stack. ( I want to use class for function definitions too and if main need.) #include <iostream> using namespace std; const int MAX = 100; struct stack {    int s[MAX]; // an array of integers    int top; // the index of the last number }; void push(stack &, int); void pop(stack&,...

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    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...

    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...

    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...

    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...

    #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...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT