?
Solved

Want to generate random two dimentional array

Posted on 2008-11-03
7
Medium Priority
?
471 Views
Last Modified: 2012-06-27
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++.

0
Comment
Question by:paragtiwari001
  • 4
  • 2
7 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22869095
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
 
LVL 86

Expert Comment

by:jkr
ID: 22869119
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
 

Author Comment

by:paragtiwari001
ID: 22869191
Hi

Thanks for the solution,

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

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:jkr
ID: 22869243
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
 

Author Comment

by:paragtiwari001
ID: 22869425
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
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 22869464
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
 
LVL 86

Expert Comment

by:jkr
ID: 22871966
May I ask why you graded that as a 'C'?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question