Thread: 3-d array assign string values

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    164

    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. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #3
    Registered User
    Join Date
    May 2004
    Posts
    164
    cool, thanks Thantos, I'll check it out.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    164
    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. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #6
    Registered User
    Join Date
    May 2004
    Posts
    164
    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. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #8
    Registered User
    Join Date
    May 2004
    Posts
    164
    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. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > 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 );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  4. Linked list question....again
    By dP munky in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2003, 05:59 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM