DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Searching a 2D Array of Strings

1. Registered User
Join Date
Nov 2006
Posts
4

## Searching a 2D Array of Strings

Hi, I need to search a String array, 41x6 to be exact. What I am trying to do is have a user enter in a String, Either a Name of Number that's just looked at as a string, No Math involved. Once a Match is Found for that String in the Array I need it to Display the Entire Row that the String was Found in.

Basically the Purpose is to Obtain Values from a Table out of the Back of my Thermodynamics Books. If your eally Care it's a Table of Critical Constants.

I was Thinking about doing a while loop that cycled through until it found a matching string and then displaying at the counter for that array.
Any Help or INput would be great. Please note I've only taken 2 C++ Courses and we never did anything more complex than functions and 3D Arrays.

2. Senior Member
Join Date
Dec 2003
Posts
3,366
If the table is small, computers are so fast that you can easily search iteratively (just go through the entire table until you find a match, one by one). (This is what you proposed, and its perfectly fine for the scope of the problem described)

If the table is moderately sized and the above search seems to be too slow, this is a prime candidate for a hash table(because the data is fixed, you can make a perfect hash for it with some effort). This is a complicated approach but gives you 1 or try for almost any search, so its uber fast!

If you do not care for the hash table, sorting the table and doing a binary search will return very quickly. This algorithm does lg(table size) iterations, which means for a 1 million sized table, you can pop back in 5 or 6 tries, and you only have to sort once, or enter the table in sorted order. This is a moderate amount of work -- sorting is part of C++ so you would just need a binary search, which is not very hard (cut in half, which half is it in? until you only have 1 item to look at)

3. Senior Member
Join Date
Dec 2003
Posts
3,366
Oh, and it sounds like you can treat this as a 1-d table, beacuse you have 1-d of "keys" and the "other dimension" is the values you wanted to print ? is this correct? for exmaple

key data data data
key data data data

where key is what you are searching for?

that makes it a one-D problem and makes it a simple piece of code.

4. Registered User
Join Date
Nov 2006
Posts
4
jonnin That is not what I am going for I'm looking at this

#include <iostream>
#include <string>

using namespace std;
using std::string;
std::string Table[33][6], x;
int i,j,k;

int main()
{
string Table[33][6] =
{{"Ammonia", "NH3", "17.031", "405.5", "11.35", "0.00426"},
{"Argon", "Ar", "39.948", "150.8", "4.87", "0.00188"},
{"Bromine", "Br2", "159.808", "588", "10.30", "0.000796"},
{"Carbon dioxide", "CO2", "44.01", "304.1", "7.38", "00212"},
{"Carbon monoxide", "CO", "28.01", "132.9", "3.50", "0.00333"},
{"Chlorine", "Cl2", "70.906", "416.9", "7.98", "0.00175"},
{"Fluorine", "F2", "37.997", "144.3", "5.22", "0.00174"},
{"Helium", "He", "4.003", "5.19", "0.227", "0.0143"},
{"Hydrogen", "H2", "2.016", "33.2", "1.30", "0.0323"},
{"Krypton", "Kr", "83.80", "209.4", "5.50", "0.00109"},
{"Neon", "Ne", "20.183", "44.4", "2.76", "0.00206"},
{"Nitric oxide", "NO", "30.006", "180", "6.48", "0.00192"},
{"Nitrogen", "N2", "28.013", "126.2", "3.39", "0.0032"},
{"Nitrogen Dioxide", "NO2", "46.006", "431", "10.1", "0.00365"},
{"Nitrous oxide", "N2O", "44.013", "309.6", "7.24", "0.00221"},
{"Oxygen", "O2", "31.999", "154.6", "5.04", "0.00229"},
{"Sulfur dioxide", "SO2", "64.063", "430.8", "7.88", "0.00191"},
{"Water", "H2O", "18.015", "647.3", "22.12", "0.00317"},
{"Xenon", "Xe", "131.30", "289.7", "5.84", "0.000902"},
{"Acetylene", "C2H2", "26.038", "308.3", "6.14", "0.00433"},
{"Benzene", "C6H6", "78.114", "562.2", "4.89", "0.00332"},
{"n-Butane", "C4H10", "58.124", "425.2", "3.80", "0.00439"},
{"Ethane", "C2H6", "30.070", "305.4", "4.88", "0.00493"},
{"Ethyl alcohol", "C2H5OH", "46.069", "513.9", "6.14", "0.00363"},
{"Ethylene", "C2H4", "28.054", "282.4", "5.04", "0.00465"},
{"n-Heptane", "C7H16", "100.205", "540.3", "2.74", "0.00431"},
{"n-Hexane", "C6H14", "86.178", "507.5", "3.01", "0.00429"},
{"Methane", "CH4", "16.043", "190.4", "4.60", "0.00615"},
{"Methyl alcohol", "CH3OH", "32.042", "512.6", "8.09", "0.00368"},
{"n-Octane", "C8H18", "114.232", "568.8", "2.49", "0.00431"},
{"n-Pentane", "C5H12", "72.151", "469.7", "3.37", "0.00421"},
{"Propane", "C3H8", "44.094", "369.8", "4.25", "0.00454"},
{"Propene", "C3H6", "42.081", "364.9", "4.60", "0.00430"}};

cout<<"Enter a Critical Constant or Substance:"<<endl;
cin>>x;
j=0;
i=0;
if (x != Table[i][j])
{
j++;
if (j == 5)
{
i++;
}
}

cout <<"--Substance-----Formula----Molec. Mass----Temp(K)--"
<<"--Press(MPa)----Vol(m^3/kg)--"<<endl;

cout<<i<<endl;
cout<<"--"<<Table[i][0]<<" "<<Table[i][1]<<" "
<<Table[i][2]<<" "<<Table[i][3]<<" "
<<Table[i][4]<<" "<<Table[i][5]<<endl;
return 0;
}

I'm trying to do this. Although My Counter Doesn't seem to Cycle Through I always get stuck on 0 or 1. PLease Help

5. Registered User
Join Date
Nov 2006
Posts
4
Oh Yeah I tested this Method with this So I Figured It would Work But it Doesn't

#include <iostream>
#include <string>

using namespace std;
std::string list[2][2], x;
int i,j,k;

int main()
{

//int list[2][2] = {{2,3},{4,5}};
string list[2][2]= {{"2","3"},{"4","5"}};
cout<<"Enter a Value"<<endl;
cin>>x;
i=0;
j=0;
while (x != list[i][j])
{
j++;
if (j == 1)
{
i++;
}
}
cout <<"Substance"<<"Formaula"<<"Molec. Mass"<<"Temp(K)"
<<"Press(MPa)"<<"Vol(m^3/kg)"<<endl;
//cout << "Hello World, this is CodeWarrior!" << endl;
k=0;
while (k <= 1)
{
cout<<list[i][k];
k++;
}
return 0;
}
This Works

6. Registered User
Join Date
Nov 2006
Posts
4
And Now I Got it to Work, Atleast Partly in a very Simplistic, non spell checking or Caps interchage. SOrry for allt he Posts

7. I would use a map here, where the first string serves as the key and the next five strings are packed in an object which is the associated value. A hash map would probably make the code more efficient but I don't think you'd notice any performance gain with such a small number of keys.

8. Registered User
Join Date
Jan 2005
Location
UK
Posts
604
Hi,
I think for this particular problem a map is not the best solution, because Modulerics doesn't only want to look for one key, but for other values as well. Probably the easiest (only in this small case!) would be to save the data in a vector of comma separated strings and than just loop through these strings and "find" the substrings.
Code:
```vector<string> vals;
vals.push_back("Ammonia,NH3,17.031,405.5,11.35,0.00426");
vals.push_back("Argon,Ar,39.948,150.8,4.87,0.00188");
...
size_t pos = -1;
for(vector<string>::iterator it=vals.begin(); it != vals.end(); it++)
{
pos = it->find(<your searchvalue>);
if(-1 != pos)
{
// (*it) is your string. just split it up to find individual values if you need them
}
}```
Hope that helps,

D

9. Yeah, this approach could work too. But how about a map<string, vector<string> >?
Besides, I think that the design and performance of this program can be drmatically improved by replacing the strings with numeric values. The first two strings can be replaced with enumerations, or at least the second one can. The rest of the values can be packed as an array of double[4].

10. On a second thought, Dieter, your idea is much better. A vector of one long string tokenized by commas is much simpler to handle. Thumbs up!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise
 Questions? Contact us. C++ Web Development Wireless Latest Tips Open Source