Answer:
The c++ program for the given scenario is given below.
#include <iostream>
using namespace std;
int main() {
int len=20, arr[len], data;
// initialize all elements of the array to 0
for(int i=0; i<len; i++)
{
arr[i] = 0;
}
cout<<"This program outputs all the numbers less than or equal to the given number."<<endl;
cout<<"Enter the list of numbers. Enter 0 to stop entering the numbers. "<<endl;
for(int i=0; i<len; i++)
{
cin>>arr[i];
// 0 indicates user wishes to stop entering values
if(arr[i] == 0)
break;
else
continue;
}
// number from which the list is to be compared
cout<<"Enter the number to be compared."<<endl;
cin>>data;
cout<<"The values less than or equal to the number "<<data<<" are "<<endl;
for(int i=0; i<len; i++)
{
// 0 indicates the end of the list entered by the user
if(arr[i]==0)
break;
if(arr[i] <= data)
cout<<arr[i]<<endl;
}
return 0;
}
OUTPUT
This program outputs all the numbers less than or equal to the given number.
Enter the list of numbers. Enter 0 to stop entering the numbers.
23
45
67
89
10
0
Enter the number to be compared.
59
The values less than or equal to the number 59 are
23
45
10
Explanation:
This program takes input only from the user and makes no assumptions.
The list of numbers entered by the user are stored in an array. If the user input is less than the size of the array, the remaining elements are set to 0.
While taking input from the user, if any element of the array is found to be 0, the loop is discontinued.
for(int i=0; i<len; i++)
{
cin>>arr[i];
if(arr[i] == 0)
break;
else
continue;
}
Same test is applied when all the numbers less than the given number are displayed.
for(int i=0; i<len; i++)
{
if(arr[i]==0)
break;
if(arr[i] <= data)
cout<<arr[i]<<endl;
}
The above program takes into account all the specifications in the given question.