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!!!
7 2162
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
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???
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
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: - #include <vector>
-
typedef std:vector<std.vector<int> > 2Dvector;
-
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
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
alternatively, you can construct the vector as: - std:vector<std:vector<int> > myArray(cols,std:vector<int>(rows));
and just fill it in your for loops using the index [][] operator.
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.
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:
That is an array of 5 elements each of which is an int.
won't compile. You need to declare the number of elements.
Second, this array:
is still an array of 5 elements. Each element is an array of 10 int.
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.
won't compile. You need to declare the number of elements.
Third, the name of an array is the address of element 0
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*.
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: -
int array[5][10];
-
-
int (*ptr)[10] = array;
-
Fourth, when the number of elements is not known at compile time, you create the array dynamically: -
int* array = new int[value];
-
int (*ptr)[10] = new int[value][10];
-
int (*ptr)[10][15] = new int[value][10][15];
-
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: -
int** ptr = new int[value][10]; //ERROR
-
new returns the address of an array of 10 int and that isn't the same as an int**.
Likewise: -
int*** ptr = new int[value][10][15]; //ERROR
-
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: -
int array[10] = {0,1,2,3,4,5,6,7,8,9};
-
has a memory layout of
0 1 2 3 4 5 6 7 8 9
Wheras this array: -
int array[5][2] = {0,1,2,3,4,5,6,7,8,9};
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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;
}
|
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...
|
by: infobahn |
last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
|
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...
|
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...
|
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...
|
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
|
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...
|
by: Yevgen Muntyan |
last post by:
Hey,
Consider the following code:
void func (void)
{
}
void func2 (void)
{
|
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...
|
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: 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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |