Skip to content

Instantly share code, notes, and snippets.

@kf4x
Created June 15, 2012 01:38
Show Gist options
  • Select an option

  • Save kf4x/2934135 to your computer and use it in GitHub Desktop.

Select an option

Save kf4x/2934135 to your computer and use it in GitHub Desktop.
Working with vectors and loops
/*
Javier C
This program is designed to order numbers ASC DESC that user has given
*/
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int first, last;
vector<int> vints;
//User imputs
cout << "Enter your first number => ";
cin >> first;
cout << "Enter your second number => ";
cin >> last;
//inserting numbers into vector and adding in-between numbers
for (int i = first; i <= last; ++i)
{
vints.push_back(i);
}
cout << "Output using for loop" << endl;
//finding last element
int lastelem = vints.size() - 1;
//output numbers ASC
for (int i = 0; i < vints.size(); ++i)
{
//format output with comma's
if(i == lastelem)
{
cout << vints.at(i) << endl;
}
else
cout << vints.at(i) << ",";
}
//output numbers DESC
int i = lastelem;
cout << "Output using while loop" << endl;
while(i >= 0)
{
//format output with comma's
if(i == 0)
{
cout << vints.at(i) << endl;
}
else
cout << vints.at(i) << ",";
i--;
}
cout << "Enough with numbers already" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment