## alex k

programe to randomly generate store and display the age of N studnets (e.g ,18,24,38 etc).the minimum age is 18 and maximum age is 75.N should be equal to 9.
1.How we can intialise all the elements of the array to -1.
2.Randomly generate and store the age of N studnets .
3.sort and display the array in ascending order .
thnks :-)

## Ancient Dragon 5,243

1. use a loop

2. call `srand( (unsigned int)time(0));` to initialize the random number generator, then for each number call `rand()` to get a random number.

3. google for sort algorithms. The bubble sort is the easiest to code but the slowest. In c++ you can also use the std::sort() method found in <algorithm> header file.

## alex k

thnks Ancient Dragon :-)
i did this ..wat do u think?

``````srand( time(0) ); //seed random number generator
int age[3] = { 18 , 40, 65 };
int randomage = rand() % 3; //return a number from [18,75)
``````

now if i want to calculate the avearge of students age..is that right?

``````Int  Sum = 0;
for( int i = 18; i < 75; i++)
{
Sum = Sum + array[i];
}
``````

I found this context about the sort algorthim

``````void selectionSort ( int arr[], int size )
{
int indexOfMin;
int pass;
int j;

for ( pass = 0; pass < size - 1; pass++ )
{
indexOfMin = pass;

for ( j = pass + 1; j < size; j++ )
if ( arr[j] < arr[pass] )
indexOfMin = j;

swap ( arr[pass], arr[indexOfMin] );
}
}

// swap function for integers
void swap ( int& x, int& y )
{
int temp;
temp = x;
x = y;
y = temp;
}

void sort()
{
int size=57;
int j,temp,i;
for(i=0;i<(size-1);i++)
{
for(j=i+1;j<size;j++)
{
if(queue[i]>queue[j])
{
temp=queue[i];
queue[i]=queue[j];
queue[j]=temp;
}

}
}
cout<<"Status: sorting done."<<endl
``````

## Ancient Dragon 5,243

>>int age[3] = { 18 , 40, 65 };
Why are you initializing it with some dummy data? Just initialise the array with all 0s `int age[3] = {0};` >>int randomage = rand() % 3; //return a number from [18,75)

That's a number between 0 and 3, not 18 and 75

>>for( int i = 18; i < 75; i++)
The array only has 3 elements. You are trying to use the age range as the index into the array. What you want is this: `for(int i = 0; i < 3; i++)`