?
Solved

Global Arrays  in Borland C++ Builder (Variable array)

Posted on 2002-06-16
11
Medium Priority
?
1,200 Views
Last Modified: 2011-04-14
I want to create a variable 3 dimensional array in c++ that i can change and access any where in the program

this is what I want to do, I want to enter a string and then from there should the 3 dimenional array be be build.
If I enter a new string a new array should be build.

At the moment I have to specify the lenght of the array
this should be done automatically

the code i've got I think is static

My code sofar

{
   char charset[] =  "groen";  //I must be able to change this
   int str_length = strlen(charset);
   char cubearr[5][5][5];
   // I must be able to use this array through the program
   // edit, re-initialize the array

   int i=0;

   int x=0;
   int y=0;
   int z=0;

   for ( x = 0; x < str_length; x++)
   {
     for ( y = 0; y < str_length; y++)
     {
       for ( z = 0 ; z < str_length ; z++)
       {
         if (i==5)
           i=0;
         cubearr[x][y][z] =  charset[i];
       }
     }
   }
}  

I want to make this dynamic and accessable all over in the program

thanks
0
Comment
Question by:morees
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +3
11 Comments
 

Accepted Solution

by:
Boots111 earned 260 total points
ID: 7083684
Morees~

To make the object visible throughout the program you will have to make it a global.  As for dynamic, you have two options.

The easier and cleaner option:

#include <vector.h>
//#include <vector> on some systems

vector<vector<vector<char>>> cubearr;

that line will declare a vector of vectors of vectors.  Thus giving you a 3-d array.  Each vector can be resized via the resize function.  Remember that you have to resize each sub vector independantly.  (If I were you, I would wrap it in a class to do most of that stuff for you)

The uglier way:

char ***cubearr;

that will give you a pointer to a pointer to a pointer of char arrays.  Each pointer must be allocated and deallocated independantly as above.

Best of luck,
Boots
0
 

Author Comment

by:morees
ID: 7083981
This is the error I get when I use this decleration

#include <vector.h>

vector<vector<vector<char>>> cubearr;


E2299 Cannot generate template specialization from 'specifier'     Compiler error
0
 
LVL 3

Expert Comment

by:Crius
ID: 7084145
Ok, well, Boots111 has it pretty much correct. The only thing I can do along his answer is provide for you the allocation code:

char ***cubearr;
char charset[] =  "groen";
int str_length = strlen(charset);
int i,j;

cubearr = (char ***)malloc(str_length*sizeof(char **));
if(!cubearr)
   //output that you are out of memory/allocation failed
for(i=0;i<str_length;i++)
{
   cubearr[i]=(char **)malloc(str_length*sizeof(char *));
   if(!cubearr[i])
      //output that you are out of memory/allocation failed
   for(j=0;j<str_length;j++)
   {
      cubearr[i][j]=(char *)malloc(str_length*sizeof(char));
      if(!cubearr[i][j])
         //output that you are out of memory/allocation failed
   }
}

Note: You will need to fill in the //output that you are out of memory/allocation failed with whatever error reporting mechanism you have (I don't know if you're in a windows app or not), and likely exit/deallocate the memory. I can provide the deallocation routines if you want.

The only other option as far as simulating a 3 dimensional array is to make a one dimensional array, like so:

cubearr = (char *)malloc(str_length*str_length*str_length*sizeof(char));

Once you have this one dimensional array made, you can access it by using multiples of the str_length, that is:

cubearr[x*str_length*str_length+y*str_length+z];

Let me know if you still have questions.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:Boots111
ID: 7084205
Morees~

Sorry apparently the compiler you are using does not fully support the language specification (I cannot really blame it, very few C++ compilers do)

The following program runs and works in VC++ 6.0:

#include "stdafx.h"
#include <iostream>
#include <vector>

using namespace std;

typedef vector<char> D1;
typedef vector<D1> D2;
typedef vector<D2> D3;

int main(int argc, char* argv[])
{

     cout << "Hello World!" << endl;

     D3 v3;
     
     v3.resize(3);
     for(int i = 0; i < 3; i++)
     {
          v3[i].resize(3);
          for(int j = 0; j < 3; j++)
          {
               v3[i][j].resize(3);
               for(int k = 0; k < 3; k++)
                    v3[i][j][k] = 't';
          }
     }

     for(i = 0; i < 3; i++)
     {
          for(int j = 0; j < 3; j++)
          {
               for(int k = 0; k < 3; k++)
                    cout << v3[i][j][k] << ' ';
               cout << "1";
          }
          cout << endl;
     }

     char z;
     cin >> z;
     return 0;
}

Notice the typedefs to get around the problem of nesting the templates...  Same concept though.

Boots
0
 
LVL 30

Expert Comment

by:Axter
ID: 7084854
Some corrections:
>>#include <vector.h>
>>//#include <vector> on some systems

On ALL standard C++ compilers, it should be <vector>, and not <vector.h>.
You should never use <vector.h> in your code.


The following code is also incorrect:
vector<vector<vector<char>>> cubearr;

Instead, use the following syntax:
vector<vector<vector<char> > > cubearr;

It might be hard to notice with the font type on this web site, but there is a space between >
In other words:
vector<vector<vector<char>[space]>[space]> cubearr;

This is needed with template types within template types.

morees,
This is also probably the cause of your compile error.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7084906
You also need to use the resize function for each array item.
Example code:
//Needed for VC++
#pragma warning(disable: 4786)

#include <vector>


typedef std::vector<std::vector<std::vector<char> > > My3DimArray;


int main(int argc, char* argv[])
{
     char charset[] =  "groen";
     int str_length = strlen(charset);
     My3DimArray cubearr;
     int i=0;
     int x=0;
     int y=0;
     int z=0;
     
     for ( x = 0; x < str_length; x++)
     {
          if (!x) cubearr.resize(str_length);
          for ( y = 0; y < str_length; y++)
          {
               if (!y) cubearr[x].resize(str_length);
               for ( z = 0 ; z < str_length ; z++)
               {
                    if (!z) cubearr[x][y].resize(str_length);
                    if (i==str_length)
                         i=0;
                    cubearr[x][y][z] =  charset[i++];
               }
          }
     }

     system("pause");
     return 0;
}
0
 

Author Comment

by:morees
ID: 7086135
This might help:

I'm using Borland C++ Builder 5
I want to read in a charset (my example groen) in a text box, and then from there create a global 3 dimesional array
that I can use in the program.

I only start creating the cube on a buttonclick event.

If I change the charset and re-create the cube, the cube must be re-created with the new charset of any lenght.

I' hope this helps

0
 
LVL 30

Expert Comment

by:Axter
ID: 7088013
morees,
Did you try the example I posted?

If you're not satisfied with the answer posted, you should reject it so that you have a better chance of getting a good answer from another expert.
0
 
LVL 11

Expert Comment

by:griessh
ID: 7284918
Dear morees

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "boots111"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 30

Expert Comment

by:Axter
ID: 7285079
>>Unless there is objection or further activity,  I will suggest to accept

IMHO, the points should be split.
The answer posted by boots111, was rejected.
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7332946
Force accepted

** Mindphaser - Community Support Moderator **

Axter,Crius there will be a separate question with points for your help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question