Archived

This topic is now archived and is closed to further replies.

omegasyphon

array question

Recommended Posts

omegasyphon    100
when i go to run the following code, nothing is displayed in my program but if i just hard coded the array it works fine. its reading in a file of just integers
    

	GLuint corners[1000];
	for (int x=0;x<1000;x++)
	{
		cin>>corners[x];
		cin>>corners[x+1];
		cin>>corners[x+2];
		cin>>corners[x+3];
	}

    
Edited by - omegasyphon on October 19, 2001 1:08:25 PM

Share this post


Link to post
Share on other sites
ncsu121978    1344
do you realize that the piece of code you have there would require you to input 4000 values ?
are you typing in these 4000 values ?
when you do
cin >> corners[x+whatever];
that means you must type in a value from the keyboard (or use command line redirection).


"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T

Share this post


Link to post
Share on other sites
Prosper/LOADED    100
Moreover, you erase previous values. Replace this :

for (int x=0;x<1000;x++)

By this :

for (int x=0;x<1000;x+=4)

But anyway, like said ncsu121978, you have to type these values on the keyboard.

Share this post


Link to post
Share on other sites
WayfarerX    130
Also, when x == 999, and cin>>corners[x+1] executes, you''re writing off of the end of your array and into memory that''s not yours.

"If consquences dictate our course of action, it doesn''t matter what''s right, it''s only wrong if you get caught."
- Tool

Share this post


Link to post
Share on other sites
nazpyro    122
lastly...nothing is displayed because you''re not displaying anything....(in addition to all the seemingly "foolish" programming practices you''re doing)

Share this post


Link to post
Share on other sites
omegasyphon    100
oops those should be fin not cin

so basically should this work?
im reading in a bunch of integers

for (int x=0;x<1000;x+=4)
{
fin>>corners[x];
fin>>corners[x+1];
fin>>corners[x+2];
fin>>corners[x+3];
}

Edited by - omegasyphon on October 19, 2001 1:09:13 PM

Share this post


Link to post
Share on other sites
Oluseyi    2112
quote:
Original post by ncsu121978
do you realize that the piece of code you have there would require you to input 4000 values ?

Um, 1003.


// doing this involves 4 additions
for( int x = 0; x < 1000; x += 4)
{
cin >> corners[x];
cin >> corners[x+1];
cin >> corners[x+2];
cin >> corners[x+3];
}

// Since you want to increase x anyway
for( int x = 0; x < 1000; )
{
cin >> corners[x++];
cin >> corners[x++];
cin >> corners[x++];
cin >> corners[x++];
}
// is exactly the same thing. Which means you've unrolled a loop
// that gives you absolutely no performance gain. Do this
// instead:
for( int x = 0; x < 1000; x++)
cin >> corners[x];

Bonus point: why are they the same?

I think you need to rethink what you're trying to do. And I don't think I've ever heard of fin (I suppose you're trying to read in from a file). Try this:

ifstream file("filename.ext");
for( int x = 0; x < 1000; x++ )
ifstream >> corners[x];


Edited by - Oluseyi on October 19, 2001 1:14:15 PM

Share this post


Link to post
Share on other sites
omegasyphon    100
ok if i have a data file like so

8 9 7 8
3 4 5 6
3 5 6 2

trying to read that file in like this doesnt work

ifstream file("data.txt");
for (int x=0;x<15;x++)
{
ifstream>>ary[x];
}


Share this post


Link to post
Share on other sites
omegasyphon    100
ok if i have a data file like so

8 9 7 8
3 4 5 6
3 5 6 2

trying to read that file in like this doesnt work

ifstream file("data.txt");
for (int x=0;x<12;x++)
{
ifstream>>ary[x];
}

it give a syntax error on reading into the array with the arrows


Share this post


Link to post
Share on other sites
Oluseyi    2112
That definition sets the first element of ary to 0.

This code works for me:
  
#include <fstream.h>

ifstream file;

int ary[12];

int main(int argc, char *argv[])
{
file.open("numbers.txt");
for(int i = 0; i < 12; ++i)
file >> ary[i];
for(int j = 0; j < 12; ++j)
cout << ary[j] << endl;
return 0;
}

Share this post


Link to post
Share on other sites
Dactylos    122
quote:
Original post by Oluseyi

--quote--
Original post by ncsu121978
do you realize that the piece of code you have there would require you to input 4000 values ?
--/quote--

Um, 1003.


Actually 4000. The loop is from 0 to 999 (inclusive), and in each iteration 4 values are read. (Only 1003 of them would be stored in the array though).

Share this post


Link to post
Share on other sites
ncsu121978    1344
no Oluseyi....if you look at his original post then you can see that he is only using x++ and not x+=4


"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T

Share this post


Link to post
Share on other sites