HELP me please
to solve this problem using if statement
Write a C++ programme that reads a sequence of integers terminated by zero value then finds the percentage of the negative numbers and the percentage of the positive number in the sequence. The programme output is as shown: :
Enter a sequence of in integers terminated by zero
Before thinking about writing code try to think of the logic.
What are your inputs?
-> Sequence of characters which are terminated by zero
What is the interpretation of that? You must keep inputting a numbers until that number which was recently read in is a 0. So in the essence, you stop input if you read a 0 from input.
Now how would you do that? A for loop maybe? And when must the loop stop?
What are you outputs?
-> Percentage of positive numbers and percentage of negative numbers.
How do you calculate percentage? Percentage is (fraction x 100). What is the fraction here?
(No. of positive numbers/ total numbers) is the fraction.
Likewise can we use the same thing for negative numbers?
Okay so we've got all of that figured out.. but how to know if a number is a negative number of a positive number!?
Imagine a number line. What is to the left of 0 and what is to the right of 0? Are the numbers to the left of zero greater in value than zero or lesser?
Think for a moment and tell us what you can come up with. We will definitely help if you get it wrong.
If you are not able to come up with anything then you need to see more sample programs till where you have learnt. That could be conditionals or looping.
First make a do-while (your preference as to which loop to use) that would keep taking integer inputs until it reads in a 0.
Here is the basic structure:
DECLARE vector of integer. OR DECLARE int inputs[100] (use vector if you have learnt about it)
DECLARE i = 0;
DECLARE VARIABLE FOR INPUT
do {
-> TAKE INPUT and store in declared variable (how to take input?)
if(INPUT = 0) (is the condition in correct syntax?)
then:- BREAK LOOP (which control statement is used for this?)
Otherwise:- (what do we use to represent an 'otherwise'?)
do:- inputs[i] = INPUT
now:-
INCREMENT i BY ONE (how to do this with unary increment?)
} while ( no condition is necessary here because of the break statement but if you don't mention break then you must write INPUT != 0 over here, or write that here anyway for clarity).