how can i randomly generate the values of members of struct

Oct 7, 2012 at 4:36pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using namespace std;
struct rectinfo
{
	int height,width,area,perimeter;
}R1,*R2;


void genData (rectinfo* );
void calAreaPer (rectinfo* );
void display (rectinfo* );
int main()
{
	R2=&R1;
	genData (R2);
	getch();
	return 0;
}
void genData (rectinfo* R)
{
	cout<<"Randomnly generated value of height is "<<R->height(rand()%20);
	cout<<"Randomnly generated value of width is "<<R->width(rand()%20);

}



this is my code and i want to randomly generate the values of height and width......... will someone help plz
Oct 7, 2012 at 4:40pm
1
2
h = rand() % 100 + 1;   // random number 1-100
w = rand() % 100 + 1;


and seed srand
Last edited on Oct 7, 2012 at 4:41pm
Oct 7, 2012 at 4:43pm
Use the rand() function from <cstdlib>. Then you can make a constructor which will initialize the values when the structure is created.

Hopefully this helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <cstdlib> // for rand() and srand()
#include <ctime>  // for time()
#include <iostream> // for cout and endl
using namespace std;

struct rectinfo
{
	int height,width,area,perimeter;

	rectinfo() // Constructor, automatically called when you create an object
	{
		height = rand()%20;
		width  = rand()%20;
		area = height * width;
		perimeter = 2*height + 2*width;
	}
}; // I don't like making global variables, we'll do this in main

void display(rectinfo* R)
{
	cout << "Rondomly generated value of height is " << R->height << endl;
	cout << "Randomly generated value of width  is " << R->width  << endl;
}

int main()
{
	srand( time(NULL) );
	rectinfo R1; // height and width are automatically generated
	rectinfo* R2 = &R1;
	
	display(&R1); // I wanted to demonstrate that you can pass the address without creating R2
	display(R2); // This'll show the same thing because it's pointing to the same object
	
	return 0;
}
Last edited on Oct 7, 2012 at 4:50pm
Oct 7, 2012 at 4:45pm
actually i have to generate the values from using a function......
Oct 7, 2012 at 4:49pm
in my way of inilization thers an error in line
1
2
cout<<"Randomnly generated value of height is "<<R->height(rand()%20);
cout<<"Randomnly generated value of width is "<<R->width(rand()%20);


the error is tht expression must have (pointer-to-) function type
Oct 7, 2012 at 4:56pm
thnx u brother
but isnt there any way tp do it in function gendata(rectinfo*R) ???
Last edited on Oct 7, 2012 at 4:57pm
Oct 7, 2012 at 5:01pm
i actually have to write a function to randomly generate the values of height and width..............


not by using constructor.......i am new to c++
and this is my home work i google alot but ahh!!! nothing found :(
so i posted here
Oct 7, 2012 at 5:35pm
1
2
3
4
5
void genData (rectinfo* R)
{
	cout<<"Randomnly generated value of height is "<< (R->height = rand()%20);
	cout<<"Randomnly generated value of width is "<< (R->width = rand()%20);
}
Oct 7, 2012 at 5:41pm
woww :)
thtx really working :)
and wil u please tell me how these brackets are working ??
i mean how can we assign int to an pointer ?
Oct 7, 2012 at 6:37pm
I'm not sure I understand what you're referring to by brackets. The parentheses are required in order to keep operator precedence from interfering with what we want to accomplish.

As for the other, we aren't assigning an int to a pointer. We're assigning numbers to the height and width members of the struct that R points to.
Topic archived. No new replies allowed.