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

void function that read a file into a 2-dimensinal array

hi, I need help in creating a void function that read from a file into a two dimensional array. I'm ok when I create the function declaration but it gets confusing when I need to put the actual read statement. I don't know if I have to declare a variable for each column or if by setting the array it does it automatically.

PLEASE HELP!!!
Oct 23 '07 #1
7 2162
gpraghuram
1,275 Expert 1GB
hi, I need help in creating a void function that read from a file into a two dimensional array. I'm ok when I create the function declaration but it gets confusing when I need to put the actual read statement. I don't know if I have to declare a variable for each column or if by setting the array it does it automatically.

PLEASE HELP!!!
If u are using an arry then u need not declare a variable for every element.
Use a common elemnt to read values from the file and store the value to the array by incrementing the index for every read.

Raghuram
Oct 23 '07 #2
thanks for helping Raghuram, I initialized the array with for loop that incremented the index but I don't know what should I put in the actual input statement here is the incomplete void function because I stuck :( !!!

Expand|Select|Wrap|Line Numbers
  1. void getData (ifstream& inData, int temp[nOfRows][nOfColumns], const int nOfRows)
  2. {
  3.      int r, c;
  4.      int high, low;
  5.  
  6.  
  7.      for (r = 0; r < nOfRows; r++)
  8.          for (c = 0; c < nOfColumns; c++)
  9.  
  10.  
  11. }
any ideas???
Oct 23 '07 #3
gpraghuram
1,275 Expert 1GB
thanks for helping Raghuram, I initialized the array with for loop that incremented the index but I don't know what should I put in the actual input statement here is the incomplete void function because I stuck :( !!!

void getData (ifstream& inData, int temp[nOfRows][nOfColumns], const int nOfRows)
{
int r, c;
int high, low;


for (r = 0; r < nOfRows; r++)
for (c = 0; c < nOfColumns; c++)


}

any ideas???
I was not suggesting the way you have replied.
First you shuld get a fair idea about how to read from a file using the stdio file operations.
Then you can get an idea about how to go about this an u can understand the idea explained by me or you can get a better idea


Raghuram
Oct 23 '07 #4
hi, I need help in creating a void function that read from a file into a two dimensional array. I'm ok when I create the function declaration but it gets confusing when I need to put the actual read statement. I don't know if I have to declare a variable for each column or if by setting the array it does it automatically.

PLEASE HELP!!!
Hi Mathanne,
It is not clear for me what you want to acheive exactly, but let me try to give you a few points:
If programming in c++, you should use a <vector> instead of an array, it is a much more easy-to-use and powerful container. You could define it as:
Expand|Select|Wrap|Line Numbers
  1. #include <vector>
  2. typedef std:vector<std.vector<int> > 2Dvector;
  3. 2Dvector myArray;
provided that it is int you want to store in your vector.

<The rest of this post was providing 'spoonfed code', which violates our Posting Guidelines, and has been removed. MODERATOR>

I am assuming that your file looks something like this:
1 5 21 23 17
4 5 6 12 12
4 95 23 23 43
87 1 2 3 51

/m
Oct 23 '07 #5
Cool enough,
I will abstain from spoonfed code.
Mathanna,
you open the file as a std:ifstream ; then you can read the file using the >> operator, and fill the 2Dvector using
Expand|Select|Wrap|Line Numbers
  1. myArray.push_back();
alternatively, you can construct the vector as:
Expand|Select|Wrap|Line Numbers
  1. std:vector<std:vector<int> > myArray(cols,std:vector<int>(rows));
and just fill it in your for loops using the index [][] operator.
Oct 23 '07 #6
Thanks for the ideas, but I can't use them since we haven't started to use vectors in programs. The exercise just says to write a function that reads and stores data in a 2-dimensional array.

Mathanne.
Oct 23 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
The exercise just says to write a function that reads and stores data in a 2-dimensional array.
Read this:
First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
That is an array of 5 elements each of which is an int.

Expand|Select|Wrap|Line Numbers
  1. int array[];
  2.  
won't compile. You need to declare the number of elements.

Second, this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
is still an array of 5 elements. Each element is an array of 10 int.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10][15];
  2.  
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


Expand|Select|Wrap|Line Numbers
  1. int array[][10];
  2.  
won't compile. You need to declare the number of elements.

Third, the name of an array is the address of element 0
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
  3. int (*ptr)[10] = array;
  4.  
Fourth, when the number of elements is not known at compile time, you create the array dynamically:

Expand|Select|Wrap|Line Numbers
  1. int* array = new int[value];
  2. int (*ptr)[10] = new int[value][10];
  3. int (*ptr)[10][15] = new int[value][10][15];
  4.  
In each case value is the number of elements. Any other brackets only describe the elements.

Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

Expand|Select|Wrap|Line Numbers
  1. int** ptr = new int[value][10];    //ERROR
  2.  
new returns the address of an array of 10 int and that isn't the same as an int**.

Likewise:
Expand|Select|Wrap|Line Numbers
  1. int*** ptr = new int[value][10][15];    //ERROR
  2.  
new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.
With the above in mind this array:
Expand|Select|Wrap|Line Numbers
  1. int array[10] = {0,1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of

0 1 2 3 4 5 6 7 8 9

Wheras this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][2] = {0,1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of

0 1 2 3 4 5 6 7 8 9

Kinda the same, right?

So if your disc file contains

0 1 2 3 4 5 6 7 8 9

Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.

Therefore, when you do your read use the address of array[0][0] and read as though you have a one-dimensional array and the values will be in the correct locations.
Oct 23 '07 #8

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

Similar topics

192
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty...
27
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
5
by: verec | last post by:
I just do not understand this error. Am I misusing dynamic_cast ? What I want to do is to have a single template construct (with no optional argument) so that it works for whatever T I want to...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
3
by: Don | last post by:
I am a beginner in C. A student I should say. Is it possible to retrieve a value from a void function? A have a void function that reads a string or number from a file. I call that function from...
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
56
by: maadhuu | last post by:
hello, this is a piece of code ,which is giving an error. #include<stdio.h> int main() { int a =10; void *p = &a; printf("%d ", *p ); //error....why should it //be an error ?can't the...
11
by: Roman Mashak | last post by:
Hello, All! I'm implementing function parsing config file, which look like this: # this is comment port=10000 path=/var/run/dump.pid .... Declared the type
49
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
28
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: void func (void) { } void func2 (void) {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.