Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 20th, 2006, 7:57 PM   #1
ZenOswyn
Newbie
 
Join Date: Nov 2006
Posts: 5
Rep Power: 0 ZenOswyn is on a distinguished road
Strange bug with a 2d array

Edit: I was changing a value that didn't exist. It's been a long day. Mods, feel free to delete this.

Hi all. I have no idea why this is happening, but whenever I change a value in my array to 1, it also changes the value in another place. It only does this when I try to change the last item on the first row.

Here's a bit of code I made to illustrate what's happening in my larger program.

#include <iostream>
using namespace std;

int main() {
	int matrix[3][3];
	
	matrix[0][3] = 1;
	cout << matrix[0][3] << " " << matrix[1][0];
	return 0;
}

That outputs two 1's on my screen

I tried this wth mingw under windows and g++ under linux, same thing. Which leads me to believe I'm probably missing something really simple.

Thanks for the help

Last edited by ZenOswyn; Nov 20th, 2006 at 8:08 PM. Reason: I was being stupid
ZenOswyn is offline   Reply With Quote
Old Nov 20th, 2006, 8:10 PM   #2
Xyhm
Programmer
 
Xyhm's Avatar
 
Join Date: Mar 2006
Posts: 60
Rep Power: 3 Xyhm is on a distinguished road
Quote:
Originally Posted by ZenOswyn View Post
Hi all. I have no idea why this is happening, but whenever I change a value in my array to 1, it also changes the value in another place. It only does this when I try to change the last item on the first row.

Here's a bit of code I made to illustrate what's happening in my larger program.

#include <iostream>
using namespace std;

int main() {
	int matrix[3][3];
	
	matrix[0][3] = 1;
	cout << matrix[0][3] << " " << matrix[1][0];
	return 0;
}

That outputs two 1's on my screen

I tried this wth mingw under windows and g++ under linux, same thing. Which leads me to believe I'm probably missing something really simple.

Thanks for the help
Well, you're assigning a value outside of the range of the array. When you declare an array as, say, int test[3], then the highest index you can assign is test[2]. test[3] is undeclared, and using that part can yield errors of various kinds, depending on the rest of the program.

So, for example, if you try the following you will see that matrix[1][0] will not be identical to the value that is assigned to matrix[0][3]:

#include <iostream>
using namespace std;

int main() {
	int matrix[4][4];
	
	matrix[1][0] = 3;
	matrix[0][3] = 2;
	cout << matrix[0][3] << " " << matrix[1][0];
	return 0;
}
Xyhm is offline   Reply With Quote
Old Nov 20th, 2006, 10:33 PM   #3
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 343
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
arrays start counting on 0. 0,1,2,3... but we normaly count on 1. 1,2,3,4... so when you say int list[3]; the varius indexes are list[0], list [1], list[2].
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
strange symbol hush Visual Basic .NET 4 Aug 26th, 2006 1:35 PM
changing size of an array Eric the Red Java 3 Apr 3rd, 2006 9:19 PM
string array problem MADTech C++ 8 Jul 27th, 2005 7:19 AM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 3:36 AM
Converting 1-dimensional array to 2-dimensional array Tazz_Mission_13 Java 6 Apr 8th, 2005 12:58 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:40 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC