Solved

# arrays

Posted on 2005-04-15
227 Views
if i have a data file w/ values stored like this:
9 11
3 3
2 13
9 20

i want to put all the values in the first column in array1[] by value: for example if their are three number 6's in the first column i need them stored in array1[5] and all the 4's in array1[3]

then i want to do the same w/ the second column. i need to count how many their are of each value and store them in its respective place.
can someone help me out on how to do this.

0
Question by:Kal130

LVL 30

Expert Comment

Hi Kal130,
> >i want to put all the values in the first column in array1[] by value:
> >for example if their are three number 6's in the first column i need
> >them stored in array1[5] and all the 4's in array1[3]
You can use a map of vectors for this.

#include <map>
#include <vector>
using namespace std;

map<int, vector<int> > MyArray;

MyArray[9-1].push_back(11);
MyArray[3-1].push_back(3);
MyArray[2-1].push_back(13);
MyArray[9-1].push_back(29);

David Maisonave :-)
Cheers!
0

Author Comment

is their a simpler way to do this maybe with counters, cause i have not learned maps and vectors yet
0

LVL 30

Expert Comment

Are you trying to create a 2 dimensional array, with one of the dimensions associated with a specific index?

If not, please explain the question with more details.
0

Author Comment

ok i need to create two 1 dimensional arrays.
ive created 2 arrays array1[10] and array2[20] and initialized them to 0.
i have a datafile and in the data file i have 2 columns of values. in the first column all the numbers are 1-10 and in the second 1-20.
if their are 12 4's in the first column i need to place them in array1[3] so that array will equal 12. then i have to do the same thing for the second column w/ the numbers 1 through 20.

is that more clear? i hope so :)
0

LVL 30

Expert Comment

So do you need help with code for reading the data from a file, or do you need help setting up the array?
0

Author Comment

reading the data from a file into an array
0

LVL 30

Expert Comment

I see you already have posted a previous related question:
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21388312.html

In which you gave a B grade.

It would help if you can show us your existing code, and let us no what was the problem with the previously posted answer, so we do not duplicate our effort.

Also, please give experts a chance to give you an A grade answer, before awarding them a B grade on a 50 point question.
0

Author Comment

i didnt realize i gave him a B, but that previous post didnt really answer my question, it was my fault in part because i did not explain thoroughly what i needed help on
0

LVL 30

Expert Comment

Can you post your current code?
0

Author Comment

#include <iostream>
#include <fstream>

using namespace std;

const int MAX=11;
const int nMAX=21;

int main ()
{
ifstream inFile;
int precinct[MAX], id[nMAX], name[nMAX];

for (int i=0; i<MAX; i++)
{precinct[i]=0;}

for (int j=0; j<nMAX; i++)
{id[j]=0;}

for (int k=0; k<nMAX; k++)
{name[k]=0;}

for(i=0;i<11;++i)

while(!inFile.eof())
{
}
return 0;

}
0

LVL 30

Expert Comment

That's great.  It looks like you have a good start on it.

Now to the specifics of your question:
>>if their are 12 4's in the first column i need to place them in array1[3] so that array will equal 12.

Do you just want to count how many 4's you have, and is that why it would equal to 12?
0

Author Comment

yeah i want to count all the numbers in the file on the first column and put them in the certain array location (array[3] = 12)
0

LVL 30

Expert Comment

You can try something like the following
while(!inFile.eof())
{
int Col1;
inFile >> Col1;
inFile >> id[Col1-1];
}
0

LVL 30

Expert Comment

But if you're just counting it, then you can do something like this:
while(!inFile.eof())
{
int Col1;
inFile >> Col1;
id[Col1-1]++;
int dummy;
inFile >> dummy;
}
0

Author Comment

this would be valid?

for(i=0;i<11;++i)
{
while(!inFile.eof())
{
int Col1;
inFile >> Col1;
precinct[Col1-1]++;
int dummy;
inFile >> dummy;
}

}

(please excuse me, im very new to c++ and its a little overwhelming)
0

LVL 30

Expert Comment

>>this would be valid?

You don't need the for loop because once the while loop reads to the end of the file, it's not going to jump back into the while loop.

0

Author Comment

would it matter if in the data file the values are not in order:

2 20
1 11
6 12
0

LVL 30

Expert Comment

>>would it matter if in the data file the values are not in order:

No, it shouldn't matter.
0

Author Comment

this part compiles but hangs
0

LVL 30

Expert Comment

>>this part compiles but hangs
Did you look at the stack to see where it hangs, and did you look at what values you're getting for Col1?
0

LVL 30

Expert Comment

Can you post exactly what you have in your text file, so I can run a quick test?
0

Author Comment

1 1
2 1
3 4
5 16
3 14
2 13
4 11
8 11
5 20
3 14
8 12
4 15
9 16
2 17
10 18
9 20
10 14
9 11

this is the source code

#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
ifstream inFile;
int precinct[11], id[21], name[21];

for (int i=0; i<11; i++)
{precinct[i]=0;}

for (int j=0; j<21; i++)
{id[j]=0;}

for (int k=0; k<21; k++)
{name[k]=0;}

while(!inFile.eof())
{
int Col1;
inFile >> Col1;
precinct[Col1-1]++;
int dummy;
inFile >> dummy;
}

return 0;

}
0

LVL 30

Expert Comment

You should have the following if condition
if (Col1 > 0 && Col1 < 21)
{
precinct[Col1-1]++;
}
0

LVL 30

Expert Comment

The following for loop had the wrong increment variable:
for (int j=0; j<21; j++)
{id[j]=0;}
0

LVL 30

Accepted Solution

Here's a corrected version of the code:

int main ()
{
ifstream inFile;
int precinct[11], id[21], name[21];

for (int i=0; i<11; i++)
{precinct[i]=0;}

for (int j=0; j<21; j++)
{id[j]=0;}

for (int k=0; k<21; k++)
{name[k]=0;}

while(!inFile.eof())
{
int Col1;
inFile >> Col1;
if (Col1 > 0 && Col1 < 21)
{
precinct[Col1-1]++;
}
int dummy;
inFile >> dummy;
}

return 0;

}
0

Author Comment

this is just for the first column...
i can do the same thing for the second column right?
0

LVL 30

Expert Comment

>> can do the same thing for the second column right?

Yes.
Just replace the above Dummy variable with Col2, and use Col2 to index your target array.
0

Author Comment

thanks a lot for your patience man, you've been a HUGE help!
0

## Featured Post

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base classâ€¦
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article locâ€¦
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relatâ€¦
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.