Powered by Invision Power Board

 
  Pages: (10) « First ... 2 3 [4] 5 6 ... Last » ( Go to first unread post ) Reply to this topicStart new topicStart Poll

> Giygas learns the art of C++
United States
Giygas
Posted: Sep 25 2009, 01:54 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


Okay, so after a while of trying stuff like
QUOTE

a=b
b=a

I realised that it could work, BUT, b cannot move to a or vice versa because the space is occupied!

So, creating a space without any value, c, I moved a to c. So B moves to A, and then, A(in C) moves to B. Perfect!

QUOTE

#include<iostream>
using namespace std;
int main ()

int a=8;
int b=3;
int c;
a=a^b;
b=a^b;
a=a^b;
cout << a;
cout << b;
system("pause");
return 0;
}
PMEmail Poster
Top
Unspecified
Lightning
Posted: Sep 25 2009, 01:56 PM
Quote Post


Ignorance isn't stupidity but choosing to remain ignorant is
[*][*]

Group Icon
Group: IRC Operators
Posts: 6381
Member No.: 583
Joined: 31-August 04

Status: (0d) [--]


a=a^b;
b=a^b;
a=a^b;

pretty sure you copied this from somewhere. it works for integers, granted, but can you explain why?
also, you never use c. the method you explained in english is exactly correct though.

This post has been edited by Lightning on Sep 25 2009, 01:56 PM


--------------------
click here to change my avatar. / gosh why are you even here lightning
hacker, n.an individual who enjoys learning computer system details and how to capitalize on his or her capabilities...not a criminal.
(from webster's new world hacker dictionary)
fedora linux 10
Fedora 10 Final!

Download today!
quality web comics (stories):
  1. girl genius: adventure! romance! mad science!
  2. punch an' pie: try a slice of life, then swallow.
  3. dresden codak: most interesting comic ever
quality web comics (one-shots):
  1. a softer world: truth and beauty bombs
  2. smbc: saturday morning breakfast cereal
  3. buttersafe: pictures and probably some words
"Religion is comparable to a childhood neurosis." - Sigmund Freud
“It is not by delusion, however exalted, that mankind can prosper, but only by unswerving courage in the pursuit of truth.” - Bertrand Russell
“To kill an error is as good a service as, and sometimes better than, the establishing of a new truth or fact.” - Charles Darwin
PMUsers WebsiteMSN
Top
United States
Giygas
Posted: Sep 25 2009, 02:00 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


QUOTE (Lightning @ Sep 25 2009, 01:56 PM)
a=a^b;
b=a^b;
a=a^b;

pretty sure you copied this from somewhere.  it works for integers, granted, but can you explain why?
also, you never use c

Yeah, I couldn't find the right way to show that A moves to C, for some reason doing
A=C
B=A
C=A
comes up with this large string of numbers :C. I found someone suggesting using "XOR" which I searched up on google, aparently using the ^ symbol.
Does this count as cheating...?
Alas, there is much betters ways for me to find out, of course.


PMEmail Poster
Top
Unspecified
Lightning
Posted: Sep 25 2009, 02:01 PM
Quote Post


Ignorance isn't stupidity but choosing to remain ignorant is
[*][*]

Group Icon
Group: IRC Operators
Posts: 6381
Member No.: 583
Joined: 31-August 04

Status: (0d) [--]


A=C
B=A
C=A

you have them all backwards (and the last one should store C into B).

C=A
A=B
B=C


--------------------
click here to change my avatar. / gosh why are you even here lightning
hacker, n.an individual who enjoys learning computer system details and how to capitalize on his or her capabilities...not a criminal.
(from webster's new world hacker dictionary)
fedora linux 10
Fedora 10 Final!

Download today!
quality web comics (stories):
  1. girl genius: adventure! romance! mad science!
  2. punch an' pie: try a slice of life, then swallow.
  3. dresden codak: most interesting comic ever
quality web comics (one-shots):
  1. a softer world: truth and beauty bombs
  2. smbc: saturday morning breakfast cereal
  3. buttersafe: pictures and probably some words
"Religion is comparable to a childhood neurosis." - Sigmund Freud
“It is not by delusion, however exalted, that mankind can prosper, but only by unswerving courage in the pursuit of truth.” - Bertrand Russell
“To kill an error is as good a service as, and sometimes better than, the establishing of a new truth or fact.” - Charles Darwin
PMUsers WebsiteMSN
Top
United States
Giygas
Posted: Sep 25 2009, 02:03 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


QUOTE (Lightning @ Sep 25 2009, 02:01 PM)
A=C
B=A
C=A

you have them all backwards (and the last one should store C into B).

C=A
A=B
B=C

...ahaha... backwards -_-

So I guess my original try was close, just got the positions wrong.
:3

So the final code would be
QUOTE

#include<iostream>
using namespace std;
int main ()

int a=8;
int b=3;
int c;
c=a;
a=b;
b=c;
cout << a;
cout << b;
system("pause");
return 0;
}

I feel empowered... :D

This post has been edited by Giygas on Sep 25 2009, 02:04 PM
PMEmail Poster
Top
United States
Giygas
Posted: Sep 25 2009, 02:36 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


Alright...
So I know how to swap variables. I've used it in simple things...
So what should I test my knowledge of them with?
PMEmail Poster
Top
United States
RetroXYZ
Posted: Sep 25 2009, 03:05 PM
Quote Post


Standard Member
[*][*]

Group Icon
Group: Members
Posts: 2714
Member No.: 4325
Joined: 27-August 07

Status: (0d) [--]


QUOTE (Giygas @ Sep 25 2009, 03:36 PM)
Alright...
So I know how to swap variables. I've used it in simple things...
So what should I test my knowledge of them with?

To be honest, I've never found an instance in which I used them myself.
PMEmail PosterMSN
Top
United States
Giygas
Posted: Sep 25 2009, 03:11 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


QUOTE (RetroXYZ @ Sep 25 2009, 03:05 PM)
To be honest, I've never found an instance in which I used them myself.

Don't discourage me :C!
PMEmail Poster
Top
Unspecified
Lightning
Posted: Sep 25 2009, 03:35 PM
Quote Post


Ignorance isn't stupidity but choosing to remain ignorant is
[*][*]

Group Icon
Group: IRC Operators
Posts: 6381
Member No.: 583
Joined: 31-August 04

Status: (0d) [--]


use the same logic to swap values around in arrays


--------------------
click here to change my avatar. / gosh why are you even here lightning
hacker, n.an individual who enjoys learning computer system details and how to capitalize on his or her capabilities...not a criminal.
(from webster's new world hacker dictionary)
fedora linux 10
Fedora 10 Final!

Download today!
quality web comics (stories):
  1. girl genius: adventure! romance! mad science!
  2. punch an' pie: try a slice of life, then swallow.
  3. dresden codak: most interesting comic ever
quality web comics (one-shots):
  1. a softer world: truth and beauty bombs
  2. smbc: saturday morning breakfast cereal
  3. buttersafe: pictures and probably some words
"Religion is comparable to a childhood neurosis." - Sigmund Freud
“It is not by delusion, however exalted, that mankind can prosper, but only by unswerving courage in the pursuit of truth.” - Bertrand Russell
“To kill an error is as good a service as, and sometimes better than, the establishing of a new truth or fact.” - Charles Darwin
PMUsers WebsiteMSN
Top
United States
Giygas
Posted: Sep 25 2009, 03:41 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


QUOTE (Lightning @ Sep 25 2009, 03:35 PM)
use the same logic to swap values around in arrays

Oh ****.
... I'll try :c

Oh right
just switching them int's with arrays
QUOTE

#include<iostream>
using namespace std;
int main ()

int thing[2]={1,2};
int c;
c=thing[0];
thing[0]=thing[1];
thing[1]=c;
cout << thing[0];
cout << thing[1];
system("pause");
return 0;
}


This post has been edited by Giygas on Sep 25 2009, 03:47 PM
PMEmail Poster
Top
United States
Giygas
Posted: Sep 25 2009, 04:10 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


So, now that I have that down...
How would I even start to shuffle a full deck? I mean, yes, using the same logic, I could shuffle maybe 10 cards, but that would require alot of variables for empty values to go in!
I'm sure there's an easier way, i'm just not thinking hard enough...
PMEmail Poster
Top
United States
RetroXYZ
Posted: Sep 25 2009, 04:38 PM
Quote Post


Standard Member
[*][*]

Group Icon
Group: Members
Posts: 2714
Member No.: 4325
Joined: 27-August 07

Status: (0d) [--]


You only need one temporary variable to swap. Swap 104 times and it should be well-shuffled.
PMEmail PosterMSN
Top
United Kingdom
Kyori
Posted: Sep 25 2009, 04:57 PM
Quote Post


I'm a GUY >.<
[*][*]

Group Icon
Group: Members
Posts: 1467
Member No.: 6088
Joined: 13-March 09

Status: (0d) [--]


I'm confused, you don't need any extra variables to swap 2 integers.

You just do (using A & B):

A = A + B
B = A - B
A = A - B

So, A=5 and B=8:

A = 5 + 8 (=13)
B = 13 - 8 (=5)
A = 13 - 5 (=8)

A=8, B=5

Or am I missing something with what you're trying to do? :/


--------------------
user posted image
PMEmail PosterAOLMSN
Top
United States
Giygas
Posted: Sep 25 2009, 04:59 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


Woah.

This one took a bit of thinking!
After what Retro said about only needing one temp, I was wondering how I was going to program this to adapt to each card change. Then, of course, it hit me-
Use a variabe that increases by 1 every time a new card is shown!
This way, when it calls up Card[1], the variable will increase by one. So it will call up Card[2] the next time!

Then, of course, is the part where I needed to make it random.
The part
int t=card[i];card[i]=card[e];card[e]=t;
shows me switching the variables using the previously described manor, but how was I going to make I random?
Then it hit me- set a range of the numbers and subtract it by the same number that makes the array go in order.
This way, all of the numbers go in order of the array- But they go at random now!

After all of this, I came up with this:

QUOTE

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int card[52];
int e;
int main()
{
srand(time(0));
for(int i=0;i<52;i++)
{
card[i]=i;
}
shuffle:   
{   
for(int i=0;i<(52-1);i++)
{
int e=i+(rand()%(52-i));
int t=card[i];card[i]=card[e];card[e]=t;
}
for(int c=0;c<52; c++)
{
cout <<card[c]<<", ";
}
cout <<endl;
cout << "\nCard's Shuffled! Shuffle again?\n\n\n";
}
system("pause");
goto shuffle;
return 0;
}


Did I pass :D?
PMEmail Poster
Top
United States
Giygas
Posted: Sep 25 2009, 06:47 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


Gonna be expanding upon this for now...

QUOTE

#include<iostream>
using namespace std;

int main ()
{
    lol:
    int myscore=0;
    int mynewcard=0;
    int dealerscore=0;
    int mytype=1;
    srand(time(0));
    int game=1;
    while (game==1)
    {
          hit:
          cout << "\nDealing cards... Done.\n";
          cout << "\nYou have recieved a card with the value of:\n";
          mynewcard=(rand()%12)+1;
          cout << mynewcard;
          myscore=(myscore+mynewcard);
          cout << "\nYour hand has a total value of:\n";
          cout << myscore;
          cout << "\nWhat now?\n1: Hit me\n2: Stand\n";
          cin >> mytype;
          if (mytype==1)
          {
                        goto hit;
          }
          if (mytype==2)
          {
                        game=0;
          }
    }
    while (game==0)
    {
    cout << "\nYou stand with " << myscore << " points.\n";
    cout << "\nThe dealer is now playing...\n";
    cout << "\nThe dealers turn is over. The dealer has:\n";
    dealerscore=(rand()%15)+5;
    cout << dealerscore;
    cout << "\nPoints... Calculating outcome...\n";
    if (dealerscore>21)
    {
                      cout << "\n\n\nDealer busted! You win!\n\n\n";
                      goto lol;
}
if (myscore>21)
{
              cout << "\n\n\nYou busted! You lose!\n\n\n";
              goto lol;
              }
    if (dealerscore>myscore)
    {
                            cout << "\n\n\nThe dealer has more points. The dealer wins.\n\n\n";
                            goto lol;
                            }
    if (myscore>dealerscore)
    {
                            cout << "\n\n\nYou have more points. You win!\n\n\n";
                            goto lol;
                            }
    if (myscore==dealerscore)
    {
                            cout << "\n\n\nIt's a tie.\n\n\n";
                            goto lol;
                            }
system("pause");
return 0;
}
}


I'm BlackJack. Hi.
PMEmail Poster
Top
United States
Giygas
Posted: Sep 27 2009, 04:08 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


Think i'm going to let off the arrays, and move onto uh
Whatever comes next!~
*checks*
Character Sequences. Hmm. Looks good :3
PMEmail Poster
Top
United States
Xgoff
Posted: Sep 27 2009, 04:17 PM
Quote Post


<):|
[*][*][*][*][*]
[*][*]

Group Icon
Group: Members
Posts: 52341
Member No.: 24
Joined: 13-October 03

Status: (0d) [--]


QUOTE (Giygas @ Sep 27 2009, 03:08 PM)
Think i'm going to let off the arrays, and move onto uh
Whatever comes next!~
*checks*
Character Sequences. Hmm. Looks good :3

character sequences are arrays

unless i'm thinking of something totally different


--------------------

This post may contain original research or unverified claims.
Please disregard the above information and contact an administrator.

DISCLAIMER: by sending me (xgoff) a private message, you agree to the directives and their terms specified henceforth:
DIRECTIVE 1 (APPLE): i may or may not reply promptly or at all; and there are no guarantees to the usefulness of the reply. i may not acknowledge whether i have even received your private message
DIRECTIVE 2 (CHILE CON CARNE): as per my view, "private" applies only to the initial transaction, and the material of your message may or may not be made public at my discretion; as this will more than likely be a post in the CCC or IRC, you may not be able to view it
DIRECTIVE 3 (FEATHER DUSTER): you must address me (xgoff) as "Sir Master Xgofficus his Highest and Most Awesome the Third"; failure to comply with this term may invoke one or both of the above directives, and i will leave a burning bag of **** on your doorstep
DIRECTIVE 4 (BOOTSTRAP): if you have read this disclaimer, please private message me promptly, in compliance with the above terms, so i can ensure you are capable of following directions you idiot
this concludes the test of the emergency disclaimer system, your scheduled programming will now continue. satisfaction guaranteed, and 100% cash back available under certain circumstances; restrictions may or may not apply within your place of residence
NOTICE: these directives and their terms may change at any time, without notice; as a private message transaction to myself assumes an understanding and full compliance of the above, you should ensure you are fully aware of the above terms at any point before sending a private message; any message received is assumed to have been sent in compliance with the above

QUOTE
(5:25:58 PM) Mikau: xgoff
(5:26:00 PM) Mikau: guess what
(5:26:04 PM) Xgoff: chicken butt
(5:26:09 PM) Mikau: **** you
PMEmail PosterUsers WebsiteAOLMSN
Top
United States
Giygas
Posted: Sep 27 2009, 04:20 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


QUOTE (Xgoff @ Sep 27 2009, 04:17 PM)
character sequences are arrays

unless i'm thinking of something totally different

Oh hey, they are.
... *reads*

Yeah, just arrays that store data that you can input. Like names.
:3

Then I guess I go onto...
Pointers D:!

edit: holy **** thats alot to read about pointers :c

This post has been edited by Giygas on Sep 27 2009, 04:20 PM
PMEmail Poster
Top
United States
Baroque
Posted: Sep 27 2009, 04:37 PM
Quote Post





Group Icon
Group: Members
Posts: 817
Member No.: 6076
Joined: 11-March 09

Status: (0d) [--]


pointers should not take so long to explain it is why i never got it

pointers store memory addresses instead of values

meaning instead of copying an entire twelve-page essay into the clipboard, you would copy a pointer into the clipboard where the essay starts and then go

RETRIEVE_WHATEVER_IS_IN_THIS_MEMORY_ADDRESS(pointer);

which is incredibly more efficient

(just have to hope the essay's data is contiguous. i guess it won't be?)


--------------------


DeviantArt (DA needs update) | Clean FurAffinity Clean FA | Why I Cut Quotes
You see things, and you ask, 'Why?' But I dream things that never were, and I ask, 'Why not?'


QUOTE (Ryo)
its the correct form of to.........lemme run it down for ya

to is used when your talking about the number. good way to remember this: to has to letters

too is used when your talkin about actions in stuff.

two is used when your talking about places and locations
PMEmail PosterAOL
Top
United States
Giygas
Posted: Sep 27 2009, 04:42 PM
Quote Post


Standard Member
[*]

Group Icon
Group: Members
Posts: 910
Member No.: 4767
Joined: 24-November 07

Status: (0d) [--]


QUOTE (Baroque @ Sep 27 2009, 04:37 PM)
pointers should not take so long to explain it is why i never got it

pointers store memory addresses instead of values

meaning instead of copying an entire twelve-page essay into the clipboard, you would copy a pointer into the clipboard where the essay starts and then go

RETRIEVE_WHATEVER_IS_IN_THIS_MEMORY_ADDRESS(pointer);

which is incredibly more efficient

(just have to hope the essay's data is contiguous. i guess it won't be?)

Yeah, aparently, it's very useful.
I've already written a very, very small thing ( I always try to figure out the most basic operation before lookng at the example code ), although it appeared to be complicated at first, it's not :D

QUOTE

#include<iostream>
using namespace std;

int main ()
{
    int strength=5;
    int *tommy = &strength;
    cout << *tommy;
    system("pause");
    return 0;
}

It's just like saying that a=b, only instead of using b's value, it uses it's reference.
PMEmail Poster
Top
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

  Topic Options Topic Options Pages: (10) « First ... 2 3 [4] 5 6 ... Last » Reply to this topicStart new topicStart Poll

 




[ Script Execution time: 0.1157 ]   [ 13 queries used ]   [ GZIP Enabled ]   [ Server Load: 1.15 ]