473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Creating and writing to large arrays

4
I've been learning Visual C++ on the fly at work for the last 2 weeks and could use some help. I'm writing a program to read a .raw file which has a standard format of 9 columns and then depending on the size of the object, a large number of rows. I then need to rotate it and write the new position of the object to a new .raw file. I started with a box as a simple example that had only 28 rows. The code worked fine for the small number of rows, but the objects I'll be using have approx. 35000 rows and it's overloading the arrays "geom" and "geom_r". Any tips for me to get around this would be greatly appreciated.
Thanks,
Adam Bunn

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h> //For input/output
  2. #include<math.h>  //For math functions
  3. #include<stdio.h> //For fopen, FILE...
  4. int main()
  5.  
  6. {
  7.     //Declaration of Variables
  8.     const float pi = 3.14159f;
  9.     float degrees = 45.0f;
  10.     float radians  = degrees * pi / 180;
  11.     int i, j, k, p, q, r, s, c, d, w, lines;
  12.     float geom[28][9];
  13.     float geom_r[28][9];
  14.     float vertice1[3][1], vertice2[3][1], vertice3[3][1];
  15.     float r_vertice1[3][1], r_vertice2[3][1], r_vertice3[3][1];
  16.     float sum1, sum2, sum3;
  17.     float z_rotate[3][3] = {
  18.                                 {cos(radians), -sin(radians), 0},    //rotation matrices
  19.                                 {sin(radians), cos(radians), 0},
  20.                                 {0,0,1}
  21.                             };
  22.     float y_rotate[3][3] = {
  23.                                 {cos(radians), 0, -sin(radians)},
  24.                                 {0,1,0},
  25.                                 {sin(radians), 0, cos(radians)}
  26.                             };
  27.  
  28.     FILE * praw;  //Open file to read
  29.     praw = fopen("D:\\Adam\\Hip ROM Project\\C++\\figure_it_out\\box.raw", "r");  //File name!
  30.  
  31.     //for (i = 0; i < 28; i++){ //Read file into box array
  32.     i = 0;
  33.     while (!feof(praw)) {
  34.         for (j = 0; j < 9; j++){
  35.             fscanf(praw, "%f", &geom[i][j]);
  36.         }
  37.         i = i +1;
  38.     }
  39.     fclose(praw);
  40.  
  41.     lines = i - 1;  //number of lines in the file
  42.     for (p = 0; p < lines; p++){  //Break the rows of the array into 3x1 vectors
  43.         r = 3; s = 6; //initialize counters
  44.         for (q = 0; q < 3; q++){
  45.             vertice1[q][0] = geom[p][q];   //First three columns into first vertice
  46.             vertice2[q][0] = geom[p][r];   //Second three columns into second vertice
  47.             vertice3[q][0] = geom[p][s];   //Third three columns into third vertice
  48.             r = r + 1;    //increase counters
  49.             s = s + 1;       
  50.  
  51.         }
  52.  
  53.  
  54.         for(i=0;i<3;i++){ //Multiply matrices to rotate geometry
  55.             j = 0;
  56.             sum1 = 0; sum2 = 0; sum3 = 0;
  57.             for(k=0;k<3;k++){
  58.                 sum1 = sum1 + (y_rotate[i][k] * vertice1[k][j]);
  59.                 sum2 = sum2 + (y_rotate[i][k] * vertice2[k][j]);
  60.                 sum3 = sum3 + (y_rotate[i][k] * vertice3[k][j]);
  61.                 r_vertice1[i][j] = sum1;
  62.                 r_vertice2[i][j] = sum2;
  63.                 r_vertice3[i][j] = sum3;
  64.             }
  65.  
  66.         }
  67.         for (w = 0; w < 3; w++){  //Store rotated vertices in new array, box_r
  68.             c = w +3; d = w + 6;
  69.             geom_r[p][w] = r_vertice1[w][0];
  70.             geom_r[p][c] = r_vertice2[w][0];
  71.             geom_r[p][d] = r_vertice3[w][0];
  72.         }
  73.     }
  74.  
  75.     praw = fopen("D:\\Adam\\Hip ROM Project\\C++\\figure_it_out\\box_r.raw", "a"); //Write new array to new file by appending
  76.     for (i = 0; i < lines; i++){
  77.         for (j = 0; j < 9; j++){
  78.             fprintf(praw, "%f ", geom_r[i][j]);
  79.         }
  80.         fprintf (praw, "\n");
  81.     }
  82.     return 0;
  83. }
  84.  
Dec 28 '07 #1
7 2589
Savage
1,764 Expert 1GB
I've been learning Visual C++ on the fly at work for the last 2 weeks and could use some help. I'm writing a program to read a .raw file which has a standard format of 9 columns and then depending on the size of the object, a large number of rows. I then need to rotate it and write the new position of the object to a new .raw file. I started with a box as a simple example that had only 28 rows. The code worked fine for the small number of rows, but the objects I'll be using have approx. 35000 rows and it's overloading the arrays "geom" and "geom_r". Any tips for me to get around this would be greatly appreciated.
Thanks,
Adam Bunn
Do you know how to allocate dynamic memory?

Savage
Dec 28 '07 #2
abunn
4
I'm trying to wrap my head around it, as well as the use of pointers in general, but the book I've got doesn't give a good example of how to do things multi dimensionally.
Dec 28 '07 #3
Savage
1,764 Expert 1GB
I'm trying to wrap my head around it, as well as the use of pointers in general, but the book I've got doesn't give a good example of how to do things multi dimensionally.
When allocating multidimensional arrays on heap,you must first allocate array of pointers,and the for every pointer you allocate array of data that pointer can point to.In your float example that would be:

Expand|Select|Wrap|Line Numbers
  1. float **somefloat;
  2.  
  3. somefloat=new float*[elements in 1D];
  4. for(i=0;i<1D;i++) somefloat[i]=new float[elements in 2D];
But when you are done with it you must free it using reverse process.

Savage
Dec 28 '07 #4
Savage
1,764 Expert 1GB
Other solution is to use STL's vectors,if you know how to use them,which manages its memory all by it self.


Savage
Dec 29 '07 #5
abunn
4
Thanks for the help. I was able to get that to work pretty easily. There is one other thing that you might be able to help me with. Is there a way to determine how many lines/rows the .raw file has before I set how much memory is dynamically allocated? Or should I just set the array row size high enough that I won't have to worry about a file approaching the max size? If you could point me in the right direction I'd be very greatful. I changed my code to read like this now:
Expand|Select|Wrap|Line Numbers
  1. float z_rotate[3][3] = {
  2.                                 {cos(radians), -sin(radians), 0},    //rotation matrices
  3.                                 {sin(radians), cos(radians), 0},
  4.                                 {0,0,1}
  5.                             };
  6.     float y_rotate[3][3] = {
  7.                                 {cos(radians), 0, -sin(radians)},
  8.                                 {0,1,0},
  9.                                 {sin(radians), 0, cos(radians)}
  10.                             };
  11.  
  12.     geom = new float*[40000]; //Dynamic memory allocation
  13.     for (i = 0; i<40000; i++) geom[i] = new float[9];
  14.     geom_r = new float*[40000];
  15.     for (i = 0; i<40000; i++) geom_r[i] = new float[9];
  16.  
  17.     FILE * praw;  //Open file to read
  18.  
Everything else is the same until I delete the arrays near the end. Thanks again
Dec 29 '07 #6
Savage
1,764 Expert 1GB
Thanks for the help. I was able to get that to work pretty easily. There is one other thing that you might be able to help me with. Is there a way to determine how many lines/rows the .raw file has before I set how much memory is dynamically allocated? Or should I just set the array row size high enough that I won't have to worry about a file approaching the max size? If you could point me in the right direction I'd be very greatful. I changed my code to read like this now:
Expand|Select|Wrap|Line Numbers
  1. float z_rotate[3][3] = {
  2.                                 {cos(radians), -sin(radians), 0},    //rotation matrices
  3.                                 {sin(radians), cos(radians), 0},
  4.                                 {0,0,1}
  5.                             };
  6.     float y_rotate[3][3] = {
  7.                                 {cos(radians), 0, -sin(radians)},
  8.                                 {0,1,0},
  9.                                 {sin(radians), 0, cos(radians)}
  10.                             };
  11.  
  12.     geom = new float*[40000]; //Dynamic memory allocation
  13.     for (i = 0; i<40000; i++) geom[i] = new float[9];
  14.     geom_r = new float*[40000];
  15.     for (i = 0; i<40000; i++) geom_r[i] = new float[9];
  16.  
  17.     FILE * praw;  //Open file to read
  18.  
Everything else is the same until I delete the arrays near the end. Thanks again
Yes,there are varous ways to do that.One is to get size of file and size of single line(there are 9 columns so each row must have same size),and other is a simple while loop,while EOF is not reached read in a line and increase number of lines counter

Savage
Dec 29 '07 #7
abunn
4
Thanks for your help. I got it wrapped up.
Dec 29 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
8
by: Oenone | last post by:
Is it possible to create an object which can have methods and properties, but which can also be treated as a string? I'm trying to create a wrapper around the IIS Request.Form object which...
2
by: stealth_spoof | last post by:
Hi People wondering if anyone can help me with a problem I'm having I'm trying to create an array with an unspecified length, the length is based on the result i get from another task in the code...
38
by: djhulme | last post by:
Hi, I'm using GCC. Please could you tell me, what is the maximum number of array elements that I can create in C, i.e. char* anArray = (char*) calloc( ??MAX?? , sizeof(char) ) ; I've...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
6
by: lawpoop | last post by:
Hello! I am working on a map of a rather large php project that I've been working on. I hope to create a map of the files of the project that would look like the output of the unix 'tree'...
2
by: Aryan | last post by:
Hi, I am using C# with framework 2.0 and creating PDF files on-fly, along with this I am using Windows 2003 Server. I am using Byte to take the data input and then save into pdf format on...
7
by: =?Utf-8?B?U3RldmVa?= | last post by:
First off, I am not sure if this belongs in this group or the C# group. It seems more like a C++ problem to me. Anyways... I have a C# project which links in an unmanaged C++ DLL, both built...
3
by: hamishd | last post by:
What is the best way to store large arrays of numbers (eg, 4-byte integers)? Say I want to store an array of 1billion 4-byte integers.. If my computer has 4gB memory, then is this possible? ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.