paragtiwari001
asked on
Want to generate random two dimentional array
hi
I want to generate two dimentional array using c++.
Actuall i have to implement dijkstra,so i have to randmly generate source & destination to give into dijkstra code.
Can anyone help me in this code.
I am very new tp c++.
I want to generate two dimentional array using c++.
Actuall i have to implement dijkstra,so i have to randmly generate source & destination to give into dijkstra code.
Can anyone help me in this code.
I am very new tp c++.
To generate random numbers, you'd usually use 'rand()'. See the reference at http://www.cplusplus.com/reference/clibrary/cstdlib/rand.html - it also comes with an example program on how to use it. Then, two dimensional arrays are delared like
double adTwoDimensions[10][20];
To populate that array, you need two loops, e.g.
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 20; ++j); {
adTwoDimensions[i][j] = <some_value>;
}
}
Putting these two things together should give you what you need.
double adTwoDimensions[10][20];
To populate that array, you need two loops, e.g.
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 20; ++j); {
adTwoDimensions[i][j] = <some_value>;
}
}
Putting these two things together should give you what you need.
ASKER
Hi
Thanks for the solution,
Can you give me a sample code to which generate two dimentional array randonly
Thanks for the solution,
Can you give me a sample code to which generate two dimentional array randonly
You pretty much have it above. E.g.
#include <stdlib.h>
#include <time.h>
//...
double adTwoDimensions[10][20];
srand ( time(NULL) );
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 20; ++j); {
adTwoDimensions[i][j] = (double) rand();
}
}
ASKER
Hi
I am using this code .
But not able to get random number in output:
#include <iostream>
#include <ctime> // For time()
#include <cstdlib> // For srand() and rand()
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main ()
{
double adTwoDimensions[10][20];
int i=0,j=0;
srand ( time(NULL) );
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 20; ++j);
{
adTwoDimensions[i][j] = (double) rand();
}
}
}
Can somebody check the problem
I am using this code .
But not able to get random number in output:
#include <iostream>
#include <ctime> // For time()
#include <cstdlib> // For srand() and rand()
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main ()
{
double adTwoDimensions[10][20];
int i=0,j=0;
srand ( time(NULL) );
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 20; ++j);
{
adTwoDimensions[i][j] = (double) rand();
}
}
}
Can somebody check the problem
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
May I ask why you graded that as a 'C'?
http://www.cplusplus.com/reference/clibrary/cstdlib/rand.html
Don't forget to seed the random number generator using srand :
http://www.cplusplus.com/reference/clibrary/cstdlib/srand.html