CountNmbrsBigger – provide a count of the number of data

CountNmbrsBigger – provide a count of the number of data elements in the array that are larger than a given integer.

Ok I almost have this one done, but im not sure how I would do the code for
this section - int countNmbrsBigger(int). I have the cout written the way I want it just not sure what I should do on the class part of it. I has to display the count of numbers greater than 20. Any help on this would be great.


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
#include <iostream>
using namespace std;

class ArrayClass {
      private:

      public:
              int *theArray;
              int theArraySize;

             ArrayClass(int [], int);
             int sum(void);
             int countNmbrsBigger(int);
             double average(void);
             int high(void);
             int low(void);
             int find(int);
};
int main()
{
	const int size = 10;
	int myArray[size] = { 14, 35, 12, 23, 78, 98, 198, 90, 56, 345};
	ArrayClass arrayObject ( myArray, size);
	cout << "Low: " << arrayObject.low() << endl;
	cout << "High: " << arrayObject.high() << endl;
	cout << "Sum: " << arrayObject.sum() << endl;
	cout << "Average: " << arrayObject.average() << endl;
	cout << "Numbers higher than 20: " << arrayObject.countNmbrsBigger() << endl;
	cout << "Found 23 at " << arrayObject.find(23) << endl;
	return 0;
}
ArrayClass::ArrayClass(int anArray[], int ArraySize)
{
      //constructor
      theArray = anArray;
      theArraySize = ArraySize;
}  
int ArrayClass::low(void)
{
      //find the lowest value in the array
      int lowValue = theArray[0];
      for (int i = 0; i < theArraySize; i++)
	  {
            if (theArray[i] < lowValue)
			{
                  lowValue = theArray[i];
            }
      }
      return lowValue;
} 
int ArrayClass::sum(void)
{
	//calculate the sum and return the total
	int total = 0;
	for (int i = 0; i < theArraySize; i++)
	{
		total += theArray[i];
	}
	return total;
}
int ArrayClass::countNmbrsBigger(int)
{
	
}
double ArrayClass::average(void)
{
	//calculates the average and returns the value
	return  (double)sum() / theArraySize;
	
}
int ArrayClass::high(void)
{
//find the highest value in the array
      int highValue = theArray[0];
      for (int i = 0; i < theArraySize; i++)
	  {
            if (theArray[i] > highValue)
			{
                  highValue = theArray[i];
            }
      }
      return highValue;
}
int ArrayClass::find(int number)
{
	for (int i = 0; i < theArraySize; i++)
		if (theArray[i] == number)
			return i;
	return -1;
}
Tou add a counter variable initialized to 0. Then you run through your array and check if each elemnt is greater than your number. If it is you add one to your counter. At the end of the function you return the counter...

Hope this helps
sorry you lost me lol. Im still new to this took me forever to do this much. You mind giving an example so I can better understand you. Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main(){
   int myArray[10] = { 14, 35, 12, 23, 78, 98, 198, 90, 56, 345};
   int counter=0; //A counter to know how many you found
                  //(something like your fingers when you count something :P)
   int greaterThan = 20; //A holder for the number we want to count above
   for( int i=0; i<10; i++ ){//We run through the elements of the array
      if( myArray[i] > greaterThan )//If we find an element greater than the value we want
         ++counter; //we increase our counter by one
   }
   cout << "Found " << counter << " numbers above " << greaterThan << "." << endl;
   return 0;//for your function you will return the counter
}


Hope this helps
Last edited on
ok thank you very much I understand now
The code you need is a lot like the find() you got there. You just need to compare the array element to the parameter passed to the function and each time the if condition is true, increment a counter which will have been, before the for, initialized to 0. Then return the counter.

Another thing that needs improvement: in high(), you're correctly initializing the max to the first element of the array, but you then start the for from that very same element. Instead, start from the second element so you don't do an unnecessary cycle. The same applies to low().
thank you for the tip! 8)
Use std::count_if in combination with std::greater and std::bind2nd:
std::vector<int> A;
// fill A
unsigned int num_bigger_10 = std::count_if(A.begin(), A.end(), std::bind2nd(std::greater<int>, 10));
Always remember: code is there to be reused, especially STL code. This solution is easy to read, debug and maintain. It is 100% Bug Free (TM) and much shorter than your solutions.

Hope that helps
Last edited on
I think the teacher would prefer if he wrote the code himself. Reusing STL code is not practice for anything.
I think the teacher would prefer if he wrote the code himself. Reusing STL code is not practice for anything.

How so, if he didn't find this solution in the first place? Obviously he didn't know about it, now he does, so he learned something (of course, if he doesn't just copy&paste) - namely, that the solution can be built from the stl functionality in one line, and how.
If I were the teacher, I would ask some questions (e.g., requirements must the first parameter of bind2nd fulfill) and he better had an answer. But provided he does, I don't see why I wouldn't give him extra credit for a good, C++ only solution - it is by far easier to write an own, buggy solution than to reuse a robust, stable one, so this should be given extra credit for, I think. It is not like that the requirement was "do it without the stl", if it really was an assignment at all.
Of course, some teachers give you an assignment x while thinking of an assignment y, and if you don't do y you are screwed. I'm really really sorry for anyone who has such teachers.
No, he wasn't required to not use the STL, but he is obviously in an introductory programming course, and still learning the basics. Using the STL will not serve any purpose as programming practice, since he wouldn't be doing any actual programming, would he?

Now that we're on the subject, and although I'd prefer not to go off-topic, I've been wanting to ask you. You say code as simple as that find() is buggy, but I look at it and I don't see any obvious errors. So when you say "buggy", do you mean error-prone, or are we talking about something on a more subatomic level?
Using the STL will not serve any purpose as programming practice

As stated above, I disagree.

As for the second point, let's consider the code:
1
2
3
4
5
6
7
int ArrayClass::find(int number)
{
	for (int i = 0; i < theArraySize; i++)
		if (theArray[i] == number)
			return i;
	return -1;
}

Observations:
- is int actually the same range as size_t? Even in the positive integers? (possible errors with theArraySize and i. Or can an array be any size in the range of int values?)
- i++ creates an unnecessary temorary object (could be avoided by ++i)
- return -1 requires pretty much care when using. Any sensible C++ (or even C) programmer will assume that find returns the "past-the-end" pointer if not succssful.

So, no, this code has no bugs until invoked with wrong parameters in which case it will be hard to debug. Furthermore it does not obey the canonical way of range handling, which *will* lead to errors in the client code.

Another point is that you *have to look at it*. You have to learn that it returns -1 when not successful - which is unproductive. Reuse standard code, and all are happy.

On a last remark, I don't want to talk any solution here "bad". What I basically tried to say is "prefer the usage of the stl instead of home-brew code" and I showed the way how to do it in the given case. My statement about the difficulty of re-use and ease of writing own (but untested) solutions is a general one, not directly inspired by any code posted in this thread (e.g., would you know exactly (and without looking something up) what the requirements for a parameter used in the place of std::greater are? I would not, but I would be abel to write the "count"-algorithm without going to my bookshelf and most likely be done before I could even reached the latter. Nonetheless I prefer reusing the stl because I have to manage and maintain my code).
Last edited on
I am still new to programming so a lot of things I have not learned I thank you both for you advise. I will read up on the way you are saying exeception. I think my friend said that I should return -1 because if the program generated random numbers (I was thinking about doing that) -1 would not come up. He is new as well so we are both learning things.
I am certainly no expert at all, and neither are most people that post in this beginners forum. You have to remeber exception that we are learning the language, and sometimes more complicated topics such as templates and that whole library may not be what we are looking for.

Also I am in school, and If i start pulling advanced topics that beyond what I understand at the time, my professor will know that I am getting the answer for someone else, and it is not my own writings. Such will give me a bad grade. By no means that I am saying you are wrong, using the STL in this case may be the better way of doing it, but it is not the point of the excersise. You must remember, we are not all writing Client code yet, some times we just do this for fun!

My two cents, not that they mean much!!
I am certainly no expert at all, and neither are most people that post in this beginners forum

I understand that. But I neglect to show anyone, beginner or not, a solution which is not the best I can do. *I* would certainly not choose C++ as a language for school. But your teacher did, so you have to deal with it. And even though your teacher might not even be aware of the problems I pointed out in the code above, they are problems and I personally deem it harder to re-learn something I have learned in an incorrect or "only-sometimes-correct" way than to learn it correctly from the beginning. Of course, the ammount of stuff you get to do in the same time is then pretty limited. But that's the price you have to pay for learning C++, if you don't want to be screwed later because you don't know if what you have "learned" is worth anything.

On another note, I should not have pointed out what I did after my first post here in the beginner forum. I felt that what helios said was not completely correct, but I should just have shut up. I apologize for any confusion among the ones interested in the actual problem.
Topic archived. No new replies allowed.