Question

In: Computer Science

Problem 5 Given a table of n integers and an integer k, make a program in...

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

Solutions

Expert Solution

#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;
}

Related Solutions

Problem: Given an integer k, find the two closest integers in absolute difference and the whole...
Problem: Given an integer k, find the two closest integers in absolute difference and the whole product equals k+1 or k+2. Return the two integers in any order. Write the code in java. You can assume the integer value will be between [1,10^9] You can multiple a divisor with itself. Description of a sample run 1: The input number 8 hence, k+1 is 9 and k+2 is 10 The divisors for 9 are: 1,3,9 and for 10: 1,2,5,10 For both...
that, given an integer N and an integer K, returns the minimum number of rounds that...
that, given an integer N and an integer K, returns the minimum number of rounds that are necessary for John to leave the casino with N chips, having played all-in no more than K times.
Given a positive integer k and an array A[1..n] that contains the quiz scores of n...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n students in ascending order, design a divide and conquer algorithm to efficiently count the number of students that have quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. Let group i be the set of students with quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. The counting result should be stored in G[1..k],...
Statement: For a given integer N, print all the squares of positive integers where the square...
Statement: For a given integer N, print all the squares of positive integers where the square is less than or equal to N, in ascending order. Programming Tasks: Prompt the user to input the value of N Output to the screen all squares of positive integers <= N Tests: Item Test 1 Test 2 Test 3 Inputs: 50 9 100 Outputs: 1 4 9 16 25 36 49 1 4 9 1 4 9 16 25 36 49 64 81...
Lab 5 a) Write a program that reads in an unsigned integer K and sums the...
Lab 5 a) Write a program that reads in an unsigned integer K and sums the first K many integers that are divisible by 7. You should output the sum on a formatted manner b)Consider the following diamond it is an 11 by 11 diamond made with * signs. Write a program that takes as input positive odd integer K (greater than or equal to three and outputs a K by K diamond made with * signs * *** *...
write a program named Combinations.java that gets user input for two integers n and k, then...
write a program named Combinations.java that gets user input for two integers n and k, then computes and displays the value of n choose k using the efficient method of equation. you may assume that the user's input makes sense (i.e, n and k are non negative, and n is at least k). your code should work for any reasonably small non- negative values of n and k, including zero.
Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
For all integers n > 2, show that the number of integer partitions of n in...
For all integers n > 2, show that the number of integer partitions of n in which each part is greater than one is given by p(n)-p(n-1), where p(n) is the number of integer partitions of n.
4. Let n ≥ 8 be an even integer and let k be an integer with...
4. Let n ≥ 8 be an even integer and let k be an integer with 2 ≤ k ≤ n/2. Consider k-element subsets of the set S = {1, 2, . . . , n}. How many such subsets contain at least two even numbers?
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether we can use numbers from c[0...n − 1] to make a sum of v, allowing any particular number in the list to be used multiple times. Or, mathematically, this means that there exists non-negative integer coefficients, x0, x1, ..., xn−1, such that v = x0c[0] + x1c[1] + ...xn−1c[n − 1]. For example, given c[0...3] = {2, 4, 6, 10}, and v = 17,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT