Multiple 2D arrays using oop

Hi,
I am new to object oriented programming. As a part of my work I need to generate 100 points(x,y) making a line. I need ten such lines. I tried to make a line class and then 10 objects of line each to store 100 points with x and y coordinates making it a 100X2 array. I am struck here. How to retain the values. I have even tried with my elementary pointer knowledge but that is not working either.
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
#include <windows.h>
#include <stdio.h>
#include <math.h>


using namespace std;


class Line
{
  public:
  float x,y;
  float  Lpts[100][2];  

  void line1(int);

};
int main(){    
            int Lno=10;
	    Line L[10];
			

	   for (int k=1;k<=Lno;k++)
	     {	                 				
		
		L[k].line1(k);
                     
	     }
	                 
             cout<<L[6].Lpts[40][0]<<"     "<<L[6].Lpts[40][1]; //here coordinates of 40th point in sixth line should come 
   return 0;
}

void Line::line1(int k)
{
	            
	            int i=0;
	            float Lpts[100][2];


				for(i= 0.0f;i<=100;i++)
				{
		        
					y = k+(i*0.10);
					x = k;
				
													     
					Lpts[i][0]=x;
					Lpts[i][1]=y;
					i++; 									
							
				}

         
   return Lpts;
}


I am sorry if its too elementary but thats the problem now for me. Thanks in advance.
remove line 38. Lpts[100][2] is already a member of the Line class so you don't need to define it again. This is just making a local member which is destroyed at the end of the function.

Also remove line 55. Void functions don't return anything. Also, you don't use the return value in the main code.
1
2
3
4
for (int k=1;k<=Lno;k++)
{	                 				
     L[k].line1(k);
}

This will take L[10]. Line L[10] will vary from 0 to 9 so it will break your code every time.
i am posting here your code after little improvement..
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
class Line
{
  public:
  float x,y;
  float  Lpts[100][2];
  void line1(int);

public:
    Line()
    {}

};
int main()
{    
    int Lno=10;
    Line L[10];

    for (int k=1; k<Lno; k++)
    {	 
        L[k].line1(k);
    }
    cout<<L[6].Lpts[40][0]<<"     "<<L[6].Lpts[40][1]; //here coordinates of 40th point in sixth line should come 
   getchar();
    return 0;
}

void Line::line1(int k)
{
    int i = 0;
    for(i = 0; i <100; i++)
    {
	    y = k + (i * 0.10);
	    x = k;

	    Lpts[i][0] = x;
	    Lpts[i][1] = y;
    }
}
Topic archived. No new replies allowed.