I'm not understanding my assignment and what I've read has really confused me. This is what I need to do: write a function to initialize an integer Partially Loaded Array with distinct random values. The array has room for 50 elements. The actual number of elements to store will be determined by the following equation:
intNumberOfElementsToStore = 10 + 25 * ((double) rand() / (double) RAND_MAX) ;

This is what I wrote:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std ;

const int ARRAY_SIZE = 50 ;
int numberOfElementsToStore[ARRAY_SIZE] ;
double populateArray(int numberOfElementsToStore[]) ;
double printArray(int numberOfElementsToStore[]) ;

int main()
{	
	
	double srand((int)  time (NULL ) ) ;
	
	populateArray(numberOfElementsToStore) ;
	printArray(numberOfElementsToStore) ;

    
     return 0;
}

		
double populateArray(int numberOfElementsToStore[] )
{
	int i = 0 ;
	for (int i = 0; i < ARRAY_SIZE; i++)
	{
		numberOfElementsToStore[i] =  10  + 25 * ((double) rand( ) / (double) RAND_MAX ) ;

	}

	return i ;
}
	
double printArray(int numberOfElementsToStore[])
{
	int i = 0 ;
	for (int i = 0; i < ARRAY_SIZE; i++)
	{
		if ((i+1) % 10 == 0)
			cout << fixed << setw(6) << numberOfElementsToStore[i] << " " << endl ;
		else
			cout << fixed << setw(6) << numberOfElementsToStore[i] << " " ;
	}

	return i ;
}

This does give me numbers, but not distinct. Thanks!

This does give me numbers, but not distinct. Thanks!

dis┬Ětinct (d-stngkt)
adj.
1. Readily distinguishable from all others; discrete: on two distinct occasions.
2. Easily perceived by the senses or intellect; clear: a distinct flavor.
3. Clearly defined; unquestionable: at a distinct disadvantage.
4. Very likely; probable: There is a distinct possibility that she won't come.
5. Notable: a distinct honor and high privilege.

I don't understand what you mean by 'distinct' numbers.. do you mean 'unique'? (a number only used once within your array?)

Edited 6 Years Ago by Clinton Portis: n/a

I don't understand what you mean by 'distinct' numbers.. do you mean 'unique'? (a number only used once within your array?)

Our instructor used the term distinct. I'll assume he meant unique.

#include<algorithm>

double populateArray(int numberOfElementsToStore[] )
{
	int i = 0 ;
	for (int i = 0; i < ARRAY_SIZE; i++)
	{
		numberOfElementsToStore[i] =  10  + 25 * ((double) rand( ) / (double) RAND_MAX ) ;

	}	
                
         //This will make like values reside next to each other in the array
         sort(&numberOfElementsToStore[0], &numberOfElementsToStore[ARRAY_SIZE]);
         //This will remove consecutive like amounts
         unique(&numberOfElementsToStore[0], &numberOfElementsToStore[ARRAY_SIZE]);

         return i ;
}

unique

function template<algorithm>template <class ForwardIterator>
ForwardIterator unique ( ForwardIterator first, ForwardIterator last );

template <class ForwardIterator, class BinaryPredicate>
ForwardIterator unique ( ForwardIterator first, ForwardIterator last,
BinaryPredicate pred );
Remove consecutive duplicates in range

Removes the duplicate consecutive elements from the range [first,last). This is done by removing all the elements that compare equal to the element right preceding them (only the first element in each group of consecutive equal elements is kept).

Edited 6 Years Ago by Clinton Portis: Try the great taste of c++.

I interpreted your assignment slightly differently:

int populateArray(int a[] )
{
	int numberOfElementsToStore = (int)( 10  + 25 * ((double) rand( ) / (double) RAND_MAX )) ;
	int i = 0 ;
	for (int i = 0; i < numberOfElementsToStore; i++)
	{
		a[i] = rand() % 100;
	}

	return numberOfElementsToStore;
}
	
int printArray(int arr[],int numelements)
{
	int i = 0 ;
	for (int i = 0; i < numelements; i++)
	{
		if ((i+1) % 10 == 0)
			cout << fixed << setw(6) <<arr[i] << " " << endl ;
		else
			cout << fixed << setw(6) << arr[i] << " " ;
	}

	return i ;
}

with the random function determining the number of elements in the array. You can play around with the rand % 50 to get the range of numbers that you need. I had to rename some things and change some of your doubles to ints (again can be adjusted accordingly for your assignment).

Also, please try to avoid global variables... there wasn't much standing in the way of their being defined in main and passed in.

EDIT: There's a problem with the number of values generated it seems to be constant. I did need to take the double out from the front of srand() in main... that helped with the values but not the number of them.

Edited 6 Years Ago by jonsca: n/a

I put another srand((unsigned)time(NULL)); into the body of the populateArray() function and it seems to work now.
and took off the (int) cast of int numberOfElementsToStore = (int)( 10 + 25 * ((double) rand( ) / (double) RAND_MAX )) ;

intNumberOfElementsToStore should be an int, not an array of ints. It should be generated in main(), line 17 looks like a good spot to do it. Declare the array with a name like PartiallyLoadedArray, which is probably better than the generic name of a that jonsca used, though I know why jonsca used that name. Pas both the array and intNumberOfElementsToStore to both the populate and the print functions.

There is no imposed range of values on the ints placed in the array. That means they could have both negative and positive signs and will range in value as much as the implementation of your compiler will let you. I think it is safe to assume that only positive ints will be used to populate the array of ints, but it isn't specified in the instructions posted. Random int values (or close enough thereto) can be generated by rand(). Random signage of the values can be accomplished using rand(), too, but it takes a couple extra steps to do it.

Dont' expect to get full marks for the version posted by Clinton Portis, unles you have learned about the STL algorithms class. It's a perfectly valid way to do the task assigned, but it's probably not the learning experience your instructor had in mind, and might be felt to be a bit cheeky if submitted.

The grind it out method would involve using another variable to keep track of how many of the maximum number of random ints you are supposed to place in the array have actually been placed in the array. When each random int is generated search the values in the array already to see if that value has been generated before. If not, then add the current value to the array and adjust the appropriate variables value.

Don't try to print or search values beyond the actual number of values in the array at the time you print or search it. Unless you actually fill the array with a default value you never know what might be in there or what might happen. Stick with what you know about. That means the value to terminate the processes shouldn't default to 50 just because that's the maximum number of values the array can hold.

I put another srand((unsigned)time(NULL)); into the body of the populateArray() function and it seems to work now.
and took off the (int) cast of int numberOfElementsToStore = (int)( 10 + 25 * ((double) rand( ) / (double) RAND_MAX )) ;

I changed what you suggested but now I'm stuck:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std ;

const int ARRAY_SIZE = 50 ;
int numberOfElementsToStore[ARRAY_SIZE] ;
int populateArray(int a[]) ;
int printArray(int arr[], int numelements) ;

	
int main()
{	
	srand( (unsigned) time (NULL) ) ;
	
	populateArray(a) ;
	printArray(arr, numelements) ;
	
    return 0;
}

int populateArray(int a[])
{
	srand( (unsigned) time (NULL) ) ;
	int numberOfElementsToStore = (int)( 10  + 25 * ((double) rand( ) / (double) RAND_MAX )) ;
	int i = 0 ;
	for (int i = 0; i < numberOfElementsToStore; i++)
	{
		a[i] = rand() % 100;
	}

	return numberOfElementsToStore;
}
	
int printArray(int arr[], int numelements)
{
	int i = 0 ;
	for (int i = 0; i < numelements; i++)
	{
		if ((i+1) % 10 == 0)
			cout << fixed << setw(6) << arr[i] << " " << endl ;
		else
			cout << fixed << setw(6) << arr[i] << " " ;
	}

	return i ;
}

I'm getting these messages:
error C2065: 'a' : undeclared identifier
error C2065: 'arr' : undeclared identifier
error C2065: 'numelements' : undeclared identifier
for lines 18 and 19

if I declare them then I get:
error C2664: 'populateArray' : cannot convert parameter 1 from 'int' to 'int []'
error C2664: 'printArray' : cannot convert parameter 1 from 'int' to 'int []'

Where did you declare a before passing it to populateArray(a) in line 18? a should be declared as an array of 50 ints before you try to use it.

The first argument in line 19 should be the same as the array passed in line 18, otherwise you're likely to be in trouble.

populateArray() should be declared with type void. There is no reason to return a value, particularly if you are going to ignore the return value anyway.

As indicated in my first post, lines 9 and line 27 should be consolidated into a single declaration on line 17 and it should be passed to both the populate and print function. Don't declare any variable with global scope unless there is good reason to do so. Because it is possible to do so is not good enough reason

You only need to call srand() once per program.

You are imposing a range of values on the random numbers generated to 0-99 by using the syntax you have online 31. This range restriction is probably valid per the instructions since the numbers generated are random. However, if the word distinct in the instructions is interpreted as unique instead of as discrete, then you will need to do some extra work to assure distinctness/uniqueness.

You only need to call srand() once per program.

That was my bad, I thought that it was helping but I'd also taken the cast off of the given "random function" (10+25*etc)

That was my bad, I thought that it was helping but I'd also taken the cast off of the given "random function" (10+25*etc)

I really appreciate all the help everyone has given me, but I'm getting all zero's. Sorry for being so dense! Here's my revised code:

#include <iostream>	
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std ;

const int ARRAY_SIZE = 50 ;
int a[ARRAY_SIZE] ;
int arr[ARRAY_SIZE] ;
void populateArray(int a[]) ;
int printArray(int arr[], int numberOfElementsToStore) ;

	
int main()
{	
	srand( (unsigned) time (NULL) ) ;
	
	int numberOfElementsToStore = 10  + 25 * ((double) rand( ) / (double) RAND_MAX ) ;
	populateArray(a) ;
	printArray(arr, numberOfElementsToStore) ;
	
    return 0;
}

void populateArray(int a[])
{
	int numberOfElementsToStore = 10  + 25 * ((double) rand( ) / (double) RAND_MAX ) ;
	int i = 0 ;
	for (int i = 0; i < numberOfElementsToStore; i++)
	{
		a[i] = rand() % 100;
	}

}
	
int printArray(int arr[], int numberOfElementsToStore)
{
	int i = 0 ;
	for (int i = 0; i < numberOfElementsToStore; i++)
	{
		if ((i+1) % 10 == 0)
			cout << fixed << setw(6) << arr[i] << " " << endl ;
		else
			cout << fixed << setw(6) << arr[i] << " " ;
	}

	return i ;
}

You were writing to a in the method and then trying to pass empty arr into your print function. So pass a into the print function. Remember the name of the argument that you are passing in in main and the name of the parameter of the function have nothing to do with each other. Once you run the populate function, the array is changed (by pointer) and so when you pass in the same array into print it will receive the values by pointer also.

As far as your random values, you should only have one randomization function, either in the method or in main(). The one in main and the one in the function could come up with different values and then they will conflict. There's still something about that function that it keeps coming up with the same number over and over. I had it earlier that it wasn't doing that I'm trying to isolate it now.
Here's my main() in case that helps (though I used arr, you are fine with using a for both)

int main()
{	
	
	int arr[ARRAY_SIZE] ;
	//srand((unsigned)  time (NULL ) ) ;
	
	int numberOfElementsToStore = populateArray(arr) ;
	printArray(arr,numberOfElementsToStore) ;

    
     return 0;
}

EDIT: Ok, this is one of those oddities that I'm sure someone had explained to me more than once but I just don't get it. So I took srand out of main() (housekeeping more than anything) but I put back in a cout in populate() where I printed rand()/RAND_MAX just to see how it was changing. Then the random function seems to work...

int populateArray(int a[] )
{
	srand((unsigned)time(NULL)); //had this in here intially to see if it made a difference, but left in here for convenience
	cout<<"R/Rm " <<((double) rand())/((double) RAND_MAX)<<endl;
	int numberOfElementsToStore = ( 10  + 25 * ((double) rand() / (double) RAND_MAX )) ;
	cout <<"NETS "<<numberOfElementsToStore <<endl;
	int i = 0 ;
	for (int i = 0; i < numberOfElementsToStore; i++)
	{
		a[i] = rand() % 100;
	}

	return numberOfElementsToStore;
}

Edited 6 Years Ago by jonsca: n/a

guccitan88: Using code in post #11:
1) Move line 9 to line 18. Reason: don't use global variables unless you have to. If you feel you must use array a as a global variable then don't pass it to populateArray or printArray. But, I repeat again, you are better off declaring arrray a to be local to main() and passing it to populateArray and printArray than you are declaring it with global scope.

2) Eliminate line 10. You only need one array in this program.

3) Lines 11 and 12 should look like this:
void populateArray(int a[], int numberOfElementsToStore) ;
void printArray(int a[], int numberOfElementsToStore) ;

4) Change lines 26 and 37 to match lines 11 and 12 except don't use the terminal semicolons

5) Change lines 20 and 21 to pass the appropriate variables as expected iaccording to the prototypes.

6) Eliminate line 28 and 29, 39, 42, 43, 44 and 48 as they are superfluous or are cluttering up the landscape

When I did all that the code compiled and ran as expected.

This question has already been answered. Start a new discussion instead.