Giving values to arrays

I have 5 arrays for 5 different cars racing on a drag strip. I need to give the first slot in the array -->[0]<-- or 's'a RANDOM value. For each time these cars race the outcome needs to be different.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int first;
char chevy[] = { 's', 't', 'd', '\0' }; //s= top mph t=total time racing d=distance of the track
int main()
{
//would it be like this?
chevy[0] = ( value % 180 + 163 ) = first;
chevy[1] = ((360\first) * 10); //representing time
chevy[2] = 1; //1 standing for the 1 mile length of the track
}
Last edited on
1
2
3
4
//rare to see multiple assignments - i'd discourage it just based on the general confusion it often causes...
//chevy[0] would have the value of 'first'
//i rly dont kno what happens to the middle since u have a variable with constants...my IDE is giving errors...
chevy[0] = ( value % 180 + 163 ) = first;  


anyway, chevy[0] = value % 180 + 163 is ok. all u need now is a library that will give u a pseudo random number to be stored into 'value'. there's a simple rand() function out there that should suit most basic needs
Last edited on
rare to see multiple assignments in arrays?

I need the outcome of the random from chevy[0] so that I can use it for the time. Would that require me to set the rand() fuction, then print the value of the rand as an int? or could I use value as a string?
I'm just saying if it isn't going to be clear what u'r doing (which in the OP it isn't since u'r assigning a uninitialized variable first to chevy[0] and value % 180 + 163 //which for me failed because of the constants 180+163 , then just separate it in a few steps for the sake of readability...

All u need to do is:

chevy[0] = rand() %180 +163;//to get a random # into chevy[0] between 163 and 339 inclusive

If u need to use that particular random number again then u need to store it into a var like 'value':

1
2
int value = rand();
chevy[0] = value%180 +163;


Do u mean to use / instead of \ on line 10?

Is this what u mean (after storing the random # into chevy[0])?

chevy[1] = ((360/chevy[0]) * 10);
Last edited on
closed account (o3hC5Di1)
Hi there,

Just a small addition to soranz's post - don't forget to do srand(time()); once in main(), before calling random.

Just to avoid further confusion :)

All the best,
NwN
Ah, okay. That was what I was thinking you ment, and i did find the rand() on the tutorials. And yes i did mean to use / I was up all day coding, by the time I posted this i was brain dead. hah.

Thanks for the help soranz!
Thanks NwN.

Now I have to figure out how to post the 5 cars like a scoreboard, but i'll be back to show you a final result later!

Much appreciated.
Sounds good - cheers :D
Topic archived. No new replies allowed.