I have a problem where I need to be able to create an array using the data from the text file. The first line of the file is the array size and the following values need to be put into structs. There may be more than one array per txt file.

The data file looks like this:
3 3
90 35d43m9s45d45m19s8 36d46m19s51d25m18s45 36d46m19s11d25m39s
7 35 23 9 25 43 9 25 25 43 9 35 33 39 30 32 33 9 17 33 22
6 15 43 19 25 43 29 108 39 33 40 25 23 29 565 25 23 29 15 41 19
4 4
100 15 43 19 25 43 29 5 39 33 40 25 23 29 6 25 23 29 15 41 19 90 32 33 9 17 33 22
8 35 43 9 45 45 19 7 36 46 19 51 25 19 10 36 46 19 11 25 39 5 22 46 39 12 25 29
80 35 23 9 25 43 9 7 25 43 9 35 33 39 6 32 33 9 17 33 22 2 39 38 29 37 23 12
9 12 53 49 18 33 22 8 44 33 9 22 33 22 97 38 38 9 47 36 26 5 52 53 9 57 53 22

The d m & s should be in the file as shown in the frst line.

I am new to C++ and this problem is kicking my butt. Any help would be great

Hi and welcome to Daniweb.

I get lost from your first line of data file.
You say it should contain the size of the array?
But there is two numbers? Is it a two-dimensional array?

And I'm a bit confused - you need to put data into structs, but also put them into arrays?

I don't see how your values would fit into a two-dimensional array, though.

If you clarify what you need better, I sure can and will help you :)

I'd echo with Excizted, I can't make anything out of your example

3 3
90 35d43m9s45d45m19s8 36d46m19s51d25m18s45 36d46m19s11d25m39s
7 35 23 9 25 43 9 25 25 43 9 35 33 39 30 32 33 9 17 33 22
6 15 43 19 25 43 29 108 39 33 40 25 23 29 565 25 23 29 15 41 19

If 3x3 be the array size. there's no way the corresponding values can be put into it. They may comprise of 3 rows but not 3 columns. Same goes for 4x4.
Please elaborate.

Okay maybe it will help if I show you the whole problem:


The file below contains multiple arrays. One is a 3 X 3 and the other is a 4 X 4.
Maybe there is no way to do this when multiple array info is in one text file.
The input data below is contained in text file.

An example of input data:
3 3
90 35°43'9" 45°45'19" 8 36°46'19" 51°25'19" 45 36°46'19" 11°25'39"
7 35°23'9" 25°43'9" 25 25°43'9" 35°33'39" 30 32°33'9" 17°33'22"
6 15°43'19" 25°43'29" 108 39°33'40" 25°23'29" 565 25°23'29" 15°41'19"
4 4
100 15°43'19" 25°43'29" 5 39°33'40" 25°23'29" 6 25°23'29" 15°41'19" 90 32°33'9" 17°33'22"
8 35°43'9" 45°45'19" 7 36°46'19" 51°25'19" 10 36°46'19" 11°25'39" 5 22°46'39" 12°25'29"
80 35°23'9" 25°43'9" 7 25°43'9" 35°33'39" 6 32°33'9" 17°33'22" 2 39°38'29" 37°23'12"
9 12°53'49" 18°33'22" 8 44°33'9" 22°33'22" 97 38°38'9" 47°36'26" 5 52°53'9" 57°53'22"

The corresponding output would be:

For the 3 x 3 array:
90 8 45
7 25 30
6 108 565

The local maxima are:
90 35°43'9" 45°45'19" at point 0,0
45 36°46'19" 11°25'39" at point 0,2
565 25°23'29" 15°41'19" at point 2,2

For the 4 x 4 array:
100 5 6 90
8 7 10 5
80 7 6 2
9 8 97 5

The local maxima are:
100 15°43'19" 25°43'29" at point 0,0
90 32°33'9" 17°33'22" at point 0,3
80 35°23'9" 25°43'9" at point 2,0
97 38°38'9" 47°36'26" at point 3,2


Sorry I was so unclear the first time.

I really can't figure out these coordinates or whatever.
You need to find out how to separate each row into columns.

First, by reading in 3 and 3, you would know to create an array with this size.

size1 = 3;
size2 = 3;
std::string str[size1][size2];

Then just fill them in`?

The Geographical Information Service (GIS) division of Google Earth needs to determine the
local maxima elevations for any given map section. A map section is represented as a two
dimensional matrix of records. A record contains points consisting of an integer value for the
elevation of the location and two strings for the corresponding longitudinal and latitudinal
values. Local maxima elevations are larger than neighboring cells, those that touch the cell of
interest. Note: interior cells of the matrix have eight neighbors, edge cells have five neighbors,
and corner cells have only three neighbors.
You must develop a software solution to the above problem that includes analysis, design, and
implementation. Your solution must retrieve the necessary data from a secondary storage file
and use a two dimensional array as your internal representation of a map section. Another
system creates this file and provides it to you. Therefore, the format is fixed and is as follows:
the first line contains the dimensions of the array, number of rows, then number of columns,
needed to represent the map section; and each successive line contains elevation, longitude, and
latitude values for points in the map section. All values are space delimited. The data file can
contain more than one map section and each map section can be a different size. The name of
the input file must be mapdata.dat.
Your solution must print to the screen the elevation, longitudinal value, latitudinal value, and
array position, row then column, of all local maxima for each map section. Please include
appropriate labels to help users better understand the output. Note: This document displays the
latitude and longitude in degree, minute, second format using special characters. To facilitate the
program input and output, you may use non-special characters to designate the latitude or
longitude value as a string. For example, instead of using 15°43'19" 25°43'29" to represent the
longitude and latitude of a given point, you may use 15d43m19s 25d43m29s.
An example of input data:
3 3
90 35°43'9" 45°45'19" 8 36°46'19" 51°25'19" 45 36°46'19" 11°25'39"
7 35°23'9" 25°43'9" 25 25°43'9" 35°33'39" 30 32°33'9" 17°33'22"
6 15°43'19" 25°43'29" 108 39°33'40" 25°23'29" 565 25°23'29" 15°41'19"
4 4
100 15°43'19" 25°43'29" 5 39°33'40" 25°23'29" 6 25°23'29" 15°41'19" 90 32°33'9" 17°33'22"
8 35°43'9" 45°45'19" 7 36°46'19" 51°25'19" 10 36°46'19" 11°25'39" 5 22°46'39" 12°25'29"
80 35°23'9" 25°43'9" 7 25°43'9" 35°33'39" 6 32°33'9" 17°33'22" 2 39°38'29" 37°23'12"
9 12°53'49" 18°33'22" 8 44°33'9" 22°33'22" 97 38°38'9" 47°36'26" 5 52°53'9" 57°53'22"
The corresponding output would be:
For the 3 x 3 array:
90 8 45
7 25 30
6 108 565
The local maxima are:
90 35°43'9" 45°45'19" at point 0,0
45 36°46'19" 11°25'39" at point 0,2
565 25°23'29" 15°41'19" at point 2,2
For the 4 x 4 array:
100 5 6 90
8 7 10 5
80 7 6 2
9 8 97 5
The local maxima are:
100 15°43'19" 25°43'29" at point 0,0
90 32°33'9" 17°33'22" at point 0,3
80 35°23'9" 25°43'9" at point 2,0
97 38°38'9" 47°36'26" at point 3,2


That is everything I have for the problem. I hope maybe this clarifies some of the continued confusioin.

You need something like this:

getline(file,matSize);
stringstream size(matSize);
int row,col;
size>>row;
size>>col;
vector<string> coord;

//Loop the foll until rows
string data;
getline(file,str);
stringstream stream(str);
str>>data;
coord.push_back(data);
//End Loop

//Parse the coordinates.
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void printarray();

ifstream inFile;

int item1;
int row;
int col;

struct coord
{
	int num;
	int deg;
	int min;
	int sec;
	int deg2;
	int min2;
	int sec2;

};


int myarray[999][999];

int main()
{
	inFile.open("mapdata.dat");
	inFile >> row >> col;

	while ( ! inFile.eof() )
	{
		inFile >> item1;
		cout << item1 << endl;
	}

	printarray();
	system("pause");
	inFile.close();
	return 0;
}

void printarray()
{
	int myrow = 0;
	while (myrow < row)
	{
		int mycol = 0;
		while (mycol < col)
		{
			cout << myarray[myrow][mycol];
			mycol = mycol + 1;
		}
		cout << endl;
		myrow = myrow + 1;
	}
}

That is what I have right now but the book I am using does not show me how to put data into structs. Here is the text in the data file I am using:

3 3
90 35 43 9 45 45 19 8 36 46 19 51 25 18 45 36 46 19 11 25 39
7 35 23 9 25 43 9 25 25 43 9 35 33 39 30 32 33 9 17 33 22
6 15 43 19 25 43 29 108 39 33 40 25 23 29 565 25 23 29 15 41 19

Then you need to look into STL and/or Dynamic Allocation.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void elevation();
void getdata();
void maxresult();


struct coord
{
	int elev;
	string lat;
	string lon;
};
coord myarray[999][999];

ifstream inFile;

int item1;
int row;
int col;
int myrow;
int mycol;
int count;
int count2;



int main()
{
	inFile.open("mapdata3.dat");
	inFile >> row >> col;
	getdata();
	elevation();
	system("pause");
	inFile.close();
	return 0;
}

void getdata()
{
	count = 0;
	for (count = 0; count < row; count = count + 1)
	{
		count2 = 0;
		for (count2 = 0; count2 < col; count2 = count2 + 1)
		{
			inFile >> myarray[count][count2].elev >> myarray[count][count2].lat >> myarray[count][count2].lon;
		}
	}
}

void elevation()
{
	myrow = 0;
	for (myrow = 0; myrow < row; myrow = myrow + 1)
	{
		mycol = 0;
		for (mycol = 0; mycol < col; mycol = mycol + 1)
		{
			cout << myarray[myrow][mycol].elev << "\t";
		}
		cout << endl;
	}
}

again the data file looks like this:
3 3
90 35d43m9s 45d45m19s 8 36d46m19s 51d25m18s 45 36d46m19s 11d25m39s
7 35d23m9s 25d43m9s 25 25d43m9s 35d33m39s 30 32d33m9s 17d33m22s
6 15d43m19s 25d43m29s 108 39d33m40s 25d23m29s 565 25d23m29s 15d41m19s

Thanks for all of your help nbaztec. This is what I have now. Any quick suggestions on how to move through my array and get the maxina results as I showed in my example above?

Still working. now i have the maxina being found, captured, tested, and returned but I cant figure out how to get the rest of the data in my struct to print out with the elevation. sorry to post so much but ive only been in the class 4 weeks and im trying but its killing me

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void elevation();
void getdata();
void addMax();
void maxina();

struct coord
{
	int elev;
	int x;
	int y;
	string lat;
	string lon;
};
coord myarray[999][999];

int result[999];

ifstream inFile;

int row;
int col;
int myrow;
int mycol;
int resIndex = 0;


int main()
{
	inFile.open("mapdata3.txt");
	inFile >> row >> col;
	getdata();
	elevation();
	maxina();
	system("pause");
	inFile.close();
	return 0;
}

void addMax(int testagainst)
{
	int maxResult = row * col;
	for (int myrow = 0; myrow < maxResult; myrow = myrow + 1)
	{
		if (myrow == resIndex)
		{
			result[myrow] = testagainst;
			resIndex = resIndex + 1;
			myrow = maxResult;
		} 
		else if (testagainst == result[myrow])
			myrow = maxResult;
	}
}

void getdata()
{
	myrow = 0;
	for (myrow = 0; myrow < row; myrow = myrow + 1)
	{
		mycol = 0;
		for (mycol = 0; mycol < col; mycol = mycol + 1)
		{
			inFile >> myarray[myrow][mycol].elev >> myarray[myrow][mycol].lat >> myarray[myrow][mycol].lon;
			myarray[myrow][mycol].x = myrow;
			myarray[myrow][mycol].y = mycol;
		}
	}
}

void elevation()
{
	myrow = 0;
	for (myrow = 0; myrow < row; myrow = myrow + 1)
	{
		mycol = 0;
		for (mycol = 0; mycol < col; mycol = mycol + 1)
		{
			cout << myarray[myrow][mycol].elev << "\t";
		}
		cout << endl;
	}
}

void maxina()
{
	for (int myrow = 0; myrow < row; myrow = myrow + 1)
	{
		for (int mycol = 0; mycol < col; mycol = mycol + 1)
		{
			int testagainst = myarray[myrow][mycol].elev;
			int left = myrow - 1;  //calculate how far to the left you can go
			int right = myrow + 1; //calculate how far to the right you can go
			int top = mycol - 1;   //calculate how far  up you can go
			int bottom = mycol + 1;//calculate how far down you can go
		       
			//adjust left, right, top, bottom based on your row/column limitations
			if (myrow == 0)
				left = 0;
			if (myrow == (row - 1)) 
				right = myrow;
			if (mycol == 0)
				top = 0;
			if (mycol == (col - 1))
				bottom = mycol;
		       
			//loop from left to right, top to bottom of your 2-D array and find 
			//the highest numerical value
			for (int x = left; x <= right; x = x + 1 )
			{
				for (int y = top; y <= bottom; y = y + 1)
				{
					if (myarray[x][y].elev >= testagainst)
						testagainst = myarray[x][y].elev;
						//tempResult = myarray[myrow][mycol];
				}
			}
			addMax(testagainst);
		}
	}
	for (int myrow = 0; myrow < resIndex; myrow = myrow + 1)
		cout << result[myrow] << endl;
}

Assuming that you only require help in Parsing/Computing the data, I suggest you use a class:
Since your data pretty much has 7 pieces for each Coordinate(90 35d43m9s 45d45m19s).

class Coordinate
{
  public:
    int val1;
    int val2;
    .
    .
};

You can use this in you main:

int main()
{
  Coordinate coord[10];     //As many as you want
  vector<Coordinate> coord; //Or use a vector. Better.
  .
  .
}

To compute maxima you can either overload the >(<) operator or define a function in class:

class Coordinate
{
  public:
    .
    .
    bool operator > (Coordinate &Obj) //And < operator
    {
        if(this->val1 > Obj.val1)
        <More test conditions>
        <return true/false>
    }
    /* 
     * Or you can define a function
     */
    Coordinate GreatestOfAll(Coordinate Objects[], int size)
    {
        <Test All>
        <return Objects[max]>
    }
};

Then you can input a collection fo 7 >> statements to feed data into class or use a switch statement while in a for-loop:

int main()
{
  Coordinate coord[100]; //As many as you want
  .
  .
  //Loop until rows
    //Loop until columns
    File>>coord[i].val1; //Or coord.pus_back(<temp>), if a vector
    File>>coord[i].val2;
    .
    .
    //End Loop
  <Compare Coordinates & Take action>
  //End Loop
}

Hope this is useful.

ok. here is what i did

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void elevation();
void getdata();
void Maxina();
void check();


struct coord
{
	int elev;
	string lat;
	string lon;
	int x;
	int y;
};
coord myarray[999][999];
coord test;
coord result[999];

ifstream inFile;

int item1;
int row;
int col;
int myrow;
int mycol;
int resIndex = 0;


int main()
{
	inFile.open("mapdata.dat");
	while ( ! inFile.eof() )
	{
		inFile >> row >> col;
		getdata();
		elevation();
		Maxina();
	}
	
	system("pause");
	inFile.close();
	return 0;
}

void getdata()
{
	
	myrow = 0;
	for (myrow = 0; myrow < row; myrow = myrow + 1)
	{
		mycol = 0;
		for (mycol = 0; mycol < col; mycol = mycol + 1)
		{
			inFile >> myarray[myrow][mycol].elev >> myarray[myrow][mycol].lat >> myarray[myrow][mycol].lon;
			myarray[myrow][mycol].x = myrow;
			myarray[myrow][mycol].y = mycol;
		}
	}
}

void elevation()
{
	cout << endl << "Elevations for the " << row << " X " << col << " array" << endl;
	myrow = 0;
	for (myrow = 0; myrow < row; myrow = myrow + 1)
	{
		mycol = 0;
		for (mycol = 0; mycol < col; mycol = mycol + 1)
		{
			cout << myarray[myrow][mycol].elev << "\t";
		}
		cout << endl;
	}
}

void Maxina()
{
	cout << endl << "The local maxima are: " << endl;
	int i;
	for (i = 0; i < row; i = i + 1)
	{
		int j;
		for (j = 0; j < col; j = j + 1)
		{
			test = myarray[i][j];
			int left = i - 1;   //calculate how far to the left you can go
			int right = i + 1;  //calculate how far to the right you can go
			int top = j - 1;    //calculate how far  up you can go
			int bottom = j + 1; //calculate how far down you can go

			if (i == 0) 
				left = 0;
		    if (i == (row - 1))
				right = i;
		    if (j == 0) 
				top = 0;
		    if (j == (col - 1)) 
				bottom = j;
			
			int x;
		    for (x = left; x <= right; x = x + 1 )
			{
				int y;
				for (y = top; y <= bottom; y = y + 1)
				{
					if (myarray[x][y].elev > test.elev)
					{
						test = myarray[x][y];
					}
					else
						"do nothing";
				}
			}
			check();
		}
		for (int i = 0; i < resIndex; i = i + 1)
		{
			cout << result[i].elev << " " << result[i].lat << " " << result[i].lon;
			cout << " At point (" << result[i].x << "," << result[i].y << ")" << endl;
		}
	}
}

void check()
{
	int maxResult = row * col;
	for (int i = 0; i < maxResult; i = i + 1)
	{
		if (i == resIndex) 
		{
			result[i] = test;
			resIndex = resIndex + 1;
			i = maxResult;
		} 
		else if (test.elev = result[i].elev)
				i = maxResult;
	}
}

It works for the best part but it will not add my results properly for some reason. maybe i can figure that out soon

>> it will not add my results properly
As in?

I got it working. If you have the time you can download my data file and look to see what I was trying to accomplish the whole time. Thanks for being patient with me and I intend on becoming more clear with what I need help on. Thanks so much

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct coord
{
	int elev;
	string lat;
	string lon;
	int x;
	int y;
};
coord myarray[999][999];
coord test;
coord result[999];

void elevation();
void getdata();
void check(coord test);
void Maxina();
void printMX();
void clear();


ifstream inFile;

int item1;
int row;
int col;
int myrow;
int mycol;
int maxResult;
int resIndex = 0;


int main()
{
	inFile.open("mapdata.dat");
	while ( ! inFile.eof() )
	{
		inFile >> row >> col;
		maxResult = row * col;
		getdata();
		elevation();
		Maxina();
		printMX();
		clear();
	}
	
	system("pause");
	inFile.close();
	return 0;
}

void getdata()
{
	
	myrow = 0;
	for (myrow = 0; myrow < row; myrow = myrow + 1)
	{
		mycol = 0;
		for (mycol = 0; mycol < col; mycol = mycol + 1)
		{
			inFile >> myarray[myrow][mycol].elev >> myarray[myrow][mycol].lat >> myarray[myrow][mycol].lon;
			myarray[myrow][mycol].x = myrow;
			myarray[myrow][mycol].y = mycol;
		}
	}
}

void elevation()
{
	cout << endl << "Elevations for the " << row << " X " << col << " array" << endl;
	myrow = 0;
	for (myrow = 0; myrow < row; myrow = myrow + 1)
	{
		mycol = 0;
		for (mycol = 0; mycol < col; mycol = mycol + 1)
		{
			cout << myarray[myrow][mycol].elev << "\t";
		}
		cout << endl;
	}
}

void Maxina()
{
	coord test = myarray[row][col];
	cout << endl << "The local maxima are: " << endl;
	for (int i = 0; i < row; i = i + 1)
	{
		for (int j = 0; j < col; j = j + 1)
		{
			coord test = myarray[i][j];
			int left = i - 1;   //calculate how far to the left you can go
			int right = i + 1;  //calculate how far to the right you can go
			int top = j - 1;    //calculate how far  up you can go
			int bottom = j + 1; //calculate how far down you can go

			if (i == 0) 
				left = 0;
		    if (i == (row - 1))
				right = i;
		    if (j == 0) 
				top = 0;
		    if (j == (col - 1)) 
				bottom = j;
			
			int x;
		    for (x = left; x <= right; x = x + 1 )
			{
				int y;
				for (y = top; y <= bottom; y = y + 1)
				{
					if (myarray[x][y].elev >= test.elev)
					{
						test = myarray[x][y];
					}
				}
			}
			check(test);
		}
	}
}

void check(coord test)
{
	for (int i = 0; i < maxResult; i = i + 1)
	{
		if (i == resIndex) 
		{
			result[i] = test;
			resIndex = resIndex + 1;
			i = maxResult;
		} 
		else if (test.elev == result[i].elev)
			i = maxResult;
	}
}

void printMX()
{
	for (int i = 0; i < resIndex; i = i + 1)
	{
		cout << result[i].elev << " " << result[i].lat << " " << result[i].lon;
		cout << " At point (" << result[i].x << "," << result[i].y << ")" << endl;
	}
}

void clear()
{
	for (int i = 0; i < resIndex; i = i + 1)
	{
		resIndex = 0;
		result[i].elev = 0;
		result[i].x = 0;
		result[i].y = 0;
		result[i].lat = "";
		result[i].lon = "";
	}	
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.