Archived

This topic is now archived and is closed to further replies.

ATronic

HELP! Matrix/Array Problems

Recommended Posts

ATronic    122
Hey, I load information from a file, the format looks like this.
  
NumberOfPairs
PairPart1 PairPart2
PairPart1 PairPart2
PairPart1 PairPart2
PairPart1 PairPart2
PairPart1 PairPart2
.
.
.
  
What I need to do is to create a matrix like so.
  
float Data[NumberOfPairs][2];
  
But I find this very hard to do. I have tried to do this using dynamic memory allocation (ie: float *x=new float[3]), but I can''t figure out how to do this with a matrix, only an array. Any help is greatly appreciated. Alex Broadwin A-Tronic Software & Design ----- "if you fail in life, you were destined to fail. If you suceed in life, call me." "The answer is out there." "Please help, I''m using Windows!"

Share this post


Link to post
Share on other sites
Null and Void    1088
This is one of the cheats you can use with dynamic allocation when the non-first indices are constant:
  
float *(Data[2]);
Data = new Data[Number][2];

I''ve never had to do this, but as far as I know it should work. Otherwise, for clarity, do this:
  
struct Pair {
float A, B;
};
Pair *Data;
Data = new Pair[Number];


[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites
ATronic    122
Thank you SOOO much! I''ll have to do the first one, since it MUST be a matrix of floats in the format of a matrix of floats. The function I pass it to requires it, it''s for opengl vertex arrays. Thank you again!

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"

Share this post


Link to post
Share on other sites
transcendental    122
#include

int main()
{
int (*x)[3]=new int[2][3];


x[0][0]=1;x[0][1]=2;x[0][2]=3;
x[1][0]=4;x[1][1]=5;x[1][2]=6;
printf("\n%d %d %d", x[0][0], x[0][1], x[0][2]);
printf("\n%d %d %d", x[1][0], x[1][1], x[1][2]);


system("PAUSE");
return 0;
}

Share this post


Link to post
Share on other sites
ATronic    122
Null And Void,
Problem. I get the error "no an lvalue" when I try to compile. It thinks that your code:
  
float *(Data[2]);
Data = new Data[Number][2];

should be this:
  
float *(Data[2]);
Data[0] = new Data[Number][2];

or something of the like. By the way did you mean Data=new float[number][2]? Anyway, any ideas?

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"

Share this post


Link to post
Share on other sites
burp    122
  
#include <stdio.h>
#include <stdlib.h>
//

void call(float f[][2])
{
}
//

void main(void)
{
int x = rand()&0xff;
call(new float [x][2]);
float (*m)[2] = new float [x][2];
}

Share this post


Link to post
Share on other sites
Null and Void    1088
quote:
Original post by ATronic
By the way did you mean Data=new float[number][2]?


Yes, I did. I think it goes like this though, to correct myself:
    
float (*Data)[2];
Data = new float[Number][2];

It is kind of unintelligible, I know, that's why I had to look it up .

[Resist Windows XP's Invasive Production Activation Technology!]

Edited by - Null and Void on November 9, 2001 8:10:57 PM

Share this post


Link to post
Share on other sites