Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
plzz someone solve my problem which is that how to store data from a file in 4D array in such a way that 1D to store characters, 1 for store words, 1 for sentences and 1 for storing paragraphs.

What I have tried:

char p[10][10][10][20];
int i,j,k,l;
ifstream fi;
fin.open("data.txt");
while(fi);
for(i=0;i<10;i++)
{
    for(j=0;j<10;j++)
    {
        for(k=0;k<10;k++)
        {
            for(l=0;l<20;l++)
            {
                fi.get(ch);
                p[i][j][k][l]=ch;
                c
                if(ch==' '||ch=='.')
                break;
            }
           if(ch=='.')
           break;
        }
    }
}
Posted
Updated 3-Jan-21 6:10am
Comments
Rick York 2-Jan-21 15:47pm    
The word is please.
Mian Ehsan 3-Jan-21 2:42am    
yes
Dave Kreskowiak 2-Jan-21 17:54pm    
You're visualizing how to store that data all wrong. You don't need a 4 dimensional array for this at all. You could get away with a 2 dimensional array or (4) 1 dimensional arrays.
Mian Ehsan 3-Jan-21 2:33am    
I know it can be done easily using 2 dimensional array but it's restricted for me. I must have to use 4d array. So tell me how to do that
Dave Kreskowiak 3-Jan-21 11:51am    
You would NEVER use a 4 dimensional array for something like this. Think about it. Why would you use a cubed data structure to store lists? You wouldn't! It would result in massive amounts of unused elements in the array. That's not efficient at all.

Back to your problem. You would NOT use a loop, inside a loop, inside a loop, inside a loop. Treat each dimension as its own array, one for each list. The indexes you're not going to use for each list can all be 0, except for the one you're adding an item to.

Why on earth do you think that's a good idea?

Think about it. Just for a moment.

This reply - so far - has three paragraphs, four sentences, thirty five words, and ... 189 characters.

So your solution needs very oddly shaped array: one dimension of 170, one of 4, one of 35, and one of 3.
And until you have read and established the first dimension, filled it, analysed it, and worked out what those sizes are, you can't create any of it because arrays can't be resized. Instead, store your data in a 1D array, and store indexes to where words, sentences, and paragraphs start in separate arrays.

I have no idea why you are doing this, but I think you need to consider exactly what you are going to do with the data before leaping into code!
 
Share this answer
 
Comments
Stefan_Lang 3-Jan-21 12:15pm    
Actually the sizes must only be sufficient for paragraphs per file, sentences per paragraph, words per sentence, and chars per word. but that still raises the question on how large these limits must be?
OriginalGriff 3-Jan-21 12:28pm    
Well, James Joyce wrote a whole book with just two full stops and a single comma ... :laugh:
Stefan_Lang 3-Jan-21 12:34pm    
When I was younger I used to write very long sentences, but I did use a lot of commas, hyphens, and semicolons. The grammar was fine, according to my teacher, but I never made it past one page. Anyway, when she made me read out loud those sentences I was cured! ;-p

If you do it with this data structure, you should store a 0 character when you encounter a ' ', so your words are properly 0-terminated. Otherwise you can not know how long your word is.

That said, your fixed size array cannot handle text files with more than the allocated number of paragraphs, sentences per paragraph, words per sentence, and characters per word. Technically you must allocate the required amount of memory dynamically at every level, and your top level data type would be
C++
char ****paragraphs_of_sentences_of_words_of_chars;
Of course, to allocate the right amount of paragraphs, you need to first scan the text and find out how many there are; then you can allocate the required amount, and then you must scan the file again to actually read the paragraphs and store their contents. Only that storing these contents again requires a double scan: the first for counting the sentences and the second to actually store them after you allocated the required number of sentence-pointers. During storing a sentence, you again need to double scan to find out the number of words, and during reading a word you need to double scan to first find out the length of the word.

The big question is, what do you want to achieve, and would it be ok to use a different data structure, and C++ container classes? That would simplify the task enourmously, and after all you tagged this question as C++, so why not use it's features?

P.S.: it could be as simple as this:
C++
std::vector<std::vector<std::vector<std::string>>> paragraphs_of_sentences_of_words;
This definition saves you the effort of memory managment and precounting the required amounts of sentences and words. Just push_back() the words up to the end of a sentence, push_back() the sentences up to the end of the paragraph (btw., how do you recognize a new paragraph?), and push_back() the paragraphs up to the end of the file.

P.P.S.: in one of your comments you mentioned that you must use a 4D array. note that the suggested solution is, technically, a 4D array, becausee std::string is effectively a 1D array of chars.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900