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

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
Asked:
Kal130
  • 16
  • 12
1 Solution
 
AxterCommented:
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
 
Kal130Author Commented:
is their a simpler way to do this maybe with counters, cause i have not learned maps and vectors yet
0
 
AxterCommented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Kal130Author 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
 
AxterCommented:
So do you need help with code for reading the data from a file, or do you need help setting up the array?
0
 
Kal130Author Commented:
reading the data from a file into an array
0
 
AxterCommented:
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
 
Kal130Author 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
 
AxterCommented:
Can you post your current code?
0
 
Kal130Author Commented:
#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;

}
0
 
AxterCommented:
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
 
Kal130Author 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
 
AxterCommented:
You can try something like the following
          while(!inFile.eof())
          {
               int Col1;
               inFile >> Col1;
               inFile >> id[Col1-1];
          }
0
 
AxterCommented:
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
 
Kal130Author 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
 
AxterCommented:
>>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
 
Kal130Author Commented:
would it matter if in the data file the values are not in order:

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

No, it shouldn't matter.
0
 
Kal130Author Commented:
this part compiles but hangs
0
 
AxterCommented:
>>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
 
AxterCommented:
Can you post exactly what you have in your text file, so I can run a quick test?
0
 
Kal130Author Commented:
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;

}
0
 
AxterCommented:
You should have the following if condition
            if (Col1 > 0 && Col1 < 21)
            {
                  precinct[Col1-1]++;
            }
0
 
AxterCommented:
The following for loop had the wrong increment variable:
      for (int j=0; j<21; j++)
      {id[j]=0;}
0
 
AxterCommented:
Here's a corrected version of the code:

int main ()
{
      ifstream inFile;
      inFile.open("c:\\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; 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
 
Kal130Author Commented:
this is just for the first column...
i can do the same thing for the second column right?
0
 
AxterCommented:
>> 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
 
Kal130Author 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.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 16
  • 12
Tackle projects and never again get stuck behind a technical roadblock.
Join Now