Problem Solving: Stream of Numbers
Learn to write a program that takes a stream of numbers and prints the maximum-minimum and even-odd frequency.
We'll cover the following...
In this lesson, we’ll solve a few problems that include the stream of inputs in which we have to deduce information by reading data only once (without having to store the data). Specifically, we will solve the following two problems:
- Even-odd frequency count
- Maximum-minimum number from the stream of numbers
So let’s start!
Even and odd frequency
Write and run a program that takes a stream of integers ending with -1 and prints the frequency of even and odd numbers. The -1 is the sentinel value here that would indicate that no more input is needed.
Sample input
2 3 4 5 6 7 -1
Sample output
No of even: 3
No of odd: 3
To solve the above problem, we will opt for the following step-by-step approach:
-
Read an integer
number(usingcin) from the console input stream. -
If the input number is even, increment by
1inevens. Otherwise, increment by1inodds. -
Read the next integer from the stream until it reads the end of the stream which is
-1. Repeat step 2. -
Print the counts of
evensandodds.
Let’s see how we can implement this step-by-step algorithm in a C++ program:
First, let’s make a function that checks whether the entered number is -1 or not. Let’s call this function isEndOfStream() which returns true for -1 and false for other numbers.
bool isEndOfStream(int number){if(number==-1){return true;}return false;// The above lines can be shortened into the following one-liner// return number == -1;}
Now we need to determine if a number is even or odd.
So let’s make a function isEven() that returns true if number is even and false otherwise.
bool isEven(int number){if(number%2==0){return true;}return false;// The above lines can be shortened into the following one-liner// return number%2 == 0;}
Do we need to create an isOdd() function as well?
To print the result on the console we will make a function print that will print the count of total numbers in the sequence, the even count, and the odd count.
Our print() function looks like this:
void print(int evens,int odds,int total){cout<<"Total numbers: "<<total<<endl;cout<<"No of evens: "<<evens<<endl;cout<<"No of odds: "<<odds<<endl;}
Let’s write the complete code below and run it to see the output.
#include <iostream>
using namespace std;
bool isEven(int number);//It will return true if number enter is even else it will return false
bool isMinusOne(int number);//It will return true if number enter is -1 else false
void print(int evens,int odds,int total);//it will print output on screen
bool isEndOfStream(int number);
void message();//It will display input message on the screen
int main()
{
int total=0,evens=0,odds=0,number=0;
message();
cin>>number;
while(!isEndOfStream(number))
{
if(isEven(number))
evens++;
else
odds++;
total++;
cin>>number;
}
print(evens,odds,total);
return 0;
}
bool isEven(int number)
{
return (number%2==0);
}
bool isEndOfStream(int number)
{
return (number==-1);
}
void print(int evens,int odds,int total)
{
cout<<"Total Number:\t"<<total<<endl;
cout<<"Even Number:\t"<<evens<<endl;
cout<<"Odd Number:\t"<<odds<<endl;
}
void message()
{
cout<<"Enter a stream (ending with -1)"<<endl;
}
-
In line 13, we check if
numberholds the end of the stream. If it does, the loop ends. -
In lines 15–16, we check if
numberis even or not. Ifnumberis even, the function will returntrueandevensis incremented by1. -
In lines 16–17,the
elsepart will be executed (when required) to incrementoddsby1. -
In line 23, we print the frequency count of
evensandodds.
Maximum and minimum from a sequence of numbers
We would like our program to read the integers stream until the user enters -1 and to output the maximum and minimum numbers.
Sample input
20 30 40 50 -1
Sample output
Minimum: 20
Maximum: 50
Let’s consider for a moment, not specifically the min/max problem but any problem that involves finding a number from the stream possessing a required property. What should we be doing?
We must maintain some variable initialized with some value, and when reading each new value, that value must be compared with our maintained variable. Our maintained variable should hold the updated result of the already-read stream based on our problem. It should then check whether the new value is better than the previously maintained value based on the problem. If it is, it should be updated.
For example, in our program, we want to find both the minimum and maximum from the stream of numbers. Let’s maintain two variables, maximum and minimum, and initialize them with the first value. We will create two functions:
The
maxUpdate()function should returntrueifmaximumis less thannumberand theminUpdate()function should returntrueif theminimumis greater than thenumber. Otherwise both should returnfalse.
Let’s write maxUpdate() and minUpdate():
bool maxUpdate(int maximum,int number){if(maximum<number){return true;}return false;}bool minUpdate(int minimum,int number){return (minimum>number); // if condition is true it returns 1 or 0 otherwise}
Let’s write the complete code below and run it to see the output.
#include <iostream>
using namespace std;
bool maxUpdate(int maximum,int number);//It will return true whether number is max or not
bool minUpdate(int minimum,int number);//It will return true whether number is min or not
bool isEndOfStream(int number);//It will return true if number entered is -1 else false
void print(int max,int min,int total);//It will Print Output on the screen
void message();//It will show Input Message on the screen
void delay(int number);//It will cause delay
int main()
{
int number=0,max=0,min=0,total=0;
message();
cin>>number;
max=min=number;
while(!isEndOfStream(number))
{
total++;
if(maxUpdate(max,number))
max=number;
if(minUpdate(min,number))
min=number;
cin>>number;
}
if(total!=0)
print(max,min,total);
return 0;
}
bool maxUpdate(int maximum,int number)
{
if(maximum<=number)
{
return true;
}
return false;
}
bool minUpdate(int minimum,int number)
{
return minimum>=number;
}
bool isEndOfStream(int number)
{
if(number==-1)
{
return true;
}
return false;
}
void print(int max,int min,int total)
{
cout<<"Total numbers entered = "<<total<<endl;
cout<<"Max number = "<<max<<endl;
cout<<"Min number = "<<min<<endl;
}
void message()
{
cout<<"Enter -1 to finish program"<<endl;
cout<<"Enter the number you want to check"<<endl;
}
-
In lines 13–14, we take the first number and consider it as both maximum and minimum in the variables
maxandmin, respectively. -
In line 18, we call the
maxUpdate()function with argumentsmaxand the new value. If the function returns true,maxis updated in the following line. -
In line 20, we call the
minUpdate()function with argumentsminand the new value. If the function returns true,minis updated.