i want to create N numbers of structures of type rectinfo.........

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Aqeel Abbas LW1 week5.cpp : Defines the entry point for the console application.
//
#include <cstdlib>

#include <iostream>
#include <conio.h>
using namespace std;
struct rectinfo
{
	int height,width,area,perimeter;
}R1,*R2;

void dynamic (rectinfo* );
void genData (rectinfo*,int  );
void calAreaPer (rectinfo* ,int );
void display (rectinfo* ,int );
int main()
{
	R2=&R1;
	dynamic(R2);
	
	getch();

	return 0;
}
void dynamic(rectinfo* R)
{
	int n;
	cout<<"how many structures of type rectinfo you want to declare ?? "<<endl;
	cin>>n;
	rectinfo* shape=new rectinfo[n];// this defines an array of shape[n] of rectinfo data type.
	genData (shape,n);
	calAreaPer (shape,n);
	display (shape,n);
}
	


void genData (rectinfo* R,int n)
{
	int a;
	for(int i=0; i<n; i++)
	{
	(R->height[i]=(rand()%21));
	(R->width[i]=(rand()%21));
	}
}
void calAreaPer (rectinfo* A,int n)
{
	for(int i=0; i<n; i++)
	{
	A->area[i]= (A->height*A->width);
	A->perimeter[i]=2*(A->height + A->width);
	}
}
void display (rectinfo* B,int n)
{
	for(int i=0; i<n; i++)
	{
	cout<<"Randomnly generated value of height is "<<B->height[i]<<endl;
	cout<<"Randomnly generated value of width is "<<B->width[i]<<endl;
	cout<<"Area of rectangle is :"<<B->area[i]<<endl;
	cout<<"Perimeter of rectangle is :"<<B->perimeter[i]<<endl;

	}
}



this is wht i have done.......but tht is not really working..i want to create N numbers of structs of type rectinfo and then have to save the values height,width,area and perimeter to all of them....will someone guide me tht where i am wrong.
you never use the variables R1 and R2 anywhere in the program, try looking over your program again.
i didnt get ur point dear :(
I believe what Need4Sleep meant is that there is no reason to have them in your code.

dynamic takes a parameter that it doesn't use. It also allocates memory that is immediately leaked. You don't provide a mechanism for returning the memory to main.

I think for this bit of code I would stick to allocating the memory in main.

In your functions that deal with the array, you've got the syntax wrong. For instance, genData should be:

1
2
3
4
5
6
7
8
void genData (rectinfo* R,int n)
{
    for ( int i=0;  i<n;  ++i )
    {
        R[i].height = rand()%21 ;
        R[i].width  = rand()%21 ;
    }
}


which would be equivalent to:

1
2
3
4
5
6
7
8
void genData (rectinfo* R,int n)
{
    for ( int i=0;  i<n;  ++i )
    {
        (R+i)->height = rand()%21 ;
        (R+i)->width  = rand()%21 ;
    }
}
Last edited on
cire :) thnx u brother :)
thtz really helping :)
i love u...u always helping me :)
Topic archived. No new replies allowed.