CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: [Newbie] Putting variables into an order

1. Junior Member
Join Date
Apr 2009
Posts
5

## [Newbie] Putting variables into an order

Hello,

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.

Problem 1.
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.

An example of what I want is

float number1 = 1
float number2 = 15
float number3 = 4
float number4 = 40

to become

float next1 = 1
float next2 = 4
float next3 = 15
float next4 = 40

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

Problem 2.

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.

2. ## Re: [Newbie] Putting variables into an order

Code:
```float next1 = 1
float next2 = 4
float next3 = 15
float next4 = 40```
Start by putting your data in a array.

Code:
```float next[4] = {0};

next[0] = 1;
next[1] = 4;
next[2] = 15;
next[3] = 40;```
Now you can say something like ...
Code:
```long lCnt;

for (lCnt = 0; lCnt < 3; lCnt++)
{
if (next[lCnt] > next[lCnt + 1])
{
...........
}
}```

3. Junior Member
Join Date
Apr 2009
Posts
5

## Re: [Newbie] Putting variables into an order

Thankyou Skizmo, I do not fully understand the code you have posted but that is something new for me to look into and learn, thankyou so much, I am just glad that atleast it can be done.

Thankyou

4. Junior Member
Join Date
Apr 2009
Posts
21

## Re: [Newbie] Putting variables into an order

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:

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

6. ## Re: [Newbie] Putting variables into an order

Originally Posted by Skizmo
Code:
```float next1 = 1
float next2 = 4
float next3 = 15
float next4 = 40```
Start by putting your data in a array.

Code:
```float next[4] = {0};

next[0] = 1;
next[1] = 4;
next[2] = 15;
next[3] = 40;```
Now you can say something like ...
Code:
```long lCnt;

for (lCnt = 0; lCnt < 3; lCnt++)
{
if (next[lCnt] > next[lCnt + 1])
{
...........
}
}```
Why are you using floats? Why not integers? Is this C or C++? If C++ you could sort your array using std::sort.

7. Member +
Join Date
Aug 2007
Posts
858

## Re: [Newbie] Putting variables into an order

If you are writing C++ (as opposed to plain C) you should prefer the sorting algorithms that are part of the STL. It really doesn't get much simpler than using them.

Example:
Code:
```#include <algorithm>
#include <functional>
#include <iostream>

int main( )
{
const int arraySize = 5;
float data[arraySize] = { 5.0f, 2.34f, 0.932f, 10.0f, 8.213f };

std::sort(data, data + arraySize, std::less<float>( ));
std::cout << "Sorted least to greatest:" << std::endl;
for (int i = 0; i < arraySize; ++i)
std::cout << data[i] << std::endl;

std::sort(data, data + arraySize, std::greater<float>( ));
std::cout << "Sorted greatest to least:" << std::endl;
for (int i = 0; i < arraySize; ++i)
std::cout << data[i] << std::endl;

return 0;
}```

8. ## Re: [Newbie] Putting variables into an order

Sorry Skizmo, my previous post was intended for the OP (I meant to quote his post, and not your post).

9. Junior Member
Join Date
Apr 2009
Posts
5

## Re: [Newbie] Putting variables into an order

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•