Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 577 578 [579] 580 581 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 882661 times)

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8670 on: December 13, 2015, 09:37:18 pm »

I hope when you say C you specifically mean C++.
C, C++, and (AFAIK) pretty much every other C-based language all treat arrays the same way (though many of them tend to throw horrible errors if you go beyond the end of the array, which C doesn't always do for you, preferring to instead just pretend like you actually meant to access whatever happened to be at that particular memory location beyond the end of the array instead :P).
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8671 on: December 13, 2015, 09:41:04 pm »

Sorry for the double post but I  think I can rest easy if I solve just two more problems.

1. I have for loop that doesn't work. The x in the beginning isn't defined and if I define it before-hand then it talks about pointers. Which I just don't understand neither the thier role in C++, concept, existence, or how I can get them to work. Jesus this  a lot of problems.

2. If I wanted to return the sum of an array, average of an array, or just a variable how would I do it? I know about typing "Return <variableHere>" but at this point my confidence has been so eroded.

Code: [Select]
void getbooks( int books[], int SIZE)
{

//In here I just display book names and the prices from above.

cout << "Stuff" << endl;

for (x = 0; x < SIZE; x++){

cout << SIZE[x] << endl;
}
}
[code/]


EDIT: Ninja'd

C, C++, and (AFAIK) pretty much every other C-based language all treat arrays the same way (though many of them tend to throw horrible errors if you go beyond the end of the array, which C doesn't always do for you, preferring to instead just pretend like you actually meant to access whatever happened to be at that particular memory location beyond the end of the array instead :P).

Wish we had gone over more arrays in class instead of loops. Oh well I guess I'm just going to have to work harder to know things..again. At this point I wish I knew if I should expect more out of the teacher or of myself. Learning programming at first was much easier than it is now with all the time crunching/rules.
« Last Edit: December 13, 2015, 09:43:02 pm by 3man75 »
Logged

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8672 on: December 13, 2015, 09:52:59 pm »

Sorry for the double post but I  think I can rest easy if I solve just two more problems.

1. I have for loop that doesn't work. The x in the beginning isn't defined and if I define it before-hand then it talks about pointers. Which I just don't understand neither the thier role in C++, concept, existence, or how I can get them to work. Jesus this  a lot of problems.

2. If I wanted to return the sum of an array, average of an array, or just a variable how would I do it? I know about typing "Return <variableHere>" but at this point my confidence has been so eroded.

Code: [Select]
void getbooks( int books[], int SIZE)
{

//In here I just display book names and the prices from above.

cout << "Stuff" << endl;

for (x = 0; x < SIZE; x++){

cout << SIZE[x] << endl;
}
}
Just create the x as part of the loop (which if you are programming in C++ should be something you should be doing every time unless you have a specific reason to give the variable a larger scope):
Code: [Select]
for (int x = 0; x < SIZE; x++) {
I think you might also be slightly confused in the interior part of that loop. Do you mean this:
Code: [Select]
books[x]instead of this:
Code: [Select]
SIZE[x]because the second one is going to throw an error, SIZE is just an int, not an array (unlike books, which is an array).

Edit: Further note, you don't necessarily have to pass the size of an array in C++ IIRC, since every array comes with a built in "ArrayVariable.length" value (i.e. "books.length" would give you the length of the books array), which is always equal to the length of the array. (Though if you were worrying about optimization [which you probably aren't at this level] then you might want to call it out into a variable before the loop since often that's a common things compilers actually won't optimize out in case you are going to be changing the length inside of a loop).
« Last Edit: December 13, 2015, 09:56:38 pm by i2amroy »
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8673 on: December 13, 2015, 10:03:40 pm »

No optimazation isn't a part of the repetoire yet.

I figured that out a while ago (but thank you I appreciate it alot). Right now said loop is giving me a large negative number, which I beleive is trash when I run the loop.

Code: [Select]
int main(){

//The way you initialized books was wrong. This is the correct way to do it.
int books[3] = { 1, 2, 3 };

getbooks(books, 3);//books is their to pass a name while the number 3 is how big the array is.

system("Pause");
return 0;
}

void getbooks( int books[], int SIZE)
{
int x;
//In here I just display book names and the prices from above.

cout << "Stuff" << endl;

for (x = 0; x < SIZE; x++){

cout << books[SIZE] << endl;
}
}
[code/]

Logged

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8674 on: December 13, 2015, 10:10:49 pm »

Here's the logic of what you're currently telling it to do in "getbooks()" broken out into a series of steps:
0) Starting variables:
        "books" = {1, 2, 3}
        "SIZE" = 3
1) create an integer "x" (which as I noted earlier you should just do as part of the loop in C++, as a best practice, in C you need to create it earlier as you are doing right now, though personally I prefer to create them right before the loop in question to allow for easy linking of the two visually.)
2) send the phrase "Stuff" into cout
3) Do the following steps 3 times (x = 0, 1, and 2)
    3a) put books[SIZE] into cout. Since SIZE = 3, this means put the fourth element in books into cout (since the first thing is index number 0)

Your issue is coming from step 3a (which if you are doing C++ I'm surprised isn't giving you an error right now). books only has three elements, (i.e. {1, 2, 3}), so whatever you are asking it to give you doesn't actually exist at the point that you are asking.
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8675 on: December 13, 2015, 10:21:39 pm »

Here's the logic of what you're currently telling it to do in "getbooks()" broken out into a series of steps:
0) Starting variables:
        "books" = {1, 2, 3}
        "SIZE" = 3
1) create an integer "x" (which as I noted earlier you should just do as part of the loop in C++, as a best practice, in C you need to create it earlier as you are doing right now, though personally I prefer to create them right before the loop in question to allow for easy linking of the two visually.)
2) send the phrase "Stuff" into cout
3) Do the following steps 3 times (x = 0, 1, and 2)
    3a) put books[SIZE] into cout. Since SIZE = 3, this means put the fourth element in books into cout (since the first thing is index number 0)

Your issue is coming from step 3a (which if you are doing C++ I'm surprised isn't giving you an error right now). books only has three elements, (i.e. {1, 2, 3}), so whatever you are asking it to give you doesn't actually exist at the point that you are asking.

Okay so what i'm doing wrong is that my array is being read incorrectly? I don't follow and isn't books already initialized as {1,2,3}?
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8676 on: December 13, 2015, 10:23:37 pm »

@3man75:
Your array starts numbering at zero.
The highest position in the array is therefore [2], not [3].

Or more generally, [SIZE-1], not [SIZE].
« Last Edit: December 13, 2015, 10:25:12 pm by TheBiggerFish »
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8677 on: December 13, 2015, 10:25:59 pm »

Your array starts numbering at zero.
This right here. Pretty much everything in computer science (with a few very annoying and fairly rare exceptions) starts numbering at 0 instead of 1. That means that the positions in your array look like this:
Code: [Select]
Array:       {1, 2, 3} Error
              ^  ^  ^  ^
Spot number:  0, 1, 2, 3
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8678 on: December 13, 2015, 10:32:54 pm »

I understand the concept (I think) that the computer sees my {1,2,3} as

books
  • = 1;

books[1] = 2;
books[3] = 3;

Anything after is an error but my question is why is it that my loop is reading junk? I have it set that if it reaches a "3" in the array it stops and refuses to read, I did this by making it so that for(x = 0; x < SIZE;x++)
{
code..
}
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8679 on: December 13, 2015, 10:34:56 pm »

*books[2]=3.

You may want to nobbcode your post...

Also, if you really have it set up that way, it should be terminating properly, but apparently it isn't?

Check that to make sure you aren't using <=.
« Last Edit: December 13, 2015, 10:36:44 pm by TheBiggerFish »
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8680 on: December 13, 2015, 10:37:53 pm »

I understand the concept (I think) that the computer sees my {1,2,3} as

books
  • = 1;

books[1] = 2;
books[3] = 3;

Anything after is an error but my question is why is it that my loop is reading junk? I have it set that if it reaches a "3" in the array it stops and refuses to read, I did this by making it so that for(x = 0; x < SIZE;x++)
{
code..
}
First, books[2] = 3 :P

But the problem is that (at least in the code you posted) is that you aren't telling it to read off each of the members of books 0-2. You're telling it to read off books[SIZE], which equals books[3], which equals an error. If you want it to be reading off the numbers than you are going to need something like books["iterator variable of your loop"].
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #8681 on: December 13, 2015, 10:38:14 pm »

You may want to nobbcode your post...
Code: [Select]
[code] tags are better
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8682 on: December 13, 2015, 10:39:02 pm »

You may want to nobbcode your post...
Code: [Select]
[code] tags are better
Except when you're not actually displaying code.


@i2amroy:Nice catch.
@3man75:What he said.  Don't print books[SIZE] but books[×].

E:Anyone know the tag that turns off bbcode?
So I can actually type [ x ]?
« Last Edit: December 13, 2015, 10:45:32 pm by TheBiggerFish »
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8683 on: December 13, 2015, 11:19:32 pm »

Code: [Select]
[nobbc]
[spoiler]This will be hidden if BBCode is enabled[/spoiler]
[/nobbc]


[spoiler]This will be hidden if BBCode is enabled[/spoiler]
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8684 on: December 14, 2015, 12:36:53 am »

No optimazation isn't a part of the repetoire yet.

I figured that out a while ago (but thank you I appreciate it alot). Right now said loop is giving me a large negative number, which I beleive is trash when I run the loop.

Code: [Select]
int main(){

//The way you initialized books was wrong. This is the correct way to do it.
int books[3] = { 1, 2, 3 };

getbooks(books, 3);//books is their to pass a name while the number 3 is how big the array is.

system("Pause");
return 0;
}

void getbooks( int books[], int SIZE)
{
int x;
//In here I just display book names and the prices from above.

cout << "Stuff" << endl;

for (x = 0; x < SIZE; x++){

cout << books[SIZE] << endl;
}
}

You're meant to use the "x" value as the index inside the loop, not "SIZE", since "books[SIZE]" is outside the array, you're reading junk memory: books[3] for an array that spans indexes 0,1 and 2. Also, SIZE doesn't change each time through the loop, only x does, so even if it worked, it would spit out the same value three times, which isn't what you want. The reason it's "junk" isn't that it's rubbish memory, just that it's some unrelated random memory address that you're asking the CPU to interpret as an int, whereas it could be formatted as any other type of data there.

Another warning sign is that you're not using the for loop variable of "x" at all inside the loop. A loop will keep doing the same thing inside the loop if you don't use a variable that's changing each time.
« Last Edit: December 14, 2015, 12:45:45 am by Reelya »
Logged
Pages: 1 ... 577 578 [579] 580 581 ... 796