How do I declare a dynamic 2D array and ask the user to input its size? Thanks
Printable View
How do I declare a dynamic 2D array and ask the user to input its size? Thanks
Other than that I suggest you use a Hash table or link list of link lists.Code:cout << "Enter Size 1" << endl;
cin >> size1;
cout << "Enter Size 2" <<endl;
cin >> size 2;
int *parray = new int[size1][size2];
//do stuff with array
delete [] parray;
HTH,
Alternatives , each with slightly different properties:
Code://1
int **arr2d = new int*[rows];
for (i = 0; i < rows; i++)
arr2d[i] = new int[cols];
//and release
for (i = 0; i < rows; i++)
delete[] arr2d[i];
delete[] arr2d;
//2
std::vector< std::vector<int> > arr2d(rows, std::vector<int>(cols));
// no need to explicitly release
//3
std::vector<*int> arr2d(rows);
std::vector<int> arr2d_data(rows*cols);
for (i = 0; i < rows; i++)
arr2d[i] = &arr2d_data[i*cols];
Use 'rand' (and 'srand') to generate numbers between 1 and 26 and put them in the array.
Don't worry about srand() for now. The consequence of not using it is merely that you'll get the same sequence of random values on every run of the program, which is actually a good thing in terms of debugging.
As for picking a random capital letter:
See if you can figure out why that works. Hint: % is the integer modulus operator, and a char is merely a 1-byte int that gets treated a bit differently by output operations.Code:char asciival = (rand() % 26) + 'A';
How to use srand() must be the second most asked question on codeguru and the second most common example in coursepacks and textbooks!
Any search on google should do!:D:D:D
Post the smallest and simplest compilable program that demonstrates the problem.Quote:
Originally Posted by Elite Commando
I get these errors when trying to compile your sample program with the MinGW port of g++ 3.4.5:
Of course, you can point out to me that I was expected to #include <cstdlib> and place your code snippet in a main function, but I did ask for a compilable program ;)Code:test.cpp:1: error: `rand' was not declared in this scope
test.cpp:6: error: expected unqualified-id before "for"
test.cpp:6: error: expected constructor, destructor, or type conversion before '<' token
test.cpp:6: error: expected constructor, destructor, or type conversion before '++' token
Anyway, a possible problem is that you are using the same value (asciival) on every iteration. You probably want to do this instead:
The timeSeed() function is just some code I adapted from this tutorial on using rand().Code:#include <iostream>
#include <cstdlib>
#include <limits>
// Generate a portable seed based on system time.
unsigned int timeSeed()
{
using namespace std;
time_t now = time(0);
unsigned char* p = reinterpret_cast<unsigned char*>(&now);
unsigned int seed = 0;
unsigned int seed_multiplier = numeric_limits<unsigned char>::max() + 2U;
for (size_t i = 0; i < sizeof now; ++i)
{
seed = seed * seed_multiplier + p[i];
}
return seed;
}
int main()
{
using namespace std;
srand(timeSeed());
char a[4][5];
for(int i=0; i<4; i++)
{
for(int j=0; j<5; j++)
{
a[i][j] = (rand() % 26) + 'a';
cout<<a[i][j];
}
cout<<endl;
}
}
Yes, you are right. Pity I did not catch that bug; I have informed the author of another mistake previously, but in terms of programming she is one of the people that I respect (although ironically the mistake I pointed out had to do with a code snippet).Quote:
Originally Posted by Lindley