Author | Message | Time |
---|---|---|
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. | April 21, 2004, 1:46 PM |
Mephisto | [quote author=Dyndrilliac link=board=30;threadid=6410;start=0#msg56109 date=1082555200] 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?[/quote] 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: [code] 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; [/code] [quote]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] 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. | April 21, 2004, 2:30 PM |
iago | I would put the numbers into an array: [code]int nums[10]; for(int i = 0; i < 10; i++) cin >> nums[i];[/code] Then, if you're only doing 10 numbers, just use a bubblesort: [code]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]); [/code] you also need this: [code] void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } [/code] That should work, it's untested though :) If you're doing any major sorting, though, you should look up quicksort, shellsort, quickersort, etc. | April 21, 2004, 3:01 PM |
Yoni | [quote author=Mephisto link=board=30;threadid=6410;start=0#msg56116 date=1082557825] 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.[/quote] 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.) | April 21, 2004, 3:17 PM |
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. | April 21, 2004, 3:43 PM |
Mephisto | [quote author=iago link=board=30;threadid=6410;start=0#msg56120 date=1082559664] I would put the numbers into an array: [code]int nums[10]; for(int i = 0; i < 10; i++) cin >> nums[i];[/code] Then, if you're only doing 10 numbers, just use a bubblesort: [code]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]); [/code] you also need this: [code] void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } [/code] That should work, it's untested though :) If you're doing any major sorting, though, you should look up quicksort, shellsort, quickersort, etc. [/quote] 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... | April 21, 2004, 3:52 PM |
Mephisto | (sorry for this quad-post), won't happen again. | April 21, 2004, 4:18 PM |
Mephisto | [quote]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.) [/quote] 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 | April 21, 2004, 4:30 PM |
iago | 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. | April 21, 2004, 4:47 PM |
Mephisto | 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. [code] // 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 ^= [/code] | April 21, 2004, 5:00 PM |
iago | [quote author=Mephisto link=board=30;threadid=6410;start=0#msg56134 date=1082566843] Additionally, all the iso646.h library is, is 11 macros defined to make it more "English-like" when using logical/bitwise operators. [code] <snip> [/code] [/quote] That's a pretty bad way to learn it, I wouldn't recommend it to anybody. But that's just my opinion :) | April 21, 2004, 5:17 PM |
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". | April 21, 2004, 5:23 PM |
Eibro | C++?[code]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() );[/code] | April 21, 2004, 5:48 PM |
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 | April 21, 2004, 6:43 PM |
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. :) | April 21, 2004, 7:14 PM |
Eli_1 | [quote author=Dyndrilliac link=board=30;threadid=6410;start=0#msg56162 date=1082574888] (Like II is the Logical OR, I wanted to know the equivalent of II for the Exclusive OR)[/quote] Logical OR is ||, not ii. | April 21, 2004, 7:29 PM |
Dyndrilliac | My mistake - I was typing quickly and since they looked simlar didn't catch that. | April 21, 2004, 7:42 PM |
iago | [quote author=Dyndrilliac link=board=30;threadid=6410;start=15#msg56167 date=1082576552] My mistake - I was typing quickly and since they looked simlar didn't catch that. [/quote] Easy mistake to make. I was going to point that out too :) So, is your question answered? I don't know if my code works or not, but I'm a little curious :) If you also want to *generate* random numbers, not get them from the keyboard, that will take a little more. | April 21, 2004, 7:45 PM |
Dyndrilliac | Basically, yes, my question was answered, thanks :) However, I don't have MSVC++ on my laptop so I'll have to wait until I get home to try it. And inputting them is fine, I already learned how to use random numbers using the computer clock time as my seed value. | April 21, 2004, 8:02 PM |
Mephisto | [quote author=Dyndrilliac link=board=30;threadid=6410;start=0#msg56109 date=1082555200] 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] No, I do think you're mixed up. There's no such thing as an exclusive or logical operator. There's only exclusive or and logical or, unless I'm mistaken. And when you say you think it's xor in VB and ASM (correct) you describe the logical or, and not the exclusive or. That's why I think you're mixed up. Correct me if I'm wrong. Edit: Ahh, nevermind. I see now, thanks to iago. Sorry about that Dynrilliac...This is why I dislike trying to read people's statements that are confusing and badly written. | April 21, 2004, 11:20 PM |
Myndfyr | [quote author=Mephisto link=board=30;threadid=6410;start=0#msg56116 date=1082557825] [quote author=Dyndrilliac link=board=30;threadid=6410;start=0#msg56109 date=1082555200] 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] 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. [/quote] [quote][/quote] The "Boolean Logical" exclusive OR is pretty cool -- basically, if an odd number of operands are true, then it returns true, otherwise false. As iago pointed out, XOR in C++ is ^and ^=. | April 22, 2004, 3:23 PM |
Dyndrilliac | [quote author=Mephisto link=board=30;threadid=6410;start=15#msg56197 date=1082589627] [quote author=Dyndrilliac link=board=30;threadid=6410;start=0#msg56109 date=1082555200] 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] No, I do think you're mixed up. There's no such thing as an exclusive or logical operator. There's only exclusive or and logical or, unless I'm mistaken. And when you say you think it's xor in VB and ASM (correct) you describe the logical or, and not the exclusive or. That's why I think you're mixed up. Correct me if I'm wrong. Edit: Ahh, nevermind. I see now, thanks to iago. Sorry about that Dynrilliac...This is why I dislike trying to read people's statements that are confusing and badly written. [/quote] http://www.starlink.rl.ac.uk/star/docs/sun95.htx/node412.html Logical Exclusive OR Operator. Excuse me for reversing 2 words. | April 23, 2004, 8:07 PM |
Mephisto | [code] #include <iostream> #include <set> int main() { typedef std::set<int> ISet; ISet num; for (int i = 0, temp; i < 10 && std::cin >> temp; ++i) { num.insert(temp); } ISet::const_iterator it; for (it = num.begin(); it != num.end(); ++it) { std::cout << *it << ' '; } std::cout << std::endl; return 0; } [/code] That may be easier and more efficient to use than Eibro's vector. The set will require you to insert values into the set (using the member function) and then use an iterator to scan through the set and get what you need shown in the second loop. This code is also untested, and I've had problems using templates and STL classes/containers on GCC/G++ so if you are using that (Dev-CPP is an IDE for GCC/G++ IIRC) then it may not work as expected. Compiling on Microsoft's compilers should work however. If it does not work then you might try something more manual which generally works for me on GCC: [code] #include <iostream> #include <set> int main() { typedef std::set<int> ISet; ISet num; /* this is the loop which accomplish what the next 12 lines do. This is generally where gcc screws up for me. for (int i = 0, temp; i < 10 && std::cin >> temp; ++i) { num.insert(temp); } */ int a, b, c, d, e, f, g, h, i, j; std::cin >> a >> b >> c >> d >> e >> f >> g >> h >> i >> j; num.insert(a); num.insert(b); num.insert(c); num.insert(d); num.insert(e); num.insert(f); num.insert(g); num.insert(h); num.insert(i); num.insert(j); ISet::const_iterator it; for (it = num.begin(); it != num.end(); ++it) { std::cout << *it << ' '; } std::cout << std::endl; return 0; } [/code] Note, that because this uses a regular set, you cannot have the same of two values. So if you enter two 2s, you will only get one back when it's displayed. You can use a multiset to accomplish having more than one of the same values. All you need to do to do this is simply replace set with multiset and basically the same functionality that you see applys. | April 28, 2004, 4:49 PM |