# Thread: 3-d array assign string values

1. ## 3-d array assign string values

OK, xsquared and the brain got me out of my last learning crush on arrays and I'm hoping someone can help again....

I'm creating a 3d array of strings and then trying to input an unknown number of records into the specific areas from a file using strcpy to assign variables to the array values. LOST? Me too...
heres the array
Code:
`char waves [21] [300] [5] [31];`
21 represents the number of tables to be created
300 represents the number of rows for each table
5 represents the number of columns for each table
31 represents the number of characters each string can be within the 5 columns.

heres the code on how it is used, here I am just trying to assign the input from a file recId to the rows in all 21 tables in sequence:
Code:
```ifstream fin1;
fin1.open("C:\\palletperf.txt");
if (fin1.fail())
{
cout <<"failed to open palletperf.txt";
}

ifstream fin2;
fin2.open("C:\\partnerlist.txt");
if (fin2.fail())
{
cout <<"failed to open partnerlist.txt";
}

//declare 3-d array waves
char waves [21] [300] [5] [31];

//insert partnerlist records and poplulate all waves tables
do
{
do
{
char partRow [11];
fin2>>recName;
fin2>>recId;
strcpy(waves[waveTableCount][partRow], recId );
}while (!fin2.eof());
fin2.close();
waveTableCount++;

}while (waveTableCount <=21);```
since arrays are new to me... I'm biting off alot. I've drawn the array out on paper to keep a visual, thats why I explain it from table point of few, can ya tell I'm reading a book????LOL!

Thanks for any help-

2. 1) You have a 4 dimensional array not a 3 dimensional array
2) I think its time you shifted your way of thinking of multidimensional arrays. The row col thing works fine for 2 and kinda works for 3 dimensions but with 4 or above it breaks down. I posted a nice (IMO) tree structure that you might find help. http://cboard.cprogramming.com/showt...highlight=tree

3. cool, thanks Thantos, I'll check it out.

4. Hey Thantos,
your posting does help to view that complex of array, but for the info I want to assign is there a way to use a 3d array?

the only reason I use the 4th set is because its an array of strings and I thought I needed to establish the length of the string. Is that not necessary? I got if from a book I used while studying arrays.

5. Well strings are nothing more then a fancy array of characters. So if you have an array of strings you really have a 2 dimensional array.

The question(s) I have for you is why do you want to setup your table like that?
Would using std::string be easier?

6. I need to pass through a large file, say about 50, 000 lines with 5 different elements per line each line could relate to one of 20 different persons. I'm trying to make one pass store the data in ram so this is a fast process and then make calculations on the accumulated data from each line for each person.

doing a search and handle on so many strings in such a large file was cpu intensive and a huge time hog! So I started researching the use of arrays.

I appreciate your help and taking time look at this, it is a big help. I was actually thinking of using 20 different 2 d arrays and just filling them accordingly and then using their data. But I still have a problem assign the values of the strings from the file input into the proper areas of the array. 1d array no prob 2 or > not good.

7. How about using structures and having an array of structures. Also you might look into a way to process the information as you read it instead of storing it and then reading it.

8. I will have to research structs, but that sounds worth it. I will start in my books and follow your idea and see what I can do. Probably see a post soon on structs, LOL! Thanks for the idea, I will start looking into it.

9. > the only reason I use the 4th set is because its an array of strings and I thought I needed to establish the length of the string
You are correct.
You can view it as a 4D array of chars, or
You can view it as a 3D array of strings (upto 30 chars in length, +1 for the \0)

> strcpy(waves[waveTableCount][partRow], recId );
You need a 3rd index
strcpy(waves[Table][Row][Column], recId );