C++ vector and stack introductory exercises (using sorting)

  1. Write a C + + program to read positive real numbers into a vector (in random order)
    Trailing value - 1. Sorts the numbers in ascending order and displays the sorted numbers.
    Your program must include the following two functions:
void sort(vector<float> & v);
void display(vector<float> & v);
Solution to Q1:
#include <iostream>
#include <vector>
using namespace std;
vector<float> numbers;
void sort(vector<float> & v);
void display(vector<float> & v);
int main() {
 float temp;
 temp = 99.9; // just to start the loop
 while (temp > 0.0) {
	 cout << "Enter a positive real number (-1 to stop) ";
	 cin >> temp;
	 if (temp > 0.0) {
void sort(vector<float> & v) {
	 int total, pass, i;
	 float temp;
	 total = v.size();
	 for (pass = 0; pass < total; pass++) {
		 for (i = 0; i < total - 1; i++) {
			 if (v[i] > v[i+1]) {
			 temp = v[i];
			 v[i] = v[i+1];
			 v[i+1] = temp;
void display(vector<float> & v) {
 int total, i;
 total = numbers.size();
 for (i = 0; i < total; i++) {
	 cout << numbers[i] << ", ";
 cout << endl;
  1. In the sorter above, change the first line of the sort function to:
    void sort(vector v);
    (note that you must also change the first line in the function definition.)
    Do not change any other codes. Run the program. Explain what will happen.
    Solution to Q2:

Vectors are not sorted – this is shown on the display. This happens because sorting requires moving data in the vector, that is, changing the vector. If you want to change the parameter, you must pass the parameter to the function using call by reference. By removing the & symbol, we stopped calling parameters by reference. Additional note: parameters do not need to be displayed as called by reference. However, many programmers usually use call by reference for large data structures, because this means that only one pointer is passed to the function. If you delete & (convert it to call by value), the entire data structure will be copied into the function, which will waste time.

  1. Discuss what you need to do to change the above sorter so that it uses arrays
    Not vectors. What is the biggest difference between the two programs? This is for
    Discussion only - there is no need to actually write programs using arrays.
    Solution to Q3:

Using arrays is very similar to using vectors. You need to define the array: float[500]; The only big difference is that you need to guess the maximum size of the array. In the example above, the maximum size is assumed to be 500. This may not be big enough

  1. The following program enters data for a bank account and then searches to find a specific bank
    Account. However, the program cannot run because there are 6 errors. distinguish
    Errors and explain what needs to be done to correct each error.
#include <iostream>
#include <cstdio> // for getchar
#include <vector>
using namespace std;
class bank_account {
 int accnumber;
 string name;
 float balance;
 void loaddata();
 void display();
void enteraccounts();
int main() {
 int search, total, num, i;
 cout << "Which account do you want to display? ";
 cin >> search;
 total = accounts.size();
 for (i = 0; i < total; i++) {
 num = accounts[i].getnum();
 if (num != search) {
void enteraccounts() {
 // enter 3 accounts - this could be changed
 int i;
 bank_account temp;
 for (i = 0; i < 3; i++) {
//--------------- methods for the bank_account ------------
void loaddata() {
 cout << "Enter the bank account number ";
 cin >> accnumber;
 cout << "Enter the name of the account ";
 getline(cin, name);
 cout << "Enter the balance, e.g. 32.45 ";
 cin >> balance;
void bank_account::display() {
 cout << "Data for Account " << accnumber << endl;
 cout << "Name: " << name << endl;
 cout << "Balance: $" << balance << endl << endl;

Solution to Q4:

1) method missing from the class, we need:
int getnum() { return accnumber; }
2) vector has not been defined, we need:
vector<bank_account> accounts;
3) incorrect test inside the if statement, it should be:
if (num == search) {
4) inside "enteraccounts", data is not actually placed in the vector. Insert new line:
5) method must have the class name before the function name, as follows:
void bank_account::loaddata() {
6) in method "loaddata", entering a number (accnumber) before a string (name) will cause no data to be 
entered for the string. Need to flush the input buffer with an extra line as follows:
cin >> accnumber;

5. Suppose the following program has a function that can display all items in the stack (similar to
The "show and empty" function in the comment). Write down the values that will be displayed and
In what order.
stack s;
int main() {
– call function here to display all items
Solution to Q5:

It will show 56, then 27. 56 is the last item placed on the stack, so it is the first item displayed. 83 is placed on the stack, but then deleted using pop

Keywords: C++ data structure

Added by saadatshah on Sun, 26 Dec 2021 22:02:07 +0200