• Welcome to Valhalla Legends Archive.
 

Quick Question

Started by Dyndrilliac, April 21, 2004, 08:46 AM

Previous topic - Next topic

Dyndrilliac

Im working on a program to have the user input 10 random numbers and it will automatically sort them in sequencial order, what would be the best method to do this?

Edit: Also, does anyone know what symbols to use for the exclusive or logical operator? I _think_ that is what it's called (xor in VB and ASM), where one or the other can be true for the statement to be true but not both.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

Mephisto

#1
Quote from: Dyndrilliac on April 21, 2004, 08:46 AM
Im working on a program to have the user input 10 random numbers and it will automatically sort them in sequencial order, what would be the best method to do this?

I assume you want to enter 10 numbers on one input line and then output them in the order they entered them or from least to greatest or greatest to least?  Clarify that.

If all you wanted to do was enter 10 numbers on one input line and then output them in the order they enter them you would just do it this way:


using std::cin;
using std::cout;
using std::endl;
cin >> num1 >> num2 >> num3 >> num4 >> num5 >> num6 >> num7 >> num8  >> num9 >> num10
cout << num1 << ", " << num2 << ", " << num3 << ", " << num4 << ", " << num5 << ", " << num6 << ", " << num7 << ", " << num8 << ", " << num9 << ", " << num10 << endl;




QuoteEdit: Also, does anyone know what symbols to use for the exclusive or logical operator? I _think_ that is what it's called (xor in VB and ASM), where one or the other can be true for the statement to be true but not both.

I believe you're mixed up.

xor in languages which support bitwise operators is excluseive OR.
In C/C++ the bitwise operator xor is ^ and it compares two operand's bits, and if the corresponding operands are different (e.g. 0 and 1) then the corresponding bit is set to 1.  If they are the same, the corresponding bit is set to 0.  Think of it as if both bits are different when you xor them, the return is 1, and if they are the same, the return is 0.  You can use the xor (^) by including the iso646.h library or compile with the disable language extensions compiler option (/Za).  Though you should simply beable to use it without doing anything on most modern compilers.

or is the logical operator of "or" in programming languages.  In C/C++ the operator identifier is ||.  When two operands are compared with logical OR the result is a boolean value of true if at least one operand evaluates to true.  If both operands evaluate to false, then the boolean value returned is 0.  It should be noted that before the test begins, each operand is implicitly converted to bool.

iago

I would put the numbers into an array:
int nums[10];
for(int i = 0; i < 10; i++)
 cin >> nums[i];


Then, if you're only doing 10 numbers, just use a bubblesort:

for(int i = 0; i < 10; i++)
 for(int j = i + 1; j < 10; j++)
   if(nums[j] < nums[i])
     swap(&nums[j], &nums[i]);


you also need this:

void swap(int *a, int *b)
{
 int temp = *a;
 *a = *b;
 *b = temp;
}


That should work, it's untested though :)

If you're doing any major sorting, though, you should look up quicksort, shellsort, quickersort, etc.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Yoni

Quote from: Mephisto on April 21, 2004, 09:30 AM
You can use the xor (^) by including the iso646.h library or compile with the disable language extensions compiler option (/Za).  Though you should simply beable to use it without doing anything on most modern compilers.
Hmm, never heard of requiring a library to use xor. The ^ operator should be usable without doing anything special on all compilers including very nonmodern ones.

(I recall writing XorEncode on Borland Turbo C++ 3.0 for DOS during one C class in HS last year. It worked fine, and that compiler's from like 1990.)

Mephisto

It's possible no compilers will require this, and that /Za is already enabled by default on compilers, or that it was never necessary when most compilers were made that we use now.

Mephisto

Quote from: iago on April 21, 2004, 10:01 AM
I would put the numbers into an array:
int nums[10];
for(int i = 0; i < 10; i++)
 cin >> nums[i];


Then, if you're only doing 10 numbers, just use a bubblesort:

for(int i = 0; i < 10; i++)
 for(int j = i + 1; j < 10; j++)
   if(nums[j] < nums[i])
     swap(&nums[j], &nums[i]);


you also need this:

void swap(int *a, int *b)
{
 int temp = *a;
 *a = *b;
 *b = temp;
}


That should work, it's untested though :)

If you're doing any major sorting, though, you should look up quicksort, shellsort, quickersort, etc.


iago, so would I.  However, obviously this person is very confused about C/C++ based on what he's asked in his question (the mix up of bitwise/logical xor and or.  So as a result, it may be best not to use more "advanced" concepts in C/C++ (e.g. arrays) when answering his question and something simple, since I doubt he even knows what an array is...

Mephisto

#6
(sorry for this quad-post), won't happen again.

Mephisto

QuoteHmm, never heard of requiring a library to use xor. The ^ operator should be usable without doing anything special on all compilers including very nonmodern ones.

(I recall writing XorEncode on Borland Turbo C++ 3.0 for DOS during one C class in HS last year. It worked fine, and that compiler's from like 1990.)

You need to include that library for xor and not ^.  Though it's possible (and it works for mine) that you won't need to include that library on most compilers with the disable language extensions compiler option (/Za).

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_bitwise_exclusive_or_operator.asp

iago

#8
I didn't read the second half of his question, but you should still learn to do things the right way, not with num1, num2, etc.

This goes back to a discussion we had about void main() vs. int main().  Just because void is easier, it's a bad way of doing it.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Mephisto

#9
There's nothing "bad" about the way I did it, it's just more manual work.  When he learns arrays he should do it the easier and more functional way.  But since I doubt he understands the concept of arrays and how to use then, he might aswell use the way that matches his skill level, and wait until he learns arrays to do it the way you suggested.  That's just my opinion.  :p

Additionally, all the iso646.h library is, is 11 macros defined to make it more "English-like" when using logical/bitwise operators.


// iso646.h
// required to use the text English-like version of bitwise/logical operators
#define and &&
#define and_eq &=
#define bitand &
#define bitor |
#define compl ~
#define not !
#define not_eq !=
#define or ||
#define or_eq |=
#define xor ^
#define xor_eq ^=

iago

Quote from: Mephisto on April 21, 2004, 12:00 PM
Additionally, all the iso646.h library is, is 11 macros defined to make it more "English-like" when using logical/bitwise operators.


<snip>


That's a pretty bad way to learn it, I wouldn't recommend it to anybody.  But that's just my opinion :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Yoni

I just looked inside iso646.h and I didn't like what I saw. Standard or not, I have renamed it to "Pussy Header #4.h".

Eibro

C++?std::vector<int> in;
int temp;
for ( int i = 0; i < 10 && std::cin >> temp; ++i )
  in.push_back( temp );

std::sort( in.begin(), in.end() );

Eibro of Yeti Lovers.

iago

Mephisto - I reread what he was asking, he basically said "what' the equivolant of xor in VB where one or the other can be true but not both".  Where in there did he say he doesn't understand the difference between or and xor?  

And to answer that question, the symbol in C++ is ^.  That's it.

Yoni - what are pussy headers 1-3?

Eibro - I think he wanted it to be done more manually than that :P
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Dyndrilliac

Thanks for the replies.  ;D

Mephisto, I don't know exactly what you mean by me being mixed up, I was not only correct in naming the logical operator I wanted the C++ representation of(Exclusive OR, I was correct in both what it was and how it was represented in other languages as well).  I don't know how you could propose i'm mixed up, as I looked it up, and, I was pretty clear on it, I just wanted to know what the symbol for it was(Like II is the Logical OR, I wanted to know the equivalent of II for the Exclusive OR.). And yes, I DO know what an array is. <sarcasm>Thanks for sharing your humongous ego, as you must be the greatest C++ programmer ever known</sarcasm> :P

To clarify, I wanted to input 10 random numbers, and order them from least to greatest. Or, what order they would be on a standard positive left to right number line.

Also, Mephisto, Arrays aren't exactly advanced concepts, buddy. One variable storing multiple values of like datatypes, right? ::)

Anyway - Thanks iago/Eibro, seeing as you 2 are the only ones who answered my question. :)
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.