-
March 10th, 2009, 08:40 AM
#1
Declare a 2D Dynamic array
How do I declare a dynamic 2D array and ask the user to input its size? Thanks
-
March 10th, 2009, 08:45 AM
#2
Re: Declare a 2D Dynamic array
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;
Other than that I suggest you use a Hash table or link list of link lists.
HTH,
ahoodin
To keep the plot moving, that's why.
-
March 10th, 2009, 09:08 AM
#3
Re: Declare a 2D Dynamic array
Originally Posted by Elite Commando
How do I declare a dynamic 2D array and ask the user to input its size? Thanks
If you did a simple search on CodeGuru, you would have found many, maybe hundreds of threads answering your question.
How to declare a dynamic 2 dimensional array is one of the most asked and answered questions.
Regards,
Paul McKenzie
-
March 10th, 2009, 09:08 AM
#4
Re: Declare a 2D Dynamic array
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];
-
March 10th, 2009, 09:22 AM
#5
Re: Declare a 2D Dynamic array
Originally Posted by Lindley
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];
Alright Thanks! One more question, suppose I make a matrix by using an array "mat[4][5]" 4 rows and 5 columns and I wish to fill the matrix with random alphabets, how will I do that? Thanks!
-
March 10th, 2009, 09:33 AM
#6
Re: Declare a 2D Dynamic array
Use 'rand' (and 'srand') to generate numbers between 1 and 26 and put them in the array.
-
March 10th, 2009, 09:49 AM
#7
Re: Declare a 2D Dynamic array
Originally Posted by Skizmo
Use 'rand' (and 'srand') to generate numbers between 1 and 26 and put them in the array.
Alright, I know how to use the rand() function, however, I'm not sure about srand() or how Im going to tell the compiler to pick a random number between 1 and 26? or how these numbers will be converted to alphabets? I am very new to c++, Thanks!
-
March 10th, 2009, 10:30 AM
#8
Re: Declare a 2D Dynamic 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:
Code:
char asciival = (rand() % 26) + 'A';
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.
-
March 10th, 2009, 10:33 AM
#9
Re: Declare a 2D Dynamic array
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!
ahoodin
To keep the plot moving, that's why.
-
March 10th, 2009, 10:50 AM
#10
Re: Declare a 2D Dynamic array
Originally Posted by Lindley
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:
Code:
char asciival = (rand() % 26) + 'A';
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.
Thanks! I have yet to figure out how this works, but for some reason it seems to generate only the letter "P", no other letter is generated? why?
-
March 10th, 2009, 11:00 AM
#11
Re: Declare a 2D Dynamic array
Originally Posted by Elite Commando
I have yet to figure out how this works, but for some reason it seems to generate only the letter "P", no other letter is generated? why?
Post the smallest and simplest compilable program that demonstrates the problem.
-
March 10th, 2009, 11:31 AM
#12
Re: Declare a 2D Dynamic array
Originally Posted by laserlight
Post the smallest and simplest compilable program that demonstrates the problem.
Code:
char asciival = (rand() % 26) + 'a';
char a[4][5];
for(int i=0; i<4; i++)
{
for(int j=0; j<5; j++)
{
a[i][j]=asciival;
cout<<a[i][j];
}
cout<<endl;
}
a simple 4 by 5 matrix with random alphabets is what Im looking for THANKS
-
March 10th, 2009, 11:40 AM
#13
Re: Declare a 2D Dynamic array
I get these errors when trying to compile your sample program with the MinGW port of g++ 3.4.5:
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
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
Anyway, a possible problem is that you are using the same value (asciival) on every iteration. You probably want to do this instead:
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;
}
}
The timeSeed() function is just some code I adapted from this tutorial on using rand().
-
March 10th, 2009, 12:03 PM
#14
Re: Declare a 2D Dynamic array
Originally Posted by laserlight
The timeSeed() function is just some code I adapted from this tutorial on using rand().
I'm very suspicious of any webpage that claims the uniform distribution and the normal distribution are the same thing.
-
March 10th, 2009, 12:07 PM
#15
Re: Declare a 2D Dynamic array
Originally Posted by Lindley
I'm very suspicious of any webpage that claims the uniform distribution and the normal distribution are the same thing.
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).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|