Question

C++, and comments and explanation, please.

Input: the program should read the followings: First line, an integer . Second line, an integer n

C++, and comments and explanation, please.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

For this program, we will calculate the lower bound of the given number 'x' (explained later in the code). Lower bound is calculated by expanding and modifying the code for binary search. Unlike binary search where we search for a number (inturn return an index specifying that number) here we will search for the first number (inturn the index) where the number is greater than or equal to the given number. This is done in log(n) time just like binary search. For this program we will use in-built C++ function lower bound function(explained later in the code)

NOTE: Incase you dont like the in-built implementation of the program using vectors(similar to array), just tell me I can also implement lower_bound function using own written code which will work on arrays as well.

C++ CODE (With STL lower_bound) :

#include<bits/stdc++.h>
using namespace std;

int main() {
   int x, n;
   cin >> x >> n;
   vector<int> a(n);
  
   /*
   vectors are just like arrays with added advantages. There dynamically
   allocated like lists(whose size can change) and also have indexes like arrays.
   */
   for (int i = 0; i < n; ++i) {
       cin >> a[i];
   }
   vector<int> :: iterator it; // unlike array, iterators can also be used to traverse an vector.

   /*
   lower_bound() returns an iterator to the elements in the given range which does no compare less(i.e which means is greater than or equal to :))
   than the given value. The range given should be already sorted for lower_bound() to work. In plain words it
   returns an iterator to the lower bound of the given element in a sorted range.
   */
  
   it = lower_bound(a.begin(), a.end(), x);
   /*
   lower bound has returned the position of the element which is greater than or equal to the x.
   Therefore, the numbers less than x will be till that position excluding that number.
   */
   for (int i = 0; i < (it - a.begin()); ++i) {
   /*
   it - a.begin because 'it' is a pointer and its value corresponds to an address.
   Therefore subtracting the starting address of the vector will give us the distance of that pointer from the starting address,
   which would be needed to display the numbers smaller than the given number ('x')
   */
       cout << a[i] << " ";
   }  
}

C++ Code (Without STL lower_bound):

#include<bits/stdc++.h>
using namespace std;
int Lower_bound(int a[], int n, int x) {
    int left = 0;
    int right = n;
    while (left < right) {
        int middle = (left + right) / 2;
        if (x <= a[middle]) {
            right = middle;
        } else {
            left = middle + 1;
        }
    }
    return left;
}

int main() {
   int x, n;
   cin >> x >> n;
   int a[n];
   for (int i = 0; i < n; ++i) {
       cin >> a[i];
   }
   int limit = Lower_bound(a, n, x);
   for (int i = 0; i < limit; ++i) {
       cout << a[i] << " ";
   }
  
}

OUTPUT:

1)

25 1 4 5 30 35 48 68 98 1 4 5 Process exited after 16.87 seconds with return value e Press any key to continue..

2)

31 1 4 5 18 16 48 68 98 1 4 5 10 16 Process exited after 12.61 seconds with return value Press any key to continue .. .

Add a comment
Know the answer?
Add Answer to:
C++, and comments and explanation, please. C++, and comments and explanation, please. Input: the program should...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Code please read comments and add the code to required lines....LINE 1 to LINE 5...

    Java Code please read comments and add the code to required lines....LINE 1 to LINE 5 ********************************************************************** * Program Summary: This program demonstrates these basic Java concepts: * - Creating an array based on user input * - Accessing and displaying elements of the array * * The program should declare an array to hold 10 integers. * The program should then ask the user for an integer. * The program should populate the array by assigning the user-input integer...

  • C++: Write a C++ program with user input, output and using inheritance. Please include comments in...

    C++: Write a C++ program with user input, output and using inheritance. Please include comments in your code.

  • CISC 1115 Assignment 6 Write a complete program, including javadoc comments, to process voter statistics Input...

    CISC 1115 Assignment 6 Write a complete program, including javadoc comments, to process voter statistics Input to Program: A file containing lines of data, such that each line has 2 integers on it The first integer represents a zip code (in Brooklyn or the Bronx), and the second represents the number of voters in that zip code. Output: All output may be displayed to the screen. In main: 1. Your program will read in all of the data in the...

  • c language with comments please! Name this program one.c- This program reads integers from standard input...

    c language with comments please! Name this program one.c- This program reads integers from standard input until end-of-file (control-d) and writes them to either the file good or the file bad. Write the first number to the file good. After that, as long as the number is larger than the one it read before (the previous value, write that number to the file good. Otherwise, write that number to the file bad. Two sample executions are shown below good bad...

  • Please write a C++ Program for the following problem >> Vector: Calculation the sum of each...

    Please write a C++ Program for the following problem >> Vector: Calculation the sum of each two adjacent Numbers.... >>【Description】 Read integer numbers from keyboard and store them into a vector. Calculation the sum of each two adjacent Numbers, store the result into another vector. Then output the result inversely. For example, if the input is 1 2 3 4 5 then the output is 9 7 5 3 【 Input】 There are 5 test cases. For each case, there...

  • This Java program reads an integer from a keyboard and prints it out with other comments....

    This Java program reads an integer from a keyboard and prints it out with other comments. Modify the comments at the top of the program to reflect your personal information. Submit Assignment1.java for Assignment #1 using Gradescope->Assignemnt1 on canvas.asu.edu site. You will see that the program has a problem with our submission. Your program is tested with 4 testcases (4 sets of input and output files). In order to pass all test cases, your program needs to produce the same...

  • Write a C program as follows: Single source code file Requests the user to input two...

    Write a C program as follows: Single source code file Requests the user to input two integer numbers Requests the user to make a choice between 0 (add), 1 (subtract), or 2 (multiply) Declares three separate functions Uses a pointer to these three functions to perform the requested action Outputs the result to the screen Submit your program source code file to this assignment. Sample Output Enter first integer number: 15 Enter second integer number: 10 Enter Choice: 0 for...

  • PLEASE INCLUDE COMMENTS In java Create a Java Program Add the following comments at the beginning...

    PLEASE INCLUDE COMMENTS In java Create a Java Program Add the following comments at the beginning of the file: Your name. The name of the class(es) used in the program. The core concept (found below) for this lesson. The date the program was written. Include a recursive method separate from the main method that will add together all of the even numbers between and including 1 and the value the user supplies. For instance, if the user enters 10 then...

  • Having trouble coding this for C++. Please add comments for better understanding! Write a program that...

    Having trouble coding this for C++. Please add comments for better understanding! Write a program that draws two triangles by using a character 'X' as examples below. The program must prompts a user for an integer larger than 2. The user input will then be used to set the size of the output. If a user input an even value, the program will increase it by one before drawing the box. Example 1: Input: 3 Output: XXX XXX XX XX...

  • Within this C program change the input to a file instead of individual input from the...

    Within this C program change the input to a file instead of individual input from the user. You must take the input file name on the command line. Be sure to have simple error checking to be sure the file exists and was successfully opened. The input file will have the following format: First line: Number of students Second Line: Number of grades Third Line: Student Names, space delimited Fourth+ Line(s): Grades for all students for one assignment, space delimited....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT