Link to home
Start Free TrialLog in
Avatar of Kal130
Kal130

asked on

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.

Avatar of Axter
Axter
Flag of United States of America image

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!
Avatar of Kal130
Kal130

ASKER

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

ASKER

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 :)
So do you need help with code for reading the data from a file, or do you need help setting up the array?
Avatar of Kal130

ASKER

reading the data from a file into an array
I see you already have posted a previous related question:
https://www.experts-exchange.com/questions/21388312/arrays-w-data-files.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.
Avatar of Kal130

ASKER

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
Can you post your current code?
Avatar of Kal130

ASKER

#include <iostream>
#include <fstream>

using namespace std;

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

int main ()
{
      ifstream inFile;
      inFile.open("votes.txt");
      int precinct[MAX], id[nMAX], name[nMAX];
    int votes;
   
    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())
            {
                  precinct[1("votes")]++;
            }
      return 0;

}
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?
Avatar of Kal130

ASKER

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)
You can try something like the following
          while(!inFile.eof())
          {
               int Col1;
               inFile >> Col1;
               inFile >> id[Col1-1];
          }
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;
          }
Avatar of Kal130

ASKER

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)
>>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.


Avatar of Kal130

ASKER

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

2 20
1 11
6 12
>>would it matter if in the data file the values are not in order:

No, it shouldn't matter.
Avatar of Kal130

ASKER

this part compiles but hangs
>>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?
Can you post exactly what you have in your text file, so I can run a quick test?
Avatar of Kal130

ASKER

votes.txt

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;
      inFile.open("votes.txt");
      int precinct[11], id[21], name[21];
    int votes;
   
    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;

}
You should have the following if condition
            if (Col1 > 0 && Col1 < 21)
            {
                  precinct[Col1-1]++;
            }
The following for loop had the wrong increment variable:
      for (int j=0; j<21; j++)
      {id[j]=0;}
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kal130

ASKER

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

ASKER

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