Submitted by rdjere on Tue, 05/22/2012 - 01:29
This tutorial demonstrates how to create vectors in C++, sorting the data elements in the vector from lowest to highest.
Learning Objectives:
- Define doubles.
- Discuss creating a vector in C++.
- Sorting a vector.
- Printing a vector.
- Briefly discuss passing a vector by reference versus passing it by value.
- Create a vector that accepts double precision numbers.
- Allow the user to fill the vector with an unlimited number of numbers of their choice.
- Automatically sort the vector from lowest to highest.
- Show the user the sorted output.
cpp.vectors.0: Creating and Sorting Vectors... by Djere University
The LibreOffice Impress-generated HTML slideshow for this lecture is here:
http://djere.com/lectures/node19lecture/Node19.creating_and_sorting_vect...
Source code begins directly below:
// cpp.vectors.0: Creating and Sorting a Vector // This program creates a vector of doubles. // It prompts the user to fill the vector. // It then sorts the vector from lowest to highest. // Written by Rex Djere. // This source code will be maintained at http://djere.com/node/19 #include<vector> // include this for vector support #include<algorithm> // include this for the sorting algorithm #include<iostream> // include this for input and output support using namespace std; // function prototypes - these are templates of functions that we will define later void printVector(vector<double> &, int ); int getVectorSize(vector<double> & ); int main() { // I organize my variables and vector here at the beginning of the main function. bool addMoreNumbers = 1; // boolean value acts as a choice to continue filling the vector or not double numberAdded; // this variable stores the value added by the user vector<double> myVector (0); // I create my vector, give it the name "myVector", and initialize it with a size of 0. // Welcome and explanatory messages cout << "Welcome to the vector creator and sorter." << endl; cout << "I will create and sort a vector of numbers that you enter." << endl; // start getting input from user cout << "Enter a number (decimals and negative numbers are okay): "; cin >> numberAdded; myVector.push_back(numberAdded); cout << "Enter another number: "; cin >> numberAdded; myVector.push_back(numberAdded); while (addMoreNumbers == 1) { cout << "Enter another number or enter 0 to quit: "; cin >> numberAdded; if (numberAdded != 0) myVector.push_back(numberAdded); if (numberAdded == 0) { addMoreNumbers = 0; break; } } // Input is complete. It's time to sort the vector. cout << endl << "Here are the non-zero numbers that you entered, sorted from lowest to highest: " << endl; sort (myVector.begin(),myVector.end()); // here is the actual sort printVector(myVector, getVectorSize(myVector)); // print the vector return 0; // the main function is of integer type, so it MUST return an integer such as 0 } // below, I actually define the printVector and getVectorSize functions void printVector(vector<double> & vector, int size) { int index; for(index=0; index < size; index++) { cout << vector.at(index) << endl; } } int getVectorSize(vector<double> & vector) { return vector.size(); }