C++ Program In this project you need to summarize an array. Summary of an (data)
ID: 675323 • Letter: C
Question
C++ Program
In this project you need to summarize an array. Summary of an (data) array means that printing all the items and quantity of a specific items found in the array. The summary of the array should be kept in another 1D array. Summary array should keep the items of data arrays in order. For instance, we are given an array with 10 items,
then summary of these array should be another array of
Notice that in the summary array [2*n] shows the item found in the data array and [2*n+1] shows the quantity of [2*n+1], such as [0]=3, and [1] =4, how many times 3 found in the data array. Note that the size of summary array is not known.
In your program, you will create an data array with the size 100 and fill out with the random numbers between 10 and 50 (including 10 and 50). Then build your summary array based on data array and print out summary array.
Explanation / Answer
#include<iostream>
#include<time.h>
using namespace std;
#define LEN (100)
int main() {
srand(time(NULL));
int dataArr[LEN], hashMap[51];
int summary[LEN];
for (int i=0; i<51; i++) {
hashMap[i] = 0;
}
for (int i=0; i<LEN; i++) {
int r;
while ((r = rand() % 51) < 10);
dataArr[i] = r;
}
cout<<" ";
for (int i=0; i<LEN; i++) {
hashMap[dataArr[i]] ++;
}
for (int i=0; i<LEN; i++) {
summary[i] = hashMap[dataArr[i]];
}
cout<<"Summary array: ";
for (int i=0; i<LEN; i++) {
cout<<summary[i]<<", ";
}
cout<<" ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.