6 Replies - 16363 Views - Last Post: 05 May 2007 - 08:29 PM Rate Topic: -----

#1 aliz2000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-May 07

2d array, storing value?

Posted 03 May 2007 - 04:59 PM

hi,

I have a program which creates a list of x and y values from a calculation.
e.g sx sy
10 20
15 67
20 70

Iam trying to store these into an array each time it calculates a value for x and y.

declare:
double graph[7][7]

my code:
for (int c=0; c<= sy; c++)
{
for (int x=0; x<=sx; x++)
{

graph[x][c]=' ';
cout << graph+1 << endl;

am i going about this the complete wrong way?
do i need for loops to store the results?

Is This A Good Question/Topic? 0
  • +

Replies To: 2d array, storing value?

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: 2d array, storing value?

Posted 03 May 2007 - 06:39 PM

Well your logic seems a but off... but the idea is sound enough. I find it easier to store corrdinates in a structure than an array

typedef struct
{
double x;
double y;
} Coord;

...

Coord Points[100]; //Array of 100 points...
int i; //Counter

For (i=0; i< 100; ++i)
{
	Points[i].x= i;
	Points[i].y = i +1;
}


This simplifies things. Otherwize your code has to look something like:
For (i=0; i< 100; ++i)
{
	Points[i][0]= i;
	Points[i][1] = i +1;
}


If you are trying to make something akin to a bitmap (and array of points with values at the given points) then the 2d array is the general direction, but I find that even here it is often simpeler to use a 1D-array and just calculate the position I need
value = Point[x][y]; // a point in a 2D array...

index = x + y * (width);
value = Point1D[index]; //Point in a 1D array holding 2D data...

Was This Post Helpful? 0
  • +
  • -

#3 aliz2000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-May 07

Re: 2d array, storing value?

Posted 03 May 2007 - 06:50 PM

so putting the results in a 2d array would look somehitng like this:?

for (int c=0; c<= sy; c++)


{

graph[c][0]= c;

graph[c][1] = c +1;

}



for (int x=0; x<= sx; x++)

{


graph[0][x]= x;

graph[1][x] = x+1;

}
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: 2d array, storing value?

Posted 03 May 2007 - 09:39 PM

what? no. what? NO NO NO

Ok lets say that I am using a 2D array to hold a set of (x, y) points. like so:

point1 = (10, 20)
point2 = (15, 67)
point3 = (20, 70)

I have 3 points, each containing 2 pieces of information so I will make a 3 row, 2 column array

int Points[3][2];

now the x-coordinate of the first point is:

Points[0][0] = 10;

The y-coordinate of the first point is:

Points[0][1] = 20;

Next:

Points[1][0]=15; //x data
points[1][1]=67; //y data

Next:

Points[2][0] = 20; //x data
Points[2][1] = 70; //y data

Maybe I just don't understand what you are trying to do...
Was This Post Helpful? 0
  • +
  • -

#5 aliz2000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-May 07

Re: 2d array, storing value?

Posted 03 May 2007 - 10:09 PM

iam just trying to get the x values and y values into the array.
ithe x and y values are being created in a loop through a calculation until y axis is <= 0.

can i add teh values (x,y) in the loop.
or what is the best way to add them, do i collect the data and then put them in the array outside the loop.

the program wont let me put graph[sy][sx] which is the y axis and x axis every time the loop occurs.
its prolly a simple issue but i just cant seem to be able to get it to collect all the x and y axis that are being calculated. going insane.



MY ATTEMPT:
while (sy > 0)
{
v = vsinx - (grav * timeint);	
						
timeint1 = timeint + 1;
						
timeint = timeint1;
			
s2 = sy + (v * timeint);
						
sx2 = vcosx * timeint;
												
sx =	sx2 * 0.2;
						
sy =	s2 * 0.2;
for (int y=0; y<=1; y++)		   
							{
								
graph[y][0+1]=sx;


								
for (int x=0; x<=1; x++)
								{
									
graph [0+1][x]=sy;

										for (int x=0; x<=1; x++)						
									
				
}
	}													
							
									
}
				
}

Was This Post Helpful? 0
  • +
  • -

#6 aliz2000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-May 07

Re: 2d array, storing value?

Posted 04 May 2007 - 05:26 PM

hmm, i guess i will try figure it out
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: 2d array, storing value?

Posted 05 May 2007 - 08:29 PM

I don't know how much plainer I can make this:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	double graph[100][2];
	double x, y;
	int count;

	for (count=0; count < 100; ++count)
	{
		x = 0 + (3.14159/100)*count;
		y = 100 * sin(x);
		graph[count][0]=x;
		graph[count][1]=y;
	}

	for (count=0; count < 10; ++count)
	{
		cout << "X: " << graph[count][0] << '\t' << "Y: " << graph[count][1] << endl;
	}
	return 0;
}

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1