Histograms

I have a lab project due for my C++ class soon that I'm having a lot of trouble finishing. The project is that we have to create a program that makes at least 2 histogram graphs given 4 or 5 integer inputs, and the graphs have to be vertical. I can post the code I have thus far and hopefully some of you will give me some tips out of the kindness of your hearts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
using namespace std;

int histo(int input_a, int input_b, int input_c, int input_d);
int histo(int input_a, int input_b, int input_c, int input_d, int input_e);

int main( ) {
    int input_a, input_b, input_c, input_d, input_e, ans;


    cout << "How many possible answers does this question have?" << endl;
    cin  >> ans;
    
    if(ans == 4){
           cout << "Please enter the number of times A, B, C, and D were chosen, hitting return after each input." << endl;
           cin  >> input_a;
           cin  >> input_b;
           cin  >> input_c;
           cin  >> input_d;
           cout << histo(input_a, input_b, input_c, input_d) << endl;
                }
    if(ans == 5){
           cout << "Please enter the number of times A, B, C, D and E were chosen, hitting return after each input." << endl;
           cin  >> input_a;
           cin  >> input_b;
           cin  >> input_c;
           cin  >> input_d;
           cin  >> input_e;
           cout << histo(input_a, input_b, input_c, input_d, input_e) << endl;
                }








    system("PAUSE");
    return 0;
}

//Makes histogram given 4 integer inputs.
int histo(int input_a, int input_b, int input_c, int input_d){
    int testvalues = 4;
    int A, B, C, D;
    char values[testvalues] = {A, B, C, D};
    int i = 0;
    int j = 0;
    int maxvalue;
    maxvalue = 0;
    for(i = 0; i < testvalues; i++){
          if(values[i] > maxvalue) maxvalue = values[i];
                                   }
    int minvalue = maxvalue;
    for(i = 0; i < testvalues; i++){
          if(values[i] < minvalue) minvalue = values[i];
                                   }
    int freqsize = maxvalue - minvalue + 1;
    int frequency[freqsize];
    for(i = 0; i < freqsize; i++){
          frequency[i] = 0;
                                 }
    for(i = 0; i < testvalues; i++){
          int index = values[i] - minvalue;
          frequency[index]++;
                                  }
    for(i = 1; i <= freqsize; i++){
          printf("%2d\t|", i + minvalue);
    for(j = 0; j < frequency[i]; j++){
          printf("*");
                                  }
          printf("\n");
                                  }     
                                                              } 
//Makes histogram out of 5 integer inputs.                                                              
int histo(int input_a, int input_b, int input_c, int input_d, int input_e){
    int testvalues = 5;
    int A, B, C, D, E;
    char values[testvalues] = {A, B, C, D, E};
    int i = 0;
    int j = 0;
    int maxvalue;
    maxvalue = 0;
    for(i = 0; i < testvalues; i++){
          if(values[i] > maxvalue) maxvalue = values[i];
                                   }
    int minvalue = maxvalue;
    for(i = 0; i < testvalues; i++){
          if(values[i] < minvalue) minvalue = values[i];
                                   }
    int freqsize = maxvalue - minvalue + 1;
    int frequency[freqsize];
    for(i = 0; i < freqsize; i++){
          frequency[i] = 0;
                                 }
    for(i = 0; i < testvalues; i++){
          int index = values[i] - minvalue;
          frequency[index]++;
                                  }
    for(i = 1; i <= freqsize; i++){
          printf("%2d\t|", i + minvalue);
    for(j = 0; j < frequency[i]; j++){
          printf("*");
                                  }
          printf("\n");
                                  }  
                                                                                                                                                       } 
       
Topic archived. No new replies allowed.