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++.

paragtiwari001Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
You can generate random values useing rand() :

        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
0
jkrCommented:
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.
0
paragtiwari001Author Commented:
Hi

Thanks for the solution,

Can you give me a sample code to which generate two dimentional array randonly

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

jkrCommented:
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();
  }
 
}

Open in new window

0
paragtiwari001Author Commented:
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
0
jkrCommented:
Um, there is no code to output any value - that might be the problem. Just add e.g. a 'printf()'
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();
 
    printf("[%d|%d]: %f\n", i, j, adTwoDimensions[i][j]);
  }
}
 
 return 0;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
May I ask why you graded that as a 'C'?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.