Reputation:
I am writing a code that generates a random array length of 10 filled with random integers between 1 and 20 using a function to initialize it. Then it will print out a histogram of asterisks of the amount times the number is printed in the array.
Here is an example:
Element Value Histogram
0 12 ***********
1 5 ******
2 3 ***
etc...
The problem is that I do not know how to get the code to print out the number of asterisks and how to correlate them with the number of times an integer has appeared. What I have tried is several for loops for going through the array and the values to find out which values have been created. Then there is another for loop that counts the number of times the integer has occurred. I am aware that there is an error in the last court statement. I am unsure of how to put that part into the "graph". What I am certain of is that there should be a for loop for printing out the asterisks within another for loop checking out the numbers of the array.
This is my code:
#include <iostream>
#include <iomanip>
#include<ctime>
using namespace std;
void initialize(int arr[]);
int main(){
int arr[10];
initialize(arr);
cout<<endl;
cout<< left <<setw(10)<< left << "Element"<< right<< setw(10) << "Value" << right<<setw(20)<<"Histogram "<< right<<endl;
cout<<" " << endl;
int x;
for(int i = 0; i < 10; i++){
for(x = 1; x <= 20; x++){
if(x != arr[i])
x++;
}
for(int i = 0; i < x; i++){
cout<<"*";
}
//exuse the error message, I know it is inncorrect
cout<< left <<setw(10)<< left << i << right<< setw(10) << x << right<<setw(20)<<for(int i = 0; i < x; i++){
cout<<"*";
}<< right<<endl;
}
return 0;
}
void initialize(int arr[]){
for(int i = 0; i < 10; i++){
arr[i] = rand() % (20) + 1;
}
for(int i = 0; i < 10; i++){
cout<< arr[i]<<" ";
}
}
Upvotes: 0
Views: 468
Reputation: 6326
Your code is a c style program, which is error-prone, I have tried my best to achieve your requirement
#include <ctime>
#include <iomanip>
#include <iostream>
using namespace std;
void initialize(int arr[]);
const size_t kArrayNum = 10;
const size_t kMaxVal = 21;
int main() {
int arr[kArrayNum];
srand(time(nullptr));
initialize(arr);
cout << endl;
cout << left << setw(10) << left << "Element" << right << setw(10) << "Value"
<< right << setw(20) << "Histogram " << right << endl;
cout << " " << endl;
int cnt[kMaxVal] = {0};
for (int i = 0; i < kArrayNum; i++) {
cnt[arr[i]]++;
}
for (int i = 0; i < kMaxVal; ++i) {
int x = cnt[i];
// exuse the error message, I know it is inncorrect
cout << left << setw(10) << left << i << right << setw(10) << x << right
<< setw(20);
for (int i = 0; i < x; i++) {
cout << "*";
}
cout << right << endl;
}
return 0;
}
void initialize(int arr[]) {
for (int i = 0; i < kArrayNum; i++) {
arr[i] = rand() % (kMaxVal);
}
for (int i = 0; i < kArrayNum; i++) {
cout << arr[i] << " ";
}
}
I strongly encourage that rewriting it with a c++ STL container like vector
.
An example with more c++ style code:
#include <algorithm>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
std::vector<int> initialize();
const size_t kArrayNum = 10;
const size_t kMaxVal = 21;
int main(int argc, char* argv[]) {
srand(time(nullptr));
vector<int> arr = initialize();
cout << endl;
cout << left << setw(10) << left << "Element" << right << setw(10) << "Value"
<< right << setw(20) << "Histogram " << right << endl;
cout << " " << endl;
std::vector<size_t> cnt(kMaxVal, 0);
for (int i = 0; i < kArrayNum; i++) {
cnt[arr[i]]++;
}
for (int i = 0; i < kMaxVal; ++i) {
size_t x = cnt[i];
cout << left << setw(10) << left << i << right << setw(10) << x << right
<< setw(20);
cout << string(x, '*');
cout << right << endl;
}
return 0;
}
std::vector<int> initialize() {
std::vector<int> ans(kArrayNum, 0);
std::generate(std::begin(ans), std::end(ans),
[] { return rand() % kMaxVal; });
for (auto i : ans) {
cout << i << " ";
}
cout << endl;
return ans;
}
Or for production-level usage, I suggest having a look at boost::histogram or folly::histogram.
Upvotes: 1