I have a question about my logic in trying to code something. Let's say I have an array of 50 values. Now I want to find the values that are over (lets say 100). And I want to print "There are 11 values over 100" Now, my question is, I am using a for loop with an if (num>100), then my thoughts are I need to get the number of values that are greater than 100. However, this is the part I am stuck on. I am not really sure how to do this.

Or am I approaching this all wrong and there is a different way to think about this?

You don't need to respond with the code. I am just looking for help with my thinking on how to get this data out of this array. I was searching through posts and either I am using the wrong search words, or I am just not finding what it is I am looking for.

Sorry to you all of you that are experts and this may seem like a dumb question :-(

Recommended Answers

All 4 Replies

If I'm understanding your dilemma correctly, you just need to declare a counter integer, initialized to zero, before the for loop and each time your (if num>100) condition is met, increment the counter.

Well you're very nearly there!
You said you wanted to find out how many items in the array were greater than 100, that would imply that you need some kind of counter, which is the missing ingredient in your original description.
So in your code:
You set your counter to 0
You use a for loop to loop through your array.
On each iteration of the loop, if the current item is greater than 100, increment the counter by one.

Once you're out of the loop, your counter holds the number of items which were greater than 100.

Cheers for now,
Jas.

EDIT: doh beaten to the punch!

Thanks for the input!!!!! Why do the simplest things get overlooked! Guess these things happen sometimes! :ooh: :icon_redface: Bunches of thanks to u both!!

Cheerio!

No problem. Don't forget to mark the thread solved so you don't get a hundred more posts with the same answer.


ETA: Now it's me who's been beaten to the punch!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.