dynamic array

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
# include <iostream>
using namespace std ;

class Circle {
	  	  	  	private :
						 static const float pi=3.142 ;
	  		   			 float radius ;
				public :
	 	 			   	 void setRadius ( float ) ;
	 	 			   	 float getArea () ;
			 };

void Circle::setRadius( float r)
{
 radius = r ;
}

float Circle::getArea()
{
 	  return pi * radius * radius ;
}

int main ()
{
 Circle* circles ;
 int numberOfCircles ;
 float r ;

 cout << "Enter Number of Circles :  " ;
 cin >> numberOfCircles ;
 cout << endl << endl ;
 
 circles = new Circle [ numberOfCircles ] ;
 
 for ( int i=0 ; i < numberOfCircles ; i ++ )
 {
  	 cout << "Enter radius of Circle " << i+1 << " : " ; 
  	 cin >> circles.setRadius(r)[i] ;
  	 cout << endl ;
 }


 system("pause") ;
 return 0 ;
}
Last edited on
there is an error in line 38
what is the error ?????????????????????????????
That line doesn't make sense at all, you should have
1
2
cin >> r; // see http://www.cplusplus.com/forum/articles/6046/
circles[i].setRadius();
Thank you Bazzy
really thanks
i want this prog to give the same output of the pervious one but i didnt want an array of Circles i want array of float can any one help me ?
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
# include <iostream>
using namespace std ;

class StaticCircle {
	  			   private :
				   		   	static const float pi = 3.142 ;
	   		   	   public  :
				   		   	static float getArea ( float ) ;
				   };

static float StaticCircle::getArea( float r ) 
{
 return r * r * pi ;
}

int main ()
{
 int numberOfCircles ;

 cout << "Enter Number of Circle : " ;
 cin >> numberOfCircles ;

 StaticCircle circle ;
 float raduis[numberOfCircles] ;
 
 for ( int i = 0 ; i <numberOfCircles ; i++ )
 {
  	 cout << "Enter radius of Circle " << i+1 << " : " ;
  	 cin >> raduis[i] ;
 }

 for ( int i=0 ; i < numberOfCircles ; i ++ )
 {
  	 cout << circle.getArea(i);
 }
 system("pause") ;
 return 0 ;
}
Last edited on
Line 24: you should have a dynamic array there ( not a fixed-size one )
i want to use an array of floats
instead of array of Circles i want to use array of floats can i do this ?
You can but you need a dynamic array ( or a std:: container )
Topic archived. No new replies allowed.