Random Array

Need help on Generatin an array with random number

Page 1 of 1

12 Replies - 17152 Views - Last Post: 14 April 2010 - 03:57 PM Rate Topic: -----

#1 Guest_pyomi1234*


Reputation:

Random Array

Posted 14 April 2010 - 08:19 AM

I need help on generating an array that generates random numbers from 0 to 100


the size of the array is 10, and also I'm asked to get the values in the array and display them in reverse after the random array has been generated

Here's a sample output:
The random generated array is : 94_56_87_12_10_13_45_56_23_52

The reversed array is: 52_23_56_45_13_10_12_87_56_94
Is This A Good Question/Topic? 0

Replies To: Random Array

#2 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: Random Array

Posted 14 April 2010 - 08:23 AM

Please post some source code that you have written, we are not going to do your homework for you.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_pyomi1234*


Reputation:

Re: Random Array

Posted 14 April 2010 - 10:07 AM

here's what i have so far. I don't really know how to put my random numbers in an array. Can anyone help me on this?

-pyomi1234

[code]
// A random number generating program
#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<time.h>
using namespace std;
// it generates 100 random numbers
int main ( )
{
const int randnum=10;


float randvalue;
srand(time(NULL));
for(int i=1; i<=randnum; i++)
{
randvalue=rand( ) ;
cout<<setw(10)<<randvalue<<endl;
}
return 0;
}
Was This Post Helpful? 0

#4 spintronic  Icon User is offline

  • D.I.C Head

Reputation: -32
  • View blog
  • Posts: 94
  • Joined: 25-December 09

Re: Random Array

Posted 14 April 2010 - 10:20 AM

View Postpyomi1234, on 14 April 2010 - 09:07 AM, said:

here's what i have so far. I don't really know how to put my random numbers in an array. Can anyone help me on this?

-pyomi1234

// A random number generating program
#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<time.h>
using namespace std;
		// it generates 100 random numbers
int main ( )
{
const int randnum=10;


float randvalue;
		srand(time(NULL));
		for(int i=1; i<=randnum; i++)
		{
			randvalue=rand( ) ;
			cout<<setw(10)<<randvalue<<endl;
		}
	return 0;
}




I could be wrong, but I was taught to initialise the rand seed like this

srand((int)time(NULL));




and to generate numbers like this;

float randvalue=0;
randvalue = rand( )%(10);



Where 10 is the max value of the random number.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_pyomi1234*


Reputation:

Re: Random Array

Posted 14 April 2010 - 11:40 AM

View Postspintronic, on 14 April 2010 - 09:20 AM, said:

View Postpyomi1234, on 14 April 2010 - 09:07 AM, said:

here's what i have so far. I don't really know how to put my random numbers in an array. Can anyone help me on this?

-pyomi1234

// A random number generating program
#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<time.h>
using namespace std;
		// it generates 100 random numbers
int main ( )
{
const int randnum=10;


float randvalue;
		srand(time(NULL));
		for(int i=1; i<=randnum; i++)
		{
			randvalue=rand( ) ;
			cout<<setw(10)<<randvalue<<endl;
		}
	return 0;
}




I could be wrong, but I was taught to initialise the rand seed like this

srand((int)time(NULL));




and to generate numbers like this;

float randvalue=0;
randvalue = rand( )%(10);



Where 10 is the max value of the random number.


How would I put this into an array? I'm having trouble putting the values into an array that generates random number because all I know to do is generate the random numbers.
Was This Post Helpful? 0

#6 citus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 140
  • Joined: 28-March 10

Re: Random Array

Posted 14 April 2010 - 12:14 PM

int my_array[10];
//That is how you can declare an array

my_array[0] = 1;
my_array[1] = variable_declared_somewhere_else;
//that is how you can assign a value to an array



For more information please read this tutorial on arrays

Good luck!

This post has been edited by citus: 14 April 2010 - 12:15 PM

Was This Post Helpful? -1
  • +
  • -

#7 spintronic  Icon User is offline

  • D.I.C Head

Reputation: -32
  • View blog
  • Posts: 94
  • Joined: 25-December 09

Re: Random Array

Posted 14 April 2010 - 12:22 PM

View Postpyomi1234, on 14 April 2010 - 10:40 AM, said:

View Postspintronic, on 14 April 2010 - 09:20 AM, said:

View Postpyomi1234, on 14 April 2010 - 09:07 AM, said:

here's what i have so far. I don't really know how to put my random numbers in an array. Can anyone help me on this?

-pyomi1234

// A random number generating program
#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<time.h>
using namespace std;
		// it generates 100 random numbers
int main ( )
{
const int randnum=10;


float randvalue;
		srand(time(NULL));
		for(int i=1; i<=randnum; i++)
		{
			randvalue=rand( ) ;
			cout<<setw(10)<<randvalue<<endl;
		}
	return 0;
}




I could be wrong, but I was taught to initialise the rand seed like this

srand((int)time(NULL));




and to generate numbers like this;

float randvalue=0;
randvalue = rand( )%(10);



Where 10 is the max value of the random number.


How would I put this into an array? I'm having trouble putting the values into an array that generates random number because all I know to do is generate the random numbers.



Like this;

srand((int)time(NULL));
int i=0;
float randvalue[10];

for(i=0;i<=10;i++)
{
randvalue[i] = rand( )%(100);
cout<<"\nValue = "<<randvalue[i];
}

//And then backwards.

for(i=10;i>=0;i--)
{
randvalue[i] = rand( )%(100);
cout<<"\nValue = "<<randvalue[i];
}




This post has been edited by spintronic: 14 April 2010 - 12:31 PM

Was This Post Helpful? 0
  • +
  • -

#8 citus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 140
  • Joined: 28-March 10

Re: Random Array

Posted 14 April 2010 - 12:39 PM

If we just give everyone exactly what code they need, no one ever learns. Then they will always expect that someone is simply going to hand them the code. It is better to give them an idea, help with logic, pseudo code, or a tutorial to read, rather than handing them the answer.
Was This Post Helpful? -1
  • +
  • -

#9 spintronic  Icon User is offline

  • D.I.C Head

Reputation: -32
  • View blog
  • Posts: 94
  • Joined: 25-December 09

Re: Random Array

Posted 14 April 2010 - 01:02 PM

View Postcitus, on 14 April 2010 - 11:39 AM, said:

If we just give everyone exactly what code they need, no one ever learns. Then they will always expect that someone is simply going to hand them the code. It is better to give them an idea, help with logic, pseudo code, or a tutorial to read, rather than handing them the answer.


I think your being unfair.

The code I posted is either a modification of his, or an example of his error.

Sending someone around a banana tree telling them they should first find oranges. Doesn't teach someone bananas are orange. (And if it did, who is the fool? The student or the teacher?)
Was This Post Helpful? 0
  • +
  • -

#10 citus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 140
  • Joined: 28-March 10

Re: Random Array

Posted 14 April 2010 - 01:34 PM

The OP had no knowledge of arrays. That's like someone giving you the following code:

int main()
{
int somenumber = 5;
return 0;
}



Then they say they want to run a recursive function in their program and you writing it for them.
Was This Post Helpful? -1
  • +
  • -

#11 muhammadlodhi  Icon User is offline

  • New D.I.C Head

Reputation: -16
  • View blog
  • Posts: 26
  • Joined: 14-April 10

Re: Random Array

Posted 14 April 2010 - 01:59 PM

can u help me plz i have to make a program with array which balance the paranthesis and chk them if not balance then error and solve the epression with () braces i am not understanding it .




int main()
{
char arr[30];
for(int i=0;i<30;i++){
if(c=='(')
c++;
else if(d==')')
d++;}
if(c==d)
printf("parenthesis are balance");
else
printf("are not balance");
Was This Post Helpful? 0
  • +
  • -

#12 spintronic  Icon User is offline

  • D.I.C Head

Reputation: -32
  • View blog
  • Posts: 94
  • Joined: 25-December 09

Re: Random Array

Posted 14 April 2010 - 03:47 PM

View Postcitus, on 14 April 2010 - 12:34 PM, said:

The OP had no knowledge of arrays. That's like someone giving you the following code:

int main()
{
int somenumber = 5;
return 0;
}



Then they say they want to run a recursive function in their program and you writing it for them.


Of course he had knowledge of arrays, or he wouldn't be able to form a question regarding accessing them.

And as for your rediculous hypocritical statement;

Quote

If we just give everyone exactly what code they need, no one ever learns.


I suggest you read your *own* link.


// arrays example
#include <iostream>
using namespace std;

int billy [] = {16, 2, 77, 40, 12071};
int n, result=0;

int main ()
{
  for ( n=0 ; n<5 ; n++ )
  {
    result += billy[n];
  }
  cout << result;
  return 0;
}



The *ONLY* difference, between the code *YOU* **GAVE** him, and I did was I *DIDN'T* hide mine in the guise of a banana shaped orange tree in the form of a web address.

Now piss off, minus rating my posts you fin retard.
Was This Post Helpful? -1
  • +
  • -

#13 citus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 140
  • Joined: 28-March 10

Re: Random Array

Posted 14 April 2010 - 03:57 PM

There is a difference between going through a TUTORIAL and having someone FINISH YOUR PROJECT for you. You didn't talk him through the code, you simply posted it. The tutorial explains everything that is going on with the code that they used as an example. People can learn from being walked through the steps of a problem, if the reason for implementing various solutions is explained. Do you honestly understand the difference?

EDIT:
And one more thing

Quote

I need help on generating an array that generates random numbers from 0 to 100


the size of the array is 10, and also I'm asked to get the values in the array and display them in reverse after the random array has been generated

Here's a sample output:
The random generated array is : 94_56_87_12_10_13_45_56_23_52

The reversed array is: 52_23_56_45_13_10_12_87_56_94


Sounds like a homework assignment to me.

Quote

How would I put this into an array? I'm having trouble putting the values into an array that generates random number because all I know to do is generate the random numbers.


It also sounds like he had no idea what an array was. He only knew that to complete this assignment (homework or other) that he needed to use an array, but all he knows how to do is "generate the random numbers."

This post has been edited by citus: 14 April 2010 - 04:26 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1