First I would like to welcome myself and say hi, I love the site and have spent a little time reading and just started writing some C++ to play around with. All of my code is written for the console at the moment and I hope to change that one day, but one step at a time.
I have two problems, I am not expecting someone two write the code for me but to point me in the right direction would be tops.
I am using cin to put data into my program, I am then storing the data as a float. I have around 10 floats, I would like to then put those floats in running order from lowest to highest and give them a new syntax (i think it is that, called please correct me if I am wrong) or in lame terms a new float variable.
I am also aware those are whole numbers and should be using int, but it is just an example and I wish to have the use of decimals
I know I can write a lot and lot of code to "brute force" what I want but I am hoping for a simple solution, the easiest way I can explain this is the card game 21. I want my program to select the x number of floats and get nearest to 21 without going over 21.
I will be very grateful for any help I can receive, I am totally a newbie here asking newbie questions but am trying to learn something by coding something practical in my eyes.
Thanks a lot guys, much appreciated.
Last edited by Ancient; April 7th, 2009 at 07:51 AM.
Hi man! I'm new here too, but I've been programming for almost two years now (I still have some terrible issues though), but I'll see if I can help
First of all, it is best for you to put all the numbers into an array as Skizmo mentioned.
Then, you have to know that there are a number of algorithms, which are step-by-step procedures for solving a problem in a finite amount of time, used to sort numbers.
One of them, which I prefer to use the most, is called "Selection Sort". It works this way (crappy drawing from my part):
1. Look at the first number in the array
2. Then look at all the other numbers until you find the smallest one
3. If the smallest one you found is smaller than the first one, swap the numbers
4. The next number now becomes the first number
5. Repeat steps 1-4 until you reach the end of the array
Here's the code:
int smallest, position, temp;
for (int i = 0; i < ARRAY_SIZE; i++) // looks at first number in array
smallest = ARRAY_NAME[i]; // saves his value and position (in the array)
position = i;
for (int j = i + 1; j < ARRAY_SIZE; j++) // looks at the one right next to it
if (ARRAY_NAME[j] < smallest) // if it is smaller, save it, else, ignore.
smallest = ARRAY_NAME[j];
position = j;
} // this is done until it reaches the end of the array
temp = ARRAY_NAME[i]; // this swaps numbers
ARRAY_NAME[i] = smallest;
ARRAY_NAME[position] = temp;
// change ARRAY_NAME to the name of your array
// also change ARRAY_SIZE to the size of your array
Hope this helps...
Last edited by EsX_Raptor; April 7th, 2009 at 06:23 PM.
Reason: commented the code
Thankyou EsX_Raptor I am really appreciative of how much effort you went to in explaining and writing that code, it looks like it will be perfect for what I need.
Thankyou also Speedo, I think the code is a little over my head but I am going to do my best to learn what everything means in that code and will learn from it. Hopefully I will be able to take that new knowledge and use it in my own code
PredicateNormative, I was using floats as I wanted to use decimals also. I wrote that in my OP in the middle there. correct me if I am wrong but to my knowledge an int has to be a full number?? Thanks for looking over my code though