My goal is for the user to input a number from the range 3 to 8 and it will a create matrices based on that size and then fill it with random numbers from the range of -12 to 8.
I think I've got the concept right but the variable length array confuses cause I know it not possible to create an array that isn't constant.
I keep getting the error "subscript requires array or pointer type"
#include<iostream>
#include <random>
#include<time.h>
#include<iomanip>
#include <vector>
#include <map>
usingnamespace std;
void array_gen(int* array, int arraysize) // void function to generate the array with random numbers
{
int i;
for (i = 0;i < arraysize;i++)
{
array[i] = rand() % -12 + 8; // used to generate numbers from 0 to 10
}
}
void arraydis(int* array, int arraysize) // function to sort array
{
int i;
for (i = 0;i < arraysize;i++)
{
cout << "\n";
std:cout << array[i][j] << " ";
}
}
int main()
{
int arraysize = 0, i, p;
cout << "Enter array in the range of 50 <= size <= 200: "; // records the users input
cin >> arraysize;
int* array = newint[arraysize], freq[10];
array_gen(array, arraysize);
cout << "unSorted array is:"; // shows the unsorted array
arraydis(array, arraysize);
}
#include <iostream>
#include <random>
#include <ctime>
usingnamespace std;
void array_gen(int* array, int arraysize) // void function to generate the array with random numbers
{
for (int i = 0; i < arraysize; ++i)
array[i] = rand() % 21 - 12; // used to generate numbers from -12 to 8
}
void arraydis(int* array, int arraysize) // function to sort array
{
for (int i = 0; i < arraysize; i++)
{
cout << "\n";
cout << array[i] << " ";
}
}
int main()
{
srand(time(0));
int arraysize = 0;
cout << "Enter array in the range of 50 <= size <= 200: "; // records the users input
cin >> arraysize;
int* array = newint[arraysize];
array_gen(array, arraysize);
cout << "unSorted array is:"; // shows the unsorted array
arraydis(array, arraysize);
delete[] array;
}
This compiles, creates a 1d matrix using random numbers in the required range and displays the matrix. If a 2-d matrix is needed, then without using vectors one can be created as per againtry's post.