In: Computer Science
Problem 5
Given a table of n integers and an integer k, make a program in C++
that:
a) Read n
b) Read the table of numbers.
c) Determine the number of elements in the table less than k
d) Determine the number of elements in the table equal to k
e) Determine the number of elements in the table greater than
k
f) That displays the values found
#include <iostream>
#include<string>
using namespace std;
int main()
{
int n = 0;
int k = 0;
cout<<"Enter number of elements\n";
cin>>n;
int values[n];
cout<<"\nEnter elements\n";
for(int i=0;i<n;i++)
{
cin>>values[i];
}
cout<<"Enter value of k\n";
cin>>k;
// to store values
string lessThan;
string greaterThan;
string equalTo;
// counters
int less=0;
int greater=0;
int equal=0;
for(int i=0;i<n;i++)
{
if(values[i]<k)
{
lessThan=lessThan+ " " +to_string(values[i]);
less++;
}
else if(values[i]>k)
{
greaterThan=greaterThan+ " " +to_string(values[i]); greater++;
}
else
{
equalTo=equalTo+ " " +to_string(values[i]);
equal++;
}
}
cout<<"Values in the table are ";
for(int i=0;i<n;i++)
{
cout<<values[i]<<"\t";
}
cout<<"\n"<<less<<" values are less than "<<k<<"\t"<<lessThan;
cout<<"\n"<<greater<<" values are greater than "<<k<<"\t"<<greaterThan;
cout<<"\n"<<equal<<" values are equal to "<<k<<"\t";
return 0;
}