60.5k views
9 votes
Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. That list is followed by two more integers representing lower and upper bounds of a range. Your program should output all integers from the list that are within that range (inclusive of the bounds). For coding simplicity, follow each output integer by a space, even the last one. The output ends with a newline.

Ex: If the input is: 5 25 51 0 200 33
0 50

then the output is:
25 0 33

1 Answer

10 votes

Answer:

The program implemented in C++ is as follows:

#include <iostream>

#include <vector>

using namespace std;

int main(){

int lenlist;

vector<int> mylist;

cout<<"Length List: ";

cin>>lenlist;

mylist.push_back(lenlist);

int i =0; int num;

while(i<lenlist){

cin>>num;

mylist.push_back(num);

i++;

}

int min,max;

cout<<"Min: "; cin>>min;

cout<<"Max: "; cin>>max;

cout<<"Output!"<<endl;

for(int i=1; i < mylist.size(); i++){

if(mylist.at(i)>=min && mylist.at(i)<=max){

cout<<mylist.at(i)<<" ";

}

}

return 0;

}

Step-by-step explanation:

This declares the length of the list as integer

int lenlist;

This declares an integer vector

vector<int> mylist;

This prompts user for length of the list

cout<<"Length List: ";

This gets input of length of the list from the user

cin>>lenlist;

This pushes user input to the vector

mylist.push_back(lenlist);

int i =0; int num;

The following iteration gets user inputs and pushes them into the vector

while(i<lenlist){

cin>>num;

mylist.push_back(num);

i++;

}

This declares min and max variables

int min,max;

This prompts user for the lower bound

cout<<"Min: "; cin>>min;

This prompts user for the upper bound

cout<<"Max: "; cin>>max;

The following iteration checks for numbers within the lower and upper bound and print the numbers in that range

cout<<"Output!"<<endl;

for(int i=1; i < mylist.size(); i++){

if(mylist.at(i)>=min && mylist.at(i)<=max){

cout<<mylist.at(i)<<" ";

}

}

User Bsd
by
4.5k points