Hi, I'm having trouble getting my program to input the values i need.
I have a list with 543 rows and 11 items in each row
ex.
1 4 6 4 6 3 1 3 7 6 4
1 4 6 4 2 4 6 7 8 9 9
3 4 5 6 1 2 6 8 9 5 3
For one of my arrays i need only the first column, so for that ex. i need to read 1 1 3 to use later in my program.
I have tried using loops, but I can only get it to read across the rows.
I understand that I need to use a loop to get the values, but I don't know exactly what kind of loop and how to set it up.
Any Help would be great!
Reading Certain values from a filehow to read the first value only in a row
Page 1 of 1
7 Replies - 386 Views - Last Post: 01 December 2009 - 12:19 AM
Replies To: Reading Certain values from a file
#2
Re: Reading Certain values from a file
Posted 30 November 2009 - 11:05 PM
This isn't the most efficient way of doing the task, but since there will always be 11 values in each row you can fetch the value that you need and then perform a dummy fetch 10 times after that. Put that in a loop and you will get the first column of values.
#3
Re: Reading Certain values from a file
Posted 30 November 2009 - 11:11 PM
&rew, on 30 Nov, 2009 - 10:05 PM, said:
This isn't the most efficient way of doing the task, but since there will always be 11 values in each row you can fetch the value that you need and then perform a dummy fetch 10 times after that. Put that in a loop and you will get the first column of values.
Sorry, I'm very new to this stuff, but how do you perform a dummy fetch?
#4
Re: Reading Certain values from a file
Posted 30 November 2009 - 11:21 PM
Why not feed the data into a two dimensional array and read the columns that way.
Are the numbers delimited someway? Spaces / Commas / Semicolons.
Are the numbers delimited someway? Spaces / Commas / Semicolons.
#5
Re: Reading Certain values from a file
Posted 30 November 2009 - 11:24 PM
DeCompile, on 30 Nov, 2009 - 10:21 PM, said:
Why not feed the data into a two dimensional array and read the columns that way.
Are the numbers delimited someway? Spaces / Commas / Semicolons.
Are the numbers delimited someway? Spaces / Commas / Semicolons.
I don't think I can use a two dimensional array, because I need each column in a parallel array, one array for each column.
This is my list ( http://lib.stat.cmu....ts/CPS_85_Wages )
#6
Re: Reading Certain values from a file
Posted 30 November 2009 - 11:59 PM
why not? read the info into a 2 dimentional array first, then do your stuff -- it will make your life easier later on
this code is directed towards your example file
this code is directed towards your example file
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
//load elements in multi-dimentional array first
int grid [3][11];
ifstream in("data.txt");
for(int h=0; h<3; h++)
{
for(int w=0; w<11; w++)
{
in>>grid[h][w];
}
}
// Then get first Column
int *first = new int[3];
for(int i=0; i<3; i++)
first[i] = grid[i][0];
delete []first;
cin.ignore();
cin.get();
return 0;
}
This post has been edited by ImaSexy: 01 December 2009 - 12:00 AM
#7
Re: Reading Certain values from a file
Posted 01 December 2009 - 12:07 AM
ImaSexy, on 30 Nov, 2009 - 10:59 PM, said:
why not? read the info into a 2 dimentional array first, then do your stuff -- it will make your life easier later on
this code is directed towards your example file
this code is directed towards your example file
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
//load elements in multi-dimentional array first
int grid [3][11];
ifstream in("data.txt");
for(int h=0; h<3; h++)
{
for(int w=0; w<11; w++)
{
in>>grid[h][w];
}
}
// Then get first Column
int *first = new int[3];
for(int i=0; i<3; i++)
first[i] = grid[i][0];
delete []first;
cin.ignore();
cin.get();
return 0;
}
That helps a lot! thank you, but i didnt quite understand what the delete[] first was about? Also, in my list one column has decimals, i can just make the array a float right?
#8
Re: Reading Certain values from a file
Posted 01 December 2009 - 12:19 AM
or a double, but yeah of course
this allocates memory for a pointer to an int array
and you have to delete to prevent memory leakage
you could simply not use a pointer and just put
this allocates memory for a pointer to an int array
int *first = new int[3];
and you have to delete to prevent memory leakage
delete []first;
you could simply not use a pointer and just put
double first[3];
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|