Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

C++ Arrays Crashing Program?


  • Please log in to reply
12 replies to this topic

#1 haun

haun

  • Members
  • 156 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Denver, CO
  • Local time:09:24 PM

Posted 02 January 2009 - 11:47 PM

Ok so i am doing some C++ programing on a random number generator that takes those numbers and puts them into arrays of 10 (aka array[9]) but it seems that when doing the loop to fill all those spots it crashes.

Here is the code:
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <string.h>

using namespace std;

const int full = 9; //max # of intergers
int next; //used as another portion to the rule
int cnt = 0; //Count
int pre = 0; //Pre number (used to place the humber in the array
int array1[9]; //First line array (10 digets)
int array2[9]; //Second line array (10 digets)
int array3[9]; //third line array (10 digets)

int main(int argc, char *argv[])
{
	 while( pre != 10 ) //ends loop when arrays are full
	{
			   srand( rand()*(unsigned)time( NULL ) ); //Random number generator
			   next = (rand() % full + 2) % -full; //Next creator for array1
					array1[pre] = (rand() % full)*(rand() % next); //Array1 creator
			   next = (rand() % full + 2) % -full; //Next creator for array2
					array2[pre] = (rand() % full)*(rand() % next); //Array2 Creator
			   next = (rand() % full + 2) % -full; //Next creator for array3
					array3[pre] = (rand() % full)*(rand() % next); //Array3 Creator
			   pre = pre+1; //Moves pre one over 1 (places the next number one over
			   cnt = cnt+1; //Adds to loop count
	}
cout << array1[0] << '\n' << array2[0] << '\n' << array3[0] << '\n';
	system("PAUSE");
	return EXIT_SUCCESS;
}

Now it is well commented, so if you have any questions about what it is suppose to do, then just ask.

I also did some debugging like a good programmer and found that if you loop the generator and the display infinintly it only puts out 4 loops then crashes.

What seems to be my problem?

Edited by haun, 02 January 2009 - 11:47 PM.

Annnnd you're, asking, me...

BC AdBot (Login to Remove)

 


#2 sausage

sausage

  • Members
  • 388 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Louisville Colorado
  • Local time:09:24 PM

Posted 03 January 2009 - 07:25 AM

I'm no C++ maniac but I think you got the loop and array number backwards, you need the arrays to hold 10 and the loop to go up to 9, that's what I see from your code (Having absolutely NO idea what it does, I could figure it out but I have a full day ahead of me and I just pulled an all niter.... failz to the sausage....)

EDIT: Well, maybe not, I tried that and it still crashes on moi.... 5:20 in the morning haven't slept since noon and not sleeping til 4:00 tomorrow morning I will keep trying but I probably won't get anywhere, and is it supposed to display random numbers? that's all I got on what it was supposed to do but... what the hell is the end, display array[0] on all 3 arrays, that doesn't call for any need for arrays....

EDIT2: ok, so switching while pre != 9 into while pre != 1 made it work... kind of... it displays 0 0 15, however. making it pre != 2 makes haywire again... giving up.... by the way don't think I'm a genius C++ dude, I spent maybe 3 hours teaching myself the language and kind of gave up, I do know some stuff though... Oh, and one more thing, if you change pre in the while loop to cnt you get a different output, might want to see what that does in your debugger, mine hates me. Night!

Edited by sausage, 03 January 2009 - 07:36 AM.

If I'm posting, I probably have something horribly wrong with my computer, there's no obvious explanation for it, that's just the way it is.

#3 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:24 PM

Posted 03 January 2009 - 09:21 AM

You have your indexes and array sizes screwed up. Try populating a single array, and I think you will see what your problem is. If you can get one array to populate properly, then you can do 4 or 14 at a time. In an array of size 9, which is how you instantiated them, the largest index is 8. Also, what happens when next = 0?

#4 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:08:24 PM

Posted 03 January 2009 - 09:39 AM

Unrelated but still a bug:
srand( rand()*(unsigned)time( NULL ) ); //Random number generator
No reason to call rand to initialize rand. Also, the random number generator shouldn't be re-initialized each loop.

Move this line out of the loop and make it
srand( time( NULL ) );

Billy3

Edited by Billy O'Neal, 03 January 2009 - 09:43 AM.

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#5 haun

haun
  • Topic Starter

  • Members
  • 156 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Denver, CO
  • Local time:09:24 PM

Posted 03 January 2009 - 10:12 AM

its the next generators! with those turned off it seems to work, but I'd like to have the Next generators in there.

Thanks for the array correction, i had always thought that computers went from 0-9 as [10] not 1-10 like we do.

As for the rand in the seed, I have used that same seed in many other projects and it turns out the most random numbers in my eyes, and it hasnt ever caused a bug, so im going to keep it.

I updated the code a bit:
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <string.h>

using namespace std;

const int full = 10; //max # of intergers
int next = 10; //used as another portion to the rule
int cnt = 0; //Count
int pre = 1; //Pre number (used to place the humber in the array
int array1[10]; //First line array (10 digets)
int array2[10]; //Second line array (10 digets)
int array3[10]; //third line array (10 digets)

int main(int argc, char *argv[])
{
	while(1>0){
	srand( rand()*(unsigned)time( NULL ) ); //Random number generator
	 while( pre <= 10 ) //ends loop when arrays are full
	{
			  //next = (rand() % full + 2) % -full; //Next creator for array1
					array1[pre] = ((rand() % full)*(rand() % next)) % full; //Array1 creator
			  //next = (rand() % full + 2) % -full; //Next creator for array2
					array2[pre] = ((rand() % full)*(rand() % next)) % full; //Array2 Creator
			  //next = (rand() % full + 2) % -full; //Next creator for array3
					array3[pre] = ((rand() % full)*(rand() % next)) % full; //Array3 Creator
				 
			   pre = pre+1; //Moves pre one over 1 (places the next number one over
			   cnt = cnt+1; //Adds to loop count
	}
cout << array1[1] << '\n' << array2[1] << '\n' << array3[1] << '\n';
cout << "~" << '\n';
cout << array1[2] << '\n' << array2[2] << '\n' << array3[2] << '\n';
cout << "~" << '\n';
cout << array1[3] << '\n' << array2[3] << '\n' << array3[3] << '\n';
cout << "~" << '\n';
cout << array1[4] << '\n' << array2[4] << '\n' << array3[4] << '\n';
cout << "~" << '\n';
cout << array1[5] << '\n' << array2[5] << '\n' << array3[5] << '\n';
cout << "~" << '\n';
cout << array1[6] << '\n' << array2[6] << '\n' << array3[6] << '\n';
cout << "~" << '\n';
cout << array1[7] << '\n' << array2[7] << '\n' << array3[7] << '\n';
cout << "~" << '\n';
cout << array1[8] << '\n' << array2[8] << '\n' << array3[8] << '\n';
cout << "~" << '\n';
cout << array1[9] << '\n' << array2[9] << '\n' << array3[9] << '\n';
cout << "~" << '\n';
cout << array1[10] << '\n' << array2[1] << '\n' << array3[10] << '\n';
	system("PAUSE");
}
	return EXIT_SUCCESS;
}

Edited by haun, 03 January 2009 - 10:16 AM.

Annnnd you're, asking, me...

#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:24 PM

Posted 03 January 2009 - 10:17 AM

Then you need to make sure that next never equals zero. What compiler/ide are you using?

#7 haun

haun
  • Topic Starter

  • Members
  • 156 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Denver, CO
  • Local time:09:24 PM

Posted 03 January 2009 - 10:20 AM

Dev-C++. LOVE IT!

So easy for a noob like me!
Annnnd you're, asking, me...

#8 haun

haun
  • Topic Starter

  • Members
  • 156 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Denver, CO
  • Local time:09:24 PM

Posted 03 January 2009 - 10:23 AM

Groov you are a genius!!!!!!!!

IT LIVES!!!!

Now i just need to do some other stuff to it!

THANKS GUYS!
Annnnd you're, asking, me...

#9 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:24 PM

Posted 03 January 2009 - 10:25 AM

Dev-C++. LOVE IT!

So easy for a noob like me!

Except it should have warned you about the indexing issues, and it should have told you about the divide by zero errors. I can't think that it is too useful if it doesn't help you figure out what you are doing wrong. :thumbsup:

#10 haun

haun
  • Topic Starter

  • Members
  • 156 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Denver, CO
  • Local time:09:24 PM

Posted 03 January 2009 - 10:37 AM

Aaahhhhh YES!!!!

My own Universal Machine!!!
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <string.h>

using namespace std;

const int full = 10; //max # of intergers
int next; //used as another portion to the rule
int cnt = 0; //Count
int pre = 1; //Pre number (used to place the humber in the array
int array1[10]; //First line array (10 digets)
int array2[10]; //Second line array (10 digets)
int array3[10]; //third line array (10 digets)

int main(int argc, char *argv[])
{
	srand( rand()*(unsigned)time( NULL ) ); //Random number generator
	while(1>0){
	
	pre = 1;
	 while( pre <= 10 ) //ends loop when arrays are full
	{
			
			  next = ((rand() % full + 2) % -full)+1; //Next creator for array1 [none of these seem to work.
					array1[pre] = ((rand() % full)*(rand() * next)) % full; //Array1 creator
			  next = ((rand() % full + 2) % -full)+1; //Next creator for array2
					array2[pre] = ((rand() % full)*(rand() * next)) % full; //Array2 Creator
			  next = ((rand() % full + 2) % -full)+1; //Next creator for array3
					array3[pre] = ((rand() % full)*(rand() * next)) % full; //Array3 Creator
				 
			   pre = pre+1; //Moves pre one over 1 (places the next number one over
			   cnt = cnt+1; //Adds to loop count
	}
cout << array1[1] << " " << array1[2] << " " << array1[3] << " " << array1[4] << " " << array1[5] << " " << array1[6] << " " << array1[7] << " " << array1[8] << " " << array1[9] << " " << array1[10] << '\n'; 
cout << array2[1] << " " << array2[2] << " " << array2[3] << " " << array2[4] << " " << array2[5] << " " << array2[6] << " " << array2[7] << " " << array2[8] << " " << array2[9] << " " << array2[10] << '\n';
cout << array3[1] << " " << array3[2] << " " << array3[3] << " " << array3[4] << " " << array3[5] << " " << array3[6] << " " << array3[7] << " " << array3[8] << " " << array3[9] << " " << array3[10] << '\n';
	//system("PAUSE")
}
	return EXIT_SUCCESS;
}

Try it. note the pattern of numbers divisible by 2.
Annnnd you're, asking, me...

#11 haun

haun
  • Topic Starter

  • Members
  • 156 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Denver, CO
  • Local time:09:24 PM

Posted 03 January 2009 - 10:40 AM

Oh well, the one i have is Beta 5. and im sure that its not too concerned about my math abitites.

"Cant save them all Hasselhoff'
Annnnd you're, asking, me...

#12 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:08:24 PM

Posted 03 January 2009 - 11:17 AM

Might I suggest a pair of FOR loops instead of the WHILEs here? It would me much clearer and easier to read:
for (;;) //infinite loops are usually done with FOR in C... don't ask me why
{
for (int pre = 0; pre < 10; pre++)
{
array1[pre] = rand() % full; //Array1 creator
array2[pre] = rand() % full; //Array2 Creator
array3[pre] = rand() % full; //Array3 Creator
}
cout << array1[1] << " " << array1[2] << " " << array1[3] << " " << array1[4] << " " << array1[5] << " " << array1[6] << " " << array1[7] << " " << array1[8] << " " << array1[9] << " " << array1[10] << '\n';
cout << array2[1] << " " << array2[2] << " " << array2[3] << " " << array2[4] << " " << array2[5] << " " << array2[6] << " " << array2[7] << " " << array2[8] << " " << array2[9] << " " << array2[10] << '\n';
cout << array3[1] << " " << array3[2] << " " << array3[3] << " " << array3[4] << " " << array3[5] << " " << array3[6] << " " << array3[7] << " " << array3[8] << " " << array3[9] << " " << array3[10] << '\n';
}
I'm missing the point of all the stuff with next though.
EDIT: nvm.. makes numbers divisible by two lol.
Billy3

Edited by Billy O'Neal, 03 January 2009 - 11:19 AM.

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#13 haun

haun
  • Topic Starter

  • Members
  • 156 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Denver, CO
  • Local time:09:24 PM

Posted 04 January 2009 - 12:54 PM

Its just an additive to the rule, im working on a better rule.

Thanks!
Annnnd you're, asking, me...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users