Reputation: 75
I'm new to C++ so bear with me.
I am trying to create a histogram from certain parameters (interval size, length of array containing quantities of numbers, highest number yadayada).
The details are irrelevant and a problem for myself to fiddle with, although I think I got the correct formula in my function.
When I assign variables from the C++ IO "cin" I can output those with the "cout" call, however, when I call my histogram function, also containing "cout" instructions, nothing gets printed.
My code:
#include <iostream>
#include <cmath>
using namespace std;
void histogram(int l, int n, int k, int *a)
{
int quantity = 0;
for (int i = 1; i <= l; i++)
{
for (int j = 0; i < n; j++)
{
if (a[j] >= (i-1) * k || a[j] <= i * k)
{
quantity++;
}
}
cout << (i-1) * k + ": " + quantity << endl;
quantity = 0;
}
}
int main()
{
int l,n,k;
int *a;
a = new int[n];
cin >> l >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
k = ceil((double)a[0]/l);
// cout << k;
histogram(l,n,k,a);
return 0;
}
Upvotes: 0
Views: 1993
Reputation: 167
There might be something funky going on with this line and string concatenation:
cout << (i-1) * k + ": " + quantity << endl;
You might try rewriting as cout << ((i-1) * k) << ": " << quantity << endl;
just to ensure that things are adding and concatenating correctly.
Upvotes: 1