Hey all, I'm currently trying to write a code that will create an array of randomly generated numbers whose size is based on user input, the input has to be between 3 and 100 and the array must put in rows and columns of 10. The actual assignment reads:
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 out put line would have 2 values.
For the most part I've got it down, the only issue i'm having is actually printing the array...it's all coming out as one long column.
here's the code:
#include<iostream>
#include<iomanip>
#include<time.h>
using namespace std;
void fillArray (int number);
void printArray (int array[], int number);
int main()
{
int number, array[10][10] = {};
cout << "Enter how many integers you'd like generated" << endl;
cout << "\tbetween 3 and 100: ";
cin >> number;
if (number < 3 || number > 100)
{
cout << "ERROR: THAT IS NOT A VALID NUMBER" << endl;
return 0;
}
else
{
fillArray(number); }
}
I just talked to my instructor and he said we're not allowed to use pointers so i changed the original array[10][10] to [100] and got rid of the pointer.
Displaying a 2-D array is different from displaying a 1-D array.
In your for loop in the fillArray function you only have it filling 1 matrix and then starting a new line. Since your using a 2-D array, you need to put columns into the picture. Another for loop will need to be nested into the first for loop. Also you don't need to declare input in the function, you can just use 'number'.
I can show you an example if you'd like but I'd rather push you to figure it out first.
I keep trying different varitions of the nested loop but I'm still only coming up with one column, this is what I've got:
void fillArray(int array[], int number)
{
[code] srand(time(NULL));
for (int i = 1; i <= number; i++) {
for (int j = 1; j <= 10; j++)
array [i] = rand() % 100 + 1;
cout << array[i] << (i%10?"":"\n") << endl;
}
}
I honestly am stuck and have been for a while trying to fix this, I know I'm close so just nudge me in the right direction and I'll be very grateful.