Unholypalidn - 4 months ago 70

C++ Question

I am trying to use arrays from user input feeding into my histogram function and comparing the values in each instance of the array to have a counter to print out the number of "*" to show.

With the print of the histogram I am trying to divide it into bins i.e bin 9| *** for 3 instances of scores about 90, bin 8| ***** for 5 instances in the array of values 80 to 89 points and so on.

I have tried using pointer on bins[i] in my for loop however that wont work as well as I have tried &bins[i] but this only gives the address of 1 bin and does not display the subsequent bins or * from the counter in the array.

The following is the testing I am using to try and create this histogram. Case 1: 100, 95, 90, 85, 80, 75, 70, 65, 60, 40, 20, and 5. To make it shorter on code I will post my main and my function of Histogram. Thank you for your time.

`int main()`

{

int scores[100];

int bins[10];

int counter[99];

int count = 0;

cout << "Enter a score (-1 to stop): ";

do {

cin >> scores[count++];

} while (scores[count - 1] != -1);

count--;

Histogram(scores, count, counter);

int i;

for (i = 0; i < 10; i++)

;

{

cout << &bins[i] << "| " << endl;

for (size_t k = 0; k < counter[i]; k++) {

cout << "*" << endl;

break;

}

}

deviation(scores, count);

return 0;

}

int* Histogram(int scores[], int count, int counter[])

{

int k = 0;

for (int i = 0; i < count; i++) {

if (scores[i] >= 90) {

counter[k++];

}

else if (scores[i] >= 80 && scores[i] < 90) {

counter[k++];

}

else if (scores[i] >= 70 && scores[i] < 80) {

counter[k++];

}

else if (scores[i] >= 60 && scores[i] < 70) {

counter[k++];

}

else if (scores[i] >= 50 && scores[i] < 60) {

counter[k++];

}

else if (scores[i] >= 40 && scores[i] < 50) {

counter[k++];

}

else if (scores[i] >= 30 && scores[i] < 50) {

counter[k++];

}

else if (scores[i] >= 20 && scores[i] < 30) {

counter[k++];

}

else if (scores[i] >= 10 && scores[i] < 20) {

counter[k++];

}

else if (scores[i] < 10) {

counter[k++];

}

}

return counter;

}

Answer

```
int k = 0;
counter[k++];
```

The above statement doesn't do anything. It increments `k`

but it doesn't change the value of `counter[index]`

. Maybe you mean `counter[k]++; k++;`

?

```
if (scores[i] >= 90) counter[k++];
else if (scores[i] >= 80 && scores[i] < 90) counter[k++];
else if (scores[i] >= 80 && scores[i] < 90) counter[k++];
...
else if (scores[i] < 10) counter[k++];
else
printf("never reaches here!\n");
```

The `if/else`

condition is wrong. Note that it always satisfies the same condition. It never reaches the last condition.

```
for (i = 0; i < 10; i++); <== extra ;
{
cout << &bins[i] << "| " << endl;
```

There are other errors, it's not clear what the goal is. To print a historgram, you can try the something like the following (this will print a histogram rotated by 90 degree, I don't know if that's close to what you have in mind)

```
#include <vector>
...
std::vector<int> scores;
for (int i = 0; i < 10; i++)
scores.push_back(rand()%10);
for (int i = 0; i < scores.size(); i++)
{
for (int k = 0; k < scores[i]; k++)
{
cout << "*";
}
cout << "\n";
}
```