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];
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!
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!
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.
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?
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().
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
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).
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
Bookmarks