News: 11 March 2016 - Forum Rules

### Author Topic: Need help. [SOLVED!]  (Read 5606 times)

#### InfamousKnight

• Sr. Member
• Posts: 321
##### Need help. [SOLVED!]
« on: December 19, 2012, 05:36:08 pm »
Why doesn't this output anything?
Code: [Select]
`#include<iostream>using namespace std;char map[30][30];int cx = 4;int cxm = 7;int cy = 4;int cym = 7;int x = 5;int y = 5;char input;main() { for(int gx = 0; gx <= 30; gx++) { for(int gy = 0; gy <= 30; gy++) map[gx][gy] = 'X'; } for(int gx = 5; gx <= 15; x++) { for(int gy = 5; gy <= 15; gy++) map[gx][gy] = ' '; } while(true) { for(int i = 0; i <= 55; i++) { cout << endl; } for(int vy = cy; vy <= cym; vy++) { for(int vx = cx; vx <= cxm; vx++) cout << map[vy][vx]; cout << endl; } cin >> input; switch(input) { case 'd': if(map[y+1][x] != ' ') break; map[y][x] = ' '; y++; map[y][x] = '@'; cy++; cym++; break; case 'a': if(map[y-1][x] != ' ') break; map[y][x] = ' '; y--; map[y][x] = '@'; cy--; cym--; break; case 'w': if(map[y][x-1] != ' ') break; map[y][x] = ' '; x--; map[y][x] = '@'; cx--; cxm--; break; case 's': if(map[y][x+1] != ' ') break; map[y][x] = ' '; x++; map[y][x] = '@'; cx++; cxm++; break; } }}`I'm trying to recreate a map system I made when I was 16. With clean code that is. You do not want to see my old code. It has like unused variables and sloppy indents due to copy and pasting old code off a forum. Which was my code. I pasted it into dev-cpp which wasn't a very good idea.
« Last Edit: December 19, 2012, 06:31:28 pm by InfamousKnight »

#### Garoth Moulinoski

• Hero Member
• Posts: 6290
• Disliking that smoke.
##### Re: Need help.
« Reply #1 on: December 19, 2012, 05:50:52 pm »
I think you have an extra }

Do you get any error message at all?

Edit: Nevermind. I missed one { in the loop.

I suggest doing this to make looking up brackets easier:
Code: [Select]
`class main{   //code   while (...)   {       //code       for(...)       {             //code       }       //code   }   //code}`
Edit: Maybe this can help?
« Last Edit: December 19, 2012, 05:56:12 pm by Garoth Moulinoski »
Who will quote me next?
Disclaimer: If it sounds wrong, I may have been posting while asleep.

#### BRPXQZME

• Hero Member
• Posts: 4572
• じー
##### Re: Need help.
« Reply #2 on: December 19, 2012, 05:54:20 pm »
Code: [Select]
` for(int gx = 5; gx <= 15; x++) {`
we are in a horrible and deadly danger

#### Garoth Moulinoski

• Hero Member
• Posts: 6290
• Disliking that smoke.
##### Re: Need help.
« Reply #3 on: December 19, 2012, 05:56:54 pm »
Code: [Select]
` for(int gx = 5; gx <= 15; x++) {`

Or that. That'll definitely create some sort of infinite loop or bug otherwise. So much for for loops being safer to use than while loops (Not saying that for loops are useless; I use them far more than while loops)
Who will quote me next?
Disclaimer: If it sounds wrong, I may have been posting while asleep.

#### InfamousKnight

• Sr. Member
• Posts: 321
##### Re: Need help.
« Reply #4 on: December 19, 2012, 06:22:35 pm »
I think I have to switch around the directions too. Thanks for your help! I honestly didn't see that.

That's strange. I thought array coordinates were the complete opposite of the algebra coordinates.. Like instead of xy it would be yx. But they're the same. Like they're not reversed.
« Last Edit: December 19, 2012, 06:30:46 pm by InfamousKnight »

#### BRPXQZME

• Hero Member
• Posts: 4572
• じー
##### Re: Need help. [SOLVED!]
« Reply #5 on: December 19, 2012, 06:42:20 pm »
They are not the same at all! Algebra coordinates stretch off to infinity and array coordinates are limited, for one. In fact, you exceed these limits in your first set of for loops. The next difference is why you don’t crash anything: the location after map[r][29] is map[r+1][0] for r<29.

Also, these nested for loops are very inefficient ways to handle character arrays and the only reason it’s not causing trouble is that these are not very big arrays.
we are in a horrible and deadly danger

#### InfamousKnight

• Sr. Member
• Posts: 321
##### Re: Need help. [SOLVED!]
« Reply #6 on: December 19, 2012, 08:58:04 pm »
They are not the same at all! Algebra coordinates stretch off to infinity and array coordinates are limited, for one. In fact, you exceed these limits in your first set of for loops. The next difference is why you don’t crash anything: the location after map[r][29] is map[r+1][0] for r<29.

Also, these nested for loops are very inefficient ways to handle character arrays and the only reason it’s not causing trouble is that these are not very big arrays.
That's not what I meant. I mean the ordered pairs. Like xy in the algebra system. I didn't mean that arrays and algebra coordinates were the same. I meant the ordered pairs are the same.

#### Garoth Moulinoski

• Hero Member
• Posts: 6290
• Disliking that smoke.
##### Re: Need help. [SOLVED!]
« Reply #7 on: December 19, 2012, 09:14:53 pm »
You shouldn't dismiss BRP's warning though:

Also, these nested for loops are very inefficient ways to handle character arrays and the only reason it’s not causing trouble is that these are not very big arrays.

It might not be a big a deal in this situation, but realize that every time you have a loop, you're essentially telling the computer to do a certain amount of instructions any number of times. Although each instruction takes constant time, each loop adds n to that time. So, your main takes at least a bit more than O(n^2). I wasn't very good at calculating Big O, but I can at least tell by just looking at it that it's at minimum that much (which it isn't, it's a little more than that). O(n^2) isn't very efficient, with the best you can hope is probably O(log n) and O(n) (and O(1) but chances are, you're not doing anything interesting in that situation).

Edit: 2n + n(2n), so your thing takes O(n^2), I think. Someone who remembers that may be able to provide a more accurate answer if mine is not.
Who will quote me next?
Disclaimer: If it sounds wrong, I may have been posting while asleep.

#### BRPXQZME

• Hero Member
• Posts: 4572
• じー
##### Re: Need help. [SOLVED!]
« Reply #8 on: December 19, 2012, 09:19:19 pm »
That's not what I meant. I mean the ordered pairs. Like xy in the algebra system. I didn't mean that arrays and algebra coordinates were the same. I meant the ordered pairs are the same.
That’s wrong, too. Algebraic coordinates are continuous.

[snip]
It is indeed O(n2), which is indeed no good. You can’t avoid that for the second loop (technically), but in both cases more efficient code would use memset, which will be faster and less error-prone.
we are in a horrible and deadly danger

#### Klarth

• Sr. Member
• Posts: 499
##### Re: Need help. [SOLVED!]
« Reply #9 on: December 19, 2012, 09:57:58 pm »
Why doesn't this output anything?
I know this is solved, but here's some advice: Learn to use the debugger and breakpoints. You can step through your code, view variables, etc as your program is running.

#### snarfblam

• Submission Reviewer
• Hero Member
• Posts: 595
• CANT HACK METROID
##### Re: Need help.
« Reply #10 on: December 19, 2012, 10:02:49 pm »
BRPXQZME, I'm not having a very hard time understanding what he means here. Why are you?

Like instead of xy it would be yx. But they're the same. Like they're not reversed.

In fact, InfamousKnight, you can order them however you like (as long as you're consistent), but there's usually no reason not to stick with [ x ][ y ].

#### Garoth Moulinoski

• Hero Member
• Posts: 6290
• Disliking that smoke.
##### Re: Need help. [SOLVED!]
« Reply #11 on: December 19, 2012, 10:06:58 pm »
It is indeed O(n2), which is indeed no good. You can’t avoid that for the second loop (technically), but in both cases more efficient code would use memset, which will be faster and less error-prone.

Ah, thanks. I was afraid I was rusty...

I know this is solved, but here's some advice: Learn to use the debugger and breakpoints. You can step through your code, view variables, etc as your program is running.

I wanted to give him the benefit of the doubt and hoped he'd done this. But then again, had he used a debugger, he probably would have caught this easily. It's that kind of error... But this is indispensable advice for him. Hopefully, he takes it to heart and starts using a debugger...

(And since you didn't explain what breakpoints are: IK, They're like "flags" where you can tell the debugger to pause the program while it's running; So, you want to check the state of your program at line 150, where something interesting just happened, you put a breakpoint there, run the program in debug and it runs until it reaches line 150 and then it waits for you take a step or hit run again.)
Who will quote me next?
Disclaimer: If it sounds wrong, I may have been posting while asleep.

#### BRPXQZME

• Hero Member
• Posts: 4572
• じー
##### Re: Need help.
« Reply #12 on: December 20, 2012, 12:53:01 am »
BRPXQZME, I'm not having a very hard time understanding what he means here. Why are you?
I understand perfectly well. I also have seen plenty of classmates—most of them 16–19 years old at the time—get cut down due to bad assumptions in all too many years of math classes.
we are in a horrible and deadly danger

#### relminator

• Jr. Member
• Posts: 27
##### Re: Need help. [SOLVED!]
« Reply #13 on: December 21, 2012, 10:50:10 am »
Hi, I know this is solved but aren't you accessing 1 more element than you should in your first for loop accessing char map[30][30];?