Hi there,
Well I assume that the numbers or letters will have to be put into an array (or have you seen STL containers such as std::vector yet in your classes?).
So you will need to:
* Create an array
* Put a loop around the request for a letter or number so multiple values can be entered by the user
* Insert those values into the array
Now here's where you can do several things.
If you use a plain array, you will need a sorting algorithm (just google c++ sorting algorithm) in order to sort the array. If you use an STL container, you can actually insert the items in-place and keep the container sorted all the time:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <list>
#include <algorithm>
//std::list is an unsorted sequential container allowing fast insertion at any point
//I'm assuming the user chose to enter numbers:
std::list<int> l;
while (y != -1) // -1 exits the loop for entering numbers
{
cout<"Enter number";
cin>>y;
// insert the value in the right spot, use upper_bound() for descending order
l.insert(std::lower_bound(l.begin(), l.end(), y), y);
}
|
You could also use std::map or std::set, which are sorted containers (meaning that they auto-sort themselves whenever you insert a value), but the above should be faster for smaller datasets.
If you have to use regular arrays, create one for both ints and char's:
1 2
|
int number_arr[10]; //creates an array holding 10 ints
char character_arr[10]; //creates an array holding 10 chars
|
Use a for loop to easily access the individual elements and prevent going out of range:
1 2 3 4 5
|
for (int i=0; i < 10; i++)
{
//ask number y
number_arr[i] = y;
}
|
Then figure out a way to sort it. One option is to use the bubble sort algorithm explained here:http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/bubble-sort/
Sorry I tried to evade giving you a straight solution, but that's how you'll learn :)
If you have any further questions, please do let us know.
All the best,
NwN