Random number array

Hey I have to build a random number array, heres the exact problem-


void fillArray (int array[], int number)
parameters : array of integers
number of integers to store in the array
description: generate "number" random numbers from 1 to 100 and store them in positions [0..number-1]
So the array is always the same size, but may be filled with a different number of random values on each run

void printArray (int array[], int number)
parameters : array of integers
number of integers stored in the array
description: print the values in the array. The values appear 10 per line and are comma separated. The last value on each line does not have a comma after it. The last line may or may not have 10 values: for example, if the user asked for 15 numbers, then the last line would only have 5 values; or if the user asked for 92 values, then the output would be 9 lines containing 10 values and the last output line would have 2 values.


I'm on the first part. My code is this-
Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdlib> 
#include <ctime> 
#include <iostream>

using namespace std;

void main() 
{ 

 const int size;
 cout<< "how big do you want the array?" << endl;
 cin >> size;



 int array[size];
 
 srand((unsigned)time(0)); 
     
    for(int i=0; i<size; i++){ 
        array[i] = (rand()%100)+1; 
         
        cout << array[i] << endl;
 } 
}


I think I have to let the user choose the size of the array(indicated by the second part (void printArray)), and that is what is giving me problems. If I just define the constant or put an integer in for the array size it works fine, but this code does not compile and I would like to know why.
Thanks in advance.
If you allocate memory statically through declaration array as
int array[size];
then the number of elements must be known during compilation time. So, size may be #define-d constant, const-variable constant, or simply natural number literal. Otherwise you'll get a compilation error.

If you need allocate memory dynamically for an array, use operators new[]/delete[]:
1
2
3
int* array = new int[size]; //size may be ordinary
 /* do something with array */
delete[] array;
"const int size;"

you don't want size to be const.

Just use

int size;

and the program should work.
@Hypersion: No, you would need to do it using the dynamic allocation method suggested by melkiy because you cannot statically allocate an array with a variable as the size.
GCC allows variable sized arrays. Other compilers do not.
Thanks for the help. However I have a new problem. I thought this part was going to be easy but its hard.


void printArray (int array[], int number)
parameters : array of integers
number of integers stored in the array
description: print the values in the array. The values appear 10 per line and are comma separated. The last value on each line does not have a comma after it. The last line may or may not have 10 values: for example, if the user asked for 15 numbers, then the last line would only have 5 values; or if the user asked for 92 values, then the output would be 9 lines containing 10 values and the last output line would have 2 values.


I've been trying all day to come up with a for loop to do it. I have a bunch of variations but here's the one I think is closest-



1
2
3
4
5
6
7
8
9
void printArray( int array[], int number)
{


for (int j=0; j<number; j= j +10){
  for (int i = 0; i<10; i++)
   cout<< array[i]<< " ";

cout << endl;


I just cant wrap my head around it.
Here's an algorithm to help you.

1
2
3
4
5
6
7
8
9
void printArray( int array[], int numElements ) {
  for( each element E ) {
      Output the element;
      if this is not the 10*kth element that I've output then
          output a comma;
      else
          output a newline
  }
} 


Remember that arrays are zero based, so the Nth element is stored at index N-1.
Ok I got that part figured out, but the assignments just keeps getting harder. I have to find the average of the array and print how many numbers in the array are above the average. That was easy, but now I have to -

print the positions of the three consecutive values that have the largest average. If more than 3 consecutive values have the same average, print the positions with the lowest subscripts. Note: print the positions (subscripts), NOT the actual values.

This is the code I have so far-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void printConsecutive(int array[], int number)
{
 
int largeAvg;

largeAvg = (array[0] + array[1] + array[2]);

for (int i = 3; i < number; i = i + 3){
 if ( (array[i] + array[i++] + array[i = i + 2]) > largeAvg){
  
int j = i++;
int  k = i + 2; 

 cout<< i << ", " << j << ", " << k << endl;}
 
}
}


Its not giving me the results I want, man this is a tough assignment. If I could have or one of your guys AIM or something so I can talk to you while I'm doing it would be very helpful.
You are on the right track. But you need to keep two pieces of information. You are keeping the "largest average" in largeAvg (although the name is poor, because the variable holds a sum not an average). You also need to keep the subscript of the
first value of those three.

Now, you have two bugs in your loops.

First, your for loop needs to start by considering elements 1, 2, and 3. You are considering 3, 4, and 5 first.

Second, you don't want to increment i by 3 in the for loop because you have to consider the triples (1,2,3), (2,3,4), (3,4,5) etc.

Third, your if statement is the right idea, but you don't want to change i in it, and both i++ and i = i + 2 do.

You are very close.

Topic archived. No new replies allowed.