help for save many data into array

Pages: 12
@lastchance, I think I'm applying to myself what I suggested the OP doing: follow your advice ;-)
Hi @Enoizat, please don't suggest to the OP that he follows my advice ... I've absolutely no idea what he wants any longer!!!
@lastchance whoaa what this is all about, you seem so mad at me. haha cool man.
you did such a great help, but the way you told me like that make me sad.
I know i'm not native speaker and i know i'm very bad at this c++ program and thats why i ask many question.
if you feel irritating you can delete your post.
i just think did i can save the three column data into multidimentional array like
three column data=array[i][j]
so later i can manipulate them.

Last edited on
@Ryoucplusplus,

Please try to answer the following questions accurately:

(1) How many ROWS are there in your data file?

(2) How many COLUMNS are there in your data file?

(3) Precisely WHICH of those columns do you NEED, and what quantities are in each of those columns?

(4) You want some data as array[i][j]. OK,
(i) what does the [ i ] index refer to?
(ii) what does the [ j] index refer to?
(iii) what data is to be stored in each element of the actual "array"? Is it a single number (like HEIGHT) or a struct of different numbers (i.e. a composite type, like ITEM)?

(5) You have created a number of different threads, each with a different format of data file. WHICH of these data sets do you wish us to consider? Please do not change this data set again afterwards.

(6) If, say, your latitude column repeats after a certain number of rows, please give us enough data to see at least one repeat.

(7) When you write "save the three-column data" what do you mean by "save"? @repeater has already asked you this on a different thread, but you didn't give an answer. Do you mean "save to disk in a new file", "store in some array variable", or something else.


You have been asked on your StackOverflow post to write a clear question. We are asking you to do the same. It is not possible to give a good answer if you keep changing the requirements or if your requirements are unclear or don't make logical sense.

Are you able to post the original statement (not your rewording) of your assignment/project topic/whatever?

Here, for reference, are all your threads on this topic:
http://www.cplusplus.com/forum/beginner/223549/#msg1024216
http://www.cplusplus.com/forum/beginner/223589/#msg1024347
http://www.cplusplus.com/forum/beginner/223585/#msg1024330
http://www.cplusplus.com/forum/beginner/223602/#msg1024369
http://www.cplusplus.com/forum/beginner/223335/#msg1023450
Last edited on
i still hope that you help me after i answer your question
(1). 36.252 rows
(2). 17 columns
(3). the first three column //(5)actually i have another different data so that why i ask many Q
(4). for example
(i). array[0][0] is the first row and first column
(ii). array[0][1] is first row and second column // or i can say the i j is the size of the array
(iii). i'm not sure i can understand this Q
(5). for now i need data in this threads
(6). i dont have it right now in this data and i want to make it.
(7). sorry because lack of language i just know the word "save" i'm not thinking about word like store and other because program language imply other meaning for me.
yes the thing that i want is "store the data into array."

i just make a mess in stackoverflow so i deleted it and hope someone from this forum help me.
is there any question to make this clear?
Thank-you @Ryoucplus.

Your answer to Q4 claims that
array[i][j]
is the data from the jth column of the ith row; i.e. for the ith row,
array[i][0] is LATITUDE, array[i][1] is LONGITUDE, array[i][2] is HEIGHT
and i goes from 0 to 36251. i.e. you have a 36252 x 3 array. (Fundamentally, this is a 1-d array with 3 data elements at each position).

This is fine, except for two things:
- You already have this information in the previous examples of code and if it were correct there would be no need for anything new.
With vector<Item> v you already have precisely the same information (plus, optionally, a lot more)
v[i].latitude is LATITUDE, v[i].longitude is LONGITUDE, v[i].height is HEIGHT

- One of your other posts (reading hard between the lines) suggests that you wish to do interpolation - probably of HEIGHT across a latitude/longitude grid. In that case, I would expect array[i][j] to contain the HEIGHT at the ith latitude and jth longitude values. This is NOT what you have specified in your answers, nor does it correspond to the datasets that you have included in THIS thread. It is simply not how your data is laid out in THIS thread. However, it is ALMOST how data was laid out in one of your different threads:
http://www.cplusplus.com/forum/beginner/223585/#msg1024330
except that there you had two extra columns at the front. Most importantly, you can see a sequence of EQUAL LONGITUDE VALUES in that thread - laid out nicely for subsequent interpolation if there is a constant repeat length for LATITUDE.


So, I'm trying not to "make you sad". I'm asking you to go back, look at my questions again, and above all, supply us with a dataset that is consistent and fixed and corresponds with what you are seeking to do.

Moreover, I really suspect that
array[i][j] contains the HEIGHT at the ith LATITUDE and jth LONGITUDE of a geometrically regular array ... NOT the jth column of the ith row as you have stated. I wish you to go back and rethink this.

Supply us with a correct data set.
Have another look at what you mean by array[i][j].
@Ryoucplus

I'm going to hazard a GUESS at what you might want.

First the input file (data.txt). Note that I only need the first three columns (for which I've made up some data). The later columns are ignored.

VERY IMPORTANT: see how the data file is laid out. There are a set of varying latitudes for each constant longitude (watch the first two columns). This is a bit like you used in
http://www.cplusplus.com/forum/beginner/223585/#msg1024330
Here, I have used 5 different latitudes and 4 different longitudes, making 20 rows in all. Your numbers are much bigger.

You would have to put your data file in this format - and know the number of different latitude and longitude values - if you wanted to use this.

 601      310      10      junk    moreJunk
 602      310      20      junk    moreJunk
 603      310      30      junk    moreJunk
 604      310      40      junk    moreJunk
 605      310      50      junk    moreJunk
 601      320      20      junk    moreJunk
 602      320      40      junk    moreJunk
 603      320      60      junk    moreJunk
 604      320      80      junk    moreJunk
 605      320      100     junk    moreJunk
 601      330      30      junk    moreJunk
 602      330      60      junk    moreJunk
 603      330      90      junk    moreJunk
 604      330      120     junk    moreJunk
 605      330      150     junk    moreJunk
 601      340      40      junk    moreJunk
 602      340      80      junk    moreJunk
 603      340      120     junk    moreJunk
 604      340      160     junk    moreJunk
 605      340      200     junk    moreJunk


After the code is run the third-column information is stored in a double array (actually using vectors)
A[i][j]
but I've hedged my bets by storing everything in a vector<Item> first.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;


struct Item                                                         // Will hold any important things from one row
{
   double latitude;
   double longitude;
   double data;
};


istream &operator >> ( istream& strm, Item &e )                     // Extract operator >> for an Item
{ 
   string line;

   getline( strm, line );                                           // Read the whole line into a single string first
   stringstream( line ) >> e.latitude >> e.longitude >> e.data;     // TAILOR TO YOUR REQUIREMENTS (currently: first three columns)

// int col1, col2
// stringstream( line ) >> col1 >> col2 >> e.latitude >> e.longitude >> e.data;           // Alternative if two unused columns in front

   return strm;
}


ostream &operator << ( ostream& strm, Item e  )                      // Insert operator << for an Item
{
   # define SP << '\t' << setw( 15 ) << setprecision( 8 ) <<
   strm SP e.latitude SP e.longitude SP e.data;
   return strm;
}


void readData( string filename, vector<Item> &V );



//============================================================


int main()
{
   int i, j;

   // Get original data
   vector<Item> V;
   readData( "data.txt", V );
   cout << "\nNumber of data items read = " << V.size() << '\n';
   for ( Item e : V ) cout << e << '\n';                   // Check (only if small dataset!)
  

   // Distribute to appropriate 1-d arrays
   // Determine sizes
   int imax, jmax;
   cout << "How many latitude  values? ";   cin >> imax;
   cout << "How many longitude values? ";   cin >> jmax;
   if ( imax * jmax != V.size() )
   {
      cout << "imax times jmax not equal to data size; think again.";
      exit( 1 );
   }

   vector<double> lat( imax );                                       // 1-d array indexed as lat[i]
   vector<double> lon( jmax );                                       // 1-d array indexed as lon[j]
   vector<vector<double>> A( imax, vector<double>( jmax ) );         // 2-d array indexed as A[i][j]

   // Distribute items - WARNING: ASSUMES LATITUDE CHANGES FASTEST - THIS IS A FORTRAN CONVENTION, NOT C++
   int n = 0;
   for ( j = 0; j < jmax; j++ )
   {
      lon[j] = V[n].longitude;
      for ( i = 0; i < imax; i++ ) 
      {
         lat[i] = V[n].latitude;       // Slightly wastefully overwrites each time
         A[i][j] = V[n].data;
         n++;
      }
   }


   // Basic checks
   cout << "\nLatitude values:\n";
   for ( i = 0; i < imax; i++ ) cout << i << " " << lat[i] << '\n';

   cout << "\nLongitude values:\n";
   for ( j = 0; j < jmax; j++ ) cout << j << " " << lon[j] << '\n';

   while ( true )
   {
      cout << '\n';
      cout << "Input i (0 - " << imax - 1 << ") or negative to finish: ";   cin >> i;
      cout << "Input j (0 - " << jmax - 1 << ") or negative to finish: ";   cin >> j;
      if ( i < 0 || j < 0 ) exit( 0 );
      cout << "Latitude: " << lat[i] << "     Longitude: " << lon[j] << "     Data: " << A[i][j] << '\n';
   }
}


//============================================================


void readData( string filename, vector<Item> &V )
{
   Item thing;
   fstream in( filename );
   while ( in >> thing ) V.push_back( thing );
   in.close();
}


//============================================================  


Number of data items read = 20
	            601	            310	             10
	            602	            310	             20
	            603	            310	             30
	            604	            310	             40
	            605	            310	             50
	            601	            320	             20
	            602	            320	             40
	            603	            320	             60
	            604	            320	             80
	            605	            320	            100
	            601	            330	             30
	            602	            330	             60
	            603	            330	             90
	            604	            330	            120
	            605	            330	            150
	            601	            340	             40
	            602	            340	             80
	            603	            340	            120
	            604	            340	            160
	            605	            340	            200
How many latitude  values? 5
How many longitude values? 4
Latitude values:
0 601
1 602
2 603
3 604
4 605

Longitude values:
0 310
1 320
2 330
3 340

Input i (0 - 4) or negative to finish: 2
Input j (0 - 3) or negative to finish: 3
Latitude: 603     Longitude: 340     Data: 120

...


@lastchance Ok perfect, thank you very much despite my question has has made you confused you still reply, you are truly programer.
Topic archived. No new replies allowed.
Pages: 12