Categorizing values into a bucket-please help

Okay so here is the assignment:
The program does the following:
1. first prompt the user to find out how many bucket values are there. You may assume there is
no more than 10 bucket values.
2. then read in all of the bucket values into an array. You may assume the bucket values are
strictly sorted from smallest to largest with no duplicates.
3. read in a lower bound and upper bound to describe valid range of input. You may assume
lowerBound <= upperBound
4. we will keep reading (and categorizing the value into different buckets) as long as the value
falls in between the range from step 3 [lowerBound, upperBound] inclusive.
5. print out how many values falls in each bucket.


Here is a sample input/output of the program:
v:\cmpsc101>lab14.exe
Enter number of bucket values:
2
Enter 2 bucket values:
0.5 1.5
Enter lower and upper bound of values to count:
-100
2.3
Enter values between -100 and 2.3:
-50
2.0
2.1
1.3
1.4
0.2
1.5
2.5
2 values are <= 0.5.
3 values are <= 1.5.
2 values are > 1.5.

and here is my code that will not work. please help
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
#include <iostream>
using namespace std;
int getBucket(const double bucketvalues[], int size, double value);
int main()
{
	
	int values;
	double countarray[10];
	double valuesarray[10];
	int j=0;
	int x=0;
	int size;
	double lower;
	double upper;
	double bucketvalues[10];
	cout<<"Enter number of bucket values:\n";
	cin>>values;
	cout<<"Enter "<<values<<" bucket values:\n";
	for(int i=0; i<=values; i++)
	{
		cin>>bucketvalues[i];
	}
	cout<<"Enter lower and upper bound of values to count:\n";
	cin>>lower;
	cin>>upper;
	size=upper-lower;
	cout<<"Enter values between "<<lower<<" and "<<upper<<":\n";
	for(int i=lower; i<=upper; i++) {
		cin>>valuesarray[i];
		getBucket(bucketValues, size, i);
	
	}
	
	return 0;
}



int getBucket(const double bucketValues[], int size, int values) {
	int no=0;
	int j=0;
	for(int i=0; i<size-1; i++) {
		if (bucketValues[i] >= bucketValues[i+1])
		{
			no=1;
		}
	}
	
	if (no==0) 
	{
		while ((j<size))
		{
			if (values <= bucketValues[j])
			{
				return j;
			}
			j++;
		}
		if(values>=bucketValues[j]) {
		    return size;
		}
		
	}
	else if(no>0)
	{
	    
		return -1;
	}
		
}
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
#include <iostream>

using namespace std;
int getBucket(const double bucketvalues[], int size, double value);

int main()
{
    const int MAXSIZE = 1000 ;

    int num_values;
    cout<<"Enter number of bucket values:\n";
    cin>>num_values;
    if( num_values < 1 || num_values > (MAXSIZE-1) ) return 1 ;

    double bucketvalues[MAXSIZE];
    cout<<"Enter "<< num_values <<" bucket values in ascending order: ";
    for(int i=0; i < num_values; i++) cin>>bucketvalues[i];
    // TODO: verify that bucketvalues are in ascending order

    double lower;
    double upper;
    cout<<"Enter lower and upper bound of values to count:\n";
    cin>>lower;
    cin>>upper;

    cout<<"Enter values between "<<lower<<" and "<<upper<<":\n";

    int countarray[MAXSIZE] = { 0 } ; // initialize all counts to zeroes
    double value ;

    // till an out of range value is entered
    while( std::cin >> value && value >= lower && value <= upper ) {

        // get the bucket number
        const int bucket = getBucket( bucketvalues, num_values, value ) ;

        ++countarray[bucket] ; // and increment its count
    }

    int count = 0 ;

    for( int i=0 ; i < num_values ; ++i ) {
        count += countarray[i] ;
        cout << count << " values are less than " << bucketvalues[i] << '\n' ;
    }

    // what remains are values greater than the largest bucket value
    std::cout << countarray[num_values] << " values are greater than "
               << bucketvalues[num_values-1] << '\n' ;
}

int getBucket( const double bucketValues[], int size, double value ) {

    // iterate through bucket values in ascending order
    for( int i=0 ; i < size ; ++i )
        if( value < bucketValues[i] ) return i ; // till we hit a larger value
        // TODO: read up on comparing floating point values

    return size ; // value is >= the largest bucket value
}
Topic archived. No new replies allowed.