5 Replies - 1899 Views - Last Post: 30 September 2007 - 04:09 PM Rate Topic: -----

#1 hollsmeags  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 29-March 07

Help With Random Number and Increment At Point on Array

Post icon  Posted 29 September 2007 - 11:42 AM

can someone please tell me what I need to get this running. I've tried everything, and keep getting errors.


/* Write a program that contains a 20 element integer array. Initialize the array to all zeros.
Repeatedly choose a random number between 0 and 19 and increment
the counter at that position in the array. After each increment,
print the array to the screen with dollar signs around the element just incremented.
Have your program stop when all array elements have been incremented at least once.
Print the number of random numbers generated to fill the array at the end.*/

Requirements:
Only use pointer references to elements in the array (do not use indexes)
Write a function to determine if all elements are filled
Write a function to print the array
The program should run differently each time (generate a new set of random numbers).


#include <cstdlib>
#include <iostream>


const int size =20;
bool not_full (int my_array [size]);
void print (int my_array [size],int *increment);
void zero (int my_array[size]);
void random (int my_array [size],int *last,int*increment);

using namespace std;
int main(int argc, char *argv[])
{ 
	int my_array,last,increment,loop_count;
	
	do 
{
	random(my_array, &last, &increment)
	print (my_array,&last, increment);
	loop count++;
}
while (not_full (my_array));
	  cout <<loop_count;
		
	system("PAUSE");
	return EXIT_SUCCESS;
}


 bool not_full (int my_array[size])
 {
   bool is_zero= false;
   int * counter * last_position;
  
   last_position = my_array + size - 1;
 
 for ( counter = my_array; counter <= last_position; counter ++)
 
   if ( * counter == 0)
	{ 
		is_zero = true;
		}
		return is_zero;
}	
	
 
  void print (int my_array [size])
  
	int counter, 
	int *last position;
	last position = my_array + size -1;
	
	for (counter == my_array + *increment)
	 { 
	   cout<< $<< *counter << $;
	   }
	   else
		cout << " " << * counter << " ";
		
  void zero (int my_array [size])
	 {   
		int *counter;
		int *last_position;
		
		last position = my_array + size - 1;
		
	for (counter = position; my_array <=last_position; position ++)
	
	 {  * counter = 0
	 }
	 }
	 
void random (int my_array [size],int *last,int* increment);
 {
	  
 int random value;
 
 random value = rand % 10;
 my_array = my_array + random

 
 
 last incremement = random value;


Is This A Good Question/Topic? 0
  • +

Replies To: Help With Random Number and Increment At Point on Array

#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Help With Random Number and Increment At Point on Array

Posted 29 September 2007 - 02:13 PM

well, first off, your function bodies need to be enclosed in braces { }, and some of them are not, so the compiler has no idea where one function ends and the next begins.

pass your array and the array size as two separate elements. there are ways to pass it similar to how you are trying to, but they are probably a little more involved than you want.

there is no point at which you allocate an array in your program, either statically or dynamically, so i don't think that this will work at all as you expect. you should probably take a look at Dark_Nexus's tutorial on pointers here; it contains info on both pointer and arrays.

-jjh
Was This Post Helpful? 0
  • +
  • -

#3 hollsmeags  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 29-March 07

Re: Help With Random Number and Increment At Point on Array

Posted 30 September 2007 - 08:30 AM

Okay, made some changes but still having ERRORS :(

Any advice?... My mind is turning to mud



include <cstdlib>
#include <iostream>

int loop_count;
const int size = 20;
bool fill (int my_array [size]);
void print (int my_array [size],int *increment);
void zero (int my_array[size]);
void random (int my_array [size],int *last,int*increment);

using namespace std;
int main(int argc, char *argv[])
{ 
	int my_array[20] = 0;
	int last;
	int increment;
	int loop_count;
	
	do 
	 cout<<" Enter random number (0-9)";
	 cin >> loop_cout;
	random(my_array, &last, *increment)
	print (my_array,&last, *increment);
	loop_count++;

while (fill (my_array));
	  cout <<loop_count;
		
	system("PAUSE");
	return EXIT_SUCCESS;
}


 bool fill (int my_array[size])
 {
   bool not_zero= false;
   int *counter;
   int last
  
   last = my_array + size - 1;
 
 for (*counter = my_array; counter <= last_position; *counter ++)
 
   if (*counter == 0)
	{ 
		not_zero = true;
		}
		return not_zero;
}	
	
 
  void print (int my_array [size],int *increment)
  {
	int *counter, 
	int *last position;
	last position = my_array + size -1;
	
	for (*counter == my_array + &increment)
	 { 
	   cout<< "$"<< *counter << "$" \n";
	   }
	   else
		cout << " " << *counter << " ";
		
  void zero (int my_array [size])
	 {   
		int *counter;
		int last;
		
		last = my_array + size - 1;
		
	for (*counter = position; my_array <=last; position ++)
	
	 {  *counter = 0
	 }
	 }
	 
void random (int my_array [size],int *last,int* increment);
 {
	  
 int random value;
 
 random value = rand % 10;
 my_array = my_array + random
 *increment = random value;
 }   
		
			  




Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Help With Random Number and Increment At Point on Array

Posted 30 September 2007 - 01:21 PM

you should post the errors that you are having so we can help you out. "why does this error pop up at this line" is a lot easier than "what's wrong with this entire source?" however, there are a couple of things that i can see right off.

you declare my_array to be an int[], but then you initialize it with only a single value - this will not work. you have a couple of variable names that have spaces in them (last position and random value). the bodies of nested for, do, and while statements should be enclosed at each level with braces:

do {
	//do stuff here
	if(some_value != some_other_value) {
		//do some other stuff here
	}
} while(condition!=stop_condition);


your print function is missing some closing braces, and the syntax of the for-loop you use in that function is incorrect.

-jjh
Was This Post Helpful? 0
  • +
  • -

#5 hollsmeags  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 29-March 07

Re: Help With Random Number and Increment At Point on Array

Posted 30 September 2007 - 03:46 PM

Sorry about that JJ. I will be more precise with my questioning

Okay, I will post comments next to the lines Im not sure about.
Thanks for the tip
here is my code with revisions made


const int size = 20;
bool fill (int my_array [size]);
void print (int my_array [size]);
void random (int my_array [size]);

using namespace std;
int main(int argc, char *argv[])
{ 
  
	int my_array[20] = {0};
	int last;					 // Should last or increment have a * in front when I declare them?
	int increment;
	
	do							
	{
	 int loop_count;	 // Im getting an error saying it needs a while before the do. That doest makek sense to me>?
	 cout<<" Enter random number (0-9)";
	 cin >> loop_cout;	// lIm getting an error loop count undeclared.... I thought I declared it an int?
	 random(my_array)
	 print (my_array);
	 loop_count++;
}
}
	 while (fill (my_array));   // Not sure about what to put for a condition here
	  cout <<loop_count;
		
	system("PAUSE");
	return EXIT_SUCCESS;
}


 bool fill (int my_array[size])
 {
   bool not_zero= false;
   int *counter;
   int last;
  
   last = my_array + size - 1;
 
   for (*counter = my_array; counter <= last; *counter ++)
	  if (*counter == 0)
	{ 
	   not_zero = true;					  // I'm a little confused with this function. Is the syntax correct?
		}
		return not_zero;
}	
	
 
  void print (int my_array [size])
  {
	int *counter, 
	int *last_position;
	last_position = my_array + size -1;
	
	for (*counter = my_array; counter <= last_position; *counter ++) 
	 { 
	   cout<< "$"<< *counter << "$" \n";
	   }
	   else
		cout << " " << *counter << " ";
}	
  
void random (int my_array [size],);
 {
  int ptr;	
  int random_value = rand % size;
   my_array = my_array + random
  *increment = ptr;								  // Would the following two lines be correct here?
  *increment = *increment + 1
 }   
		
  



Thanks so much for your help!

This post has been edited by hollsmeags: 30 September 2007 - 03:47 PM

Was This Post Helpful? 0
  • +
  • -

#6 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Help With Random Number and Increment At Point on Array

Posted 30 September 2007 - 04:09 PM

you need include iostream, or cout and cin won't work. if you have included them in the original source file, for future reference you will want to post your entire code with #include directives - many problems seen on this forum relate to those includes.

there are a few things that i can see are wrong in your main routine:
-it makes no difference what you declare last or increment as, since you don't use them.
-you have an extra } after the body of the do statement. this may fix your problem with the do-while error that the compiler is throwing
-under what condition do you want the while loop to exit?

after that, the code gets a little confusing. you should provide a comment for each function, describing exactly what it is supposed to be doing.

in your for loops in the various functions, you do not want to dereference your pointers in the test conditions - you want these to be addresses. the only time you want to dereference a pointer is when you need the value stored at that location (either in an array or just a simple datatype).

in void print(), else statements can only be used in conjuction with an if statement, not a for statement.

right now, the code is a bit of a mess, because you give no indication what each step is supposed to be doing. add detailed comments for each of the functions, and it'll be easier for others to help you out.

-jjh
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1