I have an array of 20 elements with numbers between 1 and 8. I need a for loop that will give me the number of elements greater than 1???

Recommended Answers

Create another variable to store the number of 1's. You go through all elements of the array and each time it equals 1 you increment that variable by 1.

You could think of that by yourself ain't that hard.

Jump to Post

What have you tried so far?

Jump to Post

All 5 Replies

Create another variable to store the number of 1's. You go through all elements of the array and each time it equals 1 you increment that variable by 1.

You could think of that by yourself ain't that hard.

Create another variable to store the number of 1's. You go through all elements of the array and each time it equals 1 you increment that variable by 1.

You could think of that by yourself ain't that hard.

I get that I just don't know how to put it into a for loop? I'm very new at this and in need of help. I don't know exactly what to put in the for loop and where?

I get that I just don't know how to put it into a for loop? I'm very new at this and in need of help. I don't know exactly what to put in the for loop and where?

I used sum for my variable but it was telling me where the 1's were in the array instead of adding them up.

What have you tried so far?

I'll give you a hint to what I'd do.
Use a for loop to iterate through the array
Use an if statement to check if the number is greater than 1.
You'll need to include a counter which you will increment every time the if statement is successful.
This is not a very difficult problem. Use my advice and post back with your best effort. Good luck.

Be a part of the DaniWeb community

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