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.
11 2316
Hi,
Check this thread , it will help u.
Also search this forum .
Thanks
Raghuram
HI,
sorry i missed the link
Raghu
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>
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
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.
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.
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
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.
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
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |