logic.

Im actually just trying to understand the logic behind why does variable "Value" not counting properly integers>=15. if really appreciate it someone can help me avoid this mistake for future projects.

/*Problem1: Write a program that saves 10 randomly generated numbers(between 1-20) into an array. The program must count and print how many integers are greater than or equal to 15.*/


#include <iostream>
#include<string>
#include<ctime>
#include<cstdlib>
using namespace std;

int main()
{

int index=0;
int value=0;

const int size=10;
int randomNumber[size];

//generate ten random numbers.
srand(time(0));
for(int x=1; x<=10; x++){
randomNumber[index]= 1+(rand() %20);
cout<<randomNumber[index]<<endl;
if ((randomNumber[index])>=15)
value=+1;
else value=0;
index++;
}
cout<<"The number of integers greater than or equal to 15 is: "<<value<<endl;
return (0);

}
Value += 1 is the right shorthand addition operator. You can also do just value++ instead, that's unary increment.

Also you don't need an else statement as you want nothing to happen with respect to the value variable when a smaller number is found.

Value =+1 is assigning value '1' and value = 0 in your esle statement is assigning value to be 0.
@Algonology

You are actually NOT assigning any values to the rest of your array, except to randomNumber[0]. You are assigning values to randomNumber[index], not to randomNumber[x] as the loop should be. Arrays start at 0, not 1, as you have in your for loop, and go to one less than the assigned size. So, randomNumber[size] goes from randomNumber[0] to randomNumber[9]
@whitenite he's incrementing index so it's fine to use index too. And he has assigned index to 0 before the loop.

So it is indeed assigning values from 0 to 9 and not 1 to 10 like you said.
@Nwb

My apologies then, to Algonology. I'm not used to seeing for loops written as such, so missed the index variable increase. I guess I should study some of these codes a bit closer, before jumping to an answer, or opinion, that isn't correct.
No no it's not even 0.1% your fault.. it's because he didn't use coding tags. This forum really needs a detection system which will avoid users from posting code without coding tags.

Some popular forums don't allow the user to post text with '{' or '}', as braces signify that the text contains code. And that has been REAAAAALLY very effective.

This forum could use that too, but doesn't seem like anything like that will happen anytime soon.. also considering the other glitches which have stayed unfixed for ages.

For really long code I just open up my compiler and paste the text, the compiler thankfully automatically indents it. Sigh.

Alponology please use coding tags [code]code here[/code.] (without the dot (.))
Those tags help preserve indents and help the readability of the code BY A LOT for the readers.

edit: I guess this forum doesn't have the 'noparse' BB code..
Last edited on
Topic archived. No new replies allowed.