473,323 Members | 1,537 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,323 software developers and data experts.

Multidimensional arrays in C

Im trying to scan a file using fscanf and want to put the results in a multidimensional array (since C has no strings I need it to store names).

Basically I want to be able to have a multidimensional array of names that were scanned in order that the file had and be able to access each one by using array[1] for example, to output one of the names.

Im very bad at multidimensional array syntax so am asking for help, I could post my code and file contents if needed, but thought someone may know how to do this without them, thanks in advance.
May 11 '07 #1
11 2316
gpraghuram
1,275 Expert 1GB
Hi,
Check this thread , it will help u.
Also search this forum .

Thanks
Raghuram
May 11 '07 #2
gpraghuram
1,275 Expert 1GB
HI,
sorry i missed the link
Raghu
May 11 '07 #3
Have a look at the following code.
This program reads "size" no of strings(of max length 15) that are separated by either space or new line from the file C:\a.txt & prints.

<Removed code as per Posting Guidelines -- MODERATOR>
May 11 '07 #4
AdrianH
1,251 Expert 1GB
If you are still having difficulties, please post the fragment that you are having difficulties with. Or state what it is that you are having problems with.

We at TSDN are not here to code the solution for you, but to help you in understanding the problem you are experiencing.


Adrian
May 11 '07 #5
okay well thanks for the help, but I'm still not getting where I wanted, I'll start a new thread and post my code in there to maybe get some more specific help.
May 12 '07 #6
In my opinion, using multidimensional arrays is always a BAD choice. It is not flexible, it is not easy to read, it suffers from distinction between compilers.

Probably the most convenient solution is use one dimensional array and adding your own dimension management. In this case, I suggest a one dimensional array for storing pointer to string, like this:

vector<string*> my_array;

then you add

vector<int> size;

to manage the dimensional information, say 2*3*5 array, and so on.
May 13 '07 #7
AdrianH
1,251 Expert 1GB
In my opinion, using multidimensional arrays is always a BAD choice. It is not flexible, it is not easy to read, it suffers from distinction between compilers.
I agree with everything you said except the last point regarding distinction between compilers. To what are you referring?
Probably the most convenient solution is use one dimensional array and adding your own dimension management. In this case, I suggest a one dimensional array for storing pointer to string, like this:

vector<string*> my_array;

then you add

vector<int> size;

to manage the dimensional information, say 2*3*5 array, and so on.
What is the vector<int> for?


Adrian
May 13 '07 #8
I agree with everything you said except the last point regarding distinction between compilers. To what are you referring?

What is the vector<int> for?


Adrian
When I started using C++, I remember some code regarding multi-dimensional arrays can not compile in some compiler, but was OK in other compilers. This might not be a problem anymore: )

the vector<int> is used for storing sizes of each dimension. For example, for a 2*2 table, the vector<int> is [2,2], for a 3 * 5 * 9 table, the vector<int> is [3, 5, 9]

In this way, we can create different abstract table from one set of data, e.g. at one time we may think a table is 2*2*2, at the other time we may think it's 2*4, no data copying is required at all.

Further more, by introducing a new vector, which I called an ACU, we can change the abstract dimension order of a table. Say a 2*3 table, the ACU is [3,1], it means we increase 1 to shift the right most dimension, and increase 3 to shift the left most dimension. Now we may want to swap the two dimensions, so we can view the table as a 3*2. In this case we use the new size [3,2] and ACU[1,3], and KEEP THE DATA UNCHANGED.
May 14 '07 #9
AdrianH
1,251 Expert 1GB
When I started using C++, I remember some code regarding multi-dimensional arrays can not compile in some compiler, but was OK in other compilers. This might not be a problem anymore: )
I hope not, this is part of the standard. ;)
the vector<int> is used for storing sizes of each dimension. For example, for a 2*2 table, the vector<int> is [2,2], for a 3 * 5 * 9 table, the vector<int> is [3, 5, 9]

In this way, we can create different abstract table from one set of data, e.g. at one time we may think a table is 2*2*2, at the other time we may think it's 2*4, no data copying is required at all.

Further more, by introducing a new vector, which I called an ACU, we can change the abstract dimension order of a table. Say a 2*3 table, the ACU is [3,1], it means we increase 1 to shift the right most dimension, and increase 3 to shift the left most dimension. Now we may want to swap the two dimensions, so we can view the table as a 3*2. In this case we use the new size [3,2] and ACU[1,3], and KEEP THE DATA UNCHANGED.
Interesting, but I must ask. Why would one want to change the array's dimentions while keeping the array's current representation?


Adrian
May 14 '07 #10
I hope not, this is part of the standard. ;)
Interesting, but I must ask. Why would one want to change the array's dimentions while keeping the array's current representation?


Adrian
ha, this actually happens in my one of my graduate program, where we must handle the product of a bunch of probability tables, such as

P(A, B, C) P(D, C) P(D, A) P(B, E), each table is large, and when you want to multiply them together, you must specify an order, such as A, B, C, D, E to ensure the result is right. Sometimes the variable order in different tables contradicts, such as P(A, B)P(C, B, A), so the variable ordering must be changed.
May 14 '07 #11
AdrianH
1,251 Expert 1GB
ha, this actually happens in my one of my graduate program, where we must handle the product of a bunch of probability tables, such as

P(A, B, C) P(D, C) P(D, A) P(B, E), each table is large, and when you want to multiply them together, you must specify an order, such as A, B, C, D, E to ensure the result is right. Sometimes the variable order in different tables contradicts, such as P(A, B)P(C, B, A), so the variable ordering must be changed.
I understand, but how are you able to process the data appropriately without the use of an adapter but by simply saying that the array should be interpreted using some arbitrary dimensions? You would have to be very careful not to use inappropriate dimensions lest the elements accessed though the dimensions not make much sense.


Adrian
May 14 '07 #12

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

Similar topics

5
by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would...
2
by: Terry | last post by:
Hi, can someone plz tell me how multidimensional arrays (like a 2-D array) are stored in memory? Are they like single dimensional arrays? Stored sequentially in one "row", so to say? Thanks ...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
3
by: Claire | last post by:
I have a multidimensional array defined as private double myArray = new double; The first column of the array contains X values, the other contains Y values I have a charting function defined as...
3
by: Ravi Singh (UCSD) | last post by:
Hello all I am trying to use jagged and multi-dimensional arrays in C++. In C# these work fine // for jagged arrays string jaggedArray = new string ; //for multidimensional arrays string...
21
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
2
by: oopsatwork | last post by:
Ok...so, I have been outside of the C world for a _very_ long time...but not so long as to remember how to do multidimensional arrays. So, let me state that I know how to malloc pointers to...
12
by: filippo nanni | last post by:
Hello everybody, my question is this: I have two multidimensional arrays and I have to create a third one (for later use) from comparing these two. Here is my example code: //BEGIN CODE var...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.