• Status: Solved
• Priority: Medium
• Security: Public
• Views: 230

# arrays

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
Kal130
• 16
• 12
1 Solution

Commented:
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 Commented:
is their a simpler way to do this maybe with counters, cause i have not learned maps and vectors yet
0

Commented:
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 Commented:
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

Commented:
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 Commented:
reading the data from a file into an array
0

Commented:
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 Commented:
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

Commented:
Can you post your current code?
0

Author Commented:
#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

Commented:
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 Commented:
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

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

Commented:
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 Commented:
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

Commented:
>>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 Commented:
would it matter if in the data file the values are not in order:

2 20
1 11
6 12
0

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

No, it shouldn't matter.
0

Author Commented:
this part compiles but hangs
0

Commented:
>>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

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

Author Commented:

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

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

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

Commented:
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 Commented:
this is just for the first column...
i can do the same thing for the second column right?
0

Commented:
>> 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 Commented:
thanks a lot for your patience man, you've been a HUGE help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.