You are viewing our Forum Archives. To view or take place in current topics click here.
C++ Storing file in an array, and getting rid of duplicates.
Posted:

C++ Storing file in an array, and getting rid of duplicates.Posted:

HasTime
  • Wise One
Status: Offline
Joined: Mar 24, 201113Year Member
Posts: 553
Reputation Power: 22
Status: Offline
Joined: Mar 24, 201113Year Member
Posts: 553
Reputation Power: 22
Hello, I need help for a challenge problem. I am at a beginning level of programming, and I recently started working with arrays.

For this problem I need to read the contents of a file called "ballots.txt" then store the contents of the file in an array, then store the non duplicates in a file called "counted.txt". while separating the duplicates. The file contains votes for an election.

The duplicates are the ones that have the same beginning integer.

For example: (from the ballots.txt file)

2YNYNYNYNYNThe President //original
2YNNYYNNYYNThe President //duplicate vote
2NNNNNNNNNNThe New Guy //duplicate vote

Im looking for help on how to set this up.

Thank you so much.

HERE IS THE FULL FILE:
1YYYYYYYYYYThe New Guy
2YNYNYNYNYNThe President
2YNNYYNNYYNThe President
2NNNNNNNNNNThe New Guy
3NYNYNYNYNYThe New Guy
3YYYYYYYYYYThe President
3YYYYYYYYNYThe President
3YYYNNYYYYYThe President
4NNYYYNYYYYThe President
4NYYYYYYYYYThe President
5YYYYYYYNNYThe President
6YYNYNYYYYYThe President
7YNYNYNYNYNThe New Guy
9YNYNYNYNYNThe President
9YNNYYNNYYNThe President
10NYNYNNYNYNThe New Guy
11NYNYNNYNYNThe President
12NYYYNNYNYNThe New Guy
16NYNYYYYNYNThe New Guy
16NYNYNNYNYNThe President
16NYNYNNYNYNThe New Guy
17NYNYYYYNYNThe President
18NYNYNNYNYNThe New Guy
19NYNYNNYNYNThe President
20NYNYYYYNYNThe New Guy
21YNYNYNYYYYThe President
22YYYNYNNNYNThe New Guy
31YNYNYNYYYYThe President
32YYYNYNNNYNThe New Guy
41YYYYYYYYYYThe New Guy
42YNYNYNYNYNThe President
42YNNYYNNYYNThe President
42NNNNNNNNNNThe New Guy
43NYNYNYNYNYThe New Guy
43YYYYYYYYYYThe President
43YYYYYYYYNYThe President
43YYYNNYYYYYThe President
44NNYYYNYYYYThe New Guy
44NYYNNNYYYYThe President
45YYYYYYYNNYThe President
46YYNYNYYYYYThe President
47YNYNYNYNYNThe New Guy
49YNYNYNYNYNThe President
49YNNYYYNYYNThe President
50NYNYNYYNYNThe New Guy
51NYNYNNYNYNThe President
52NYYYNNYNYNThe New Guy
56NYNYYYYNYNThe New Guy
56NYNYYNYNYNThe President
57NYNYNNYNYNThe New Guy
57NYNYYYYNYNThe President
58NYNYNNYNYNThe New Guy
59NYNYNNYNYNThe President
60NYNYYYYNYNThe President
61YNYNYNYYYYThe President
62YYYNYNNNYNThe New Guy
76YNYNYNYYYYThe President
79YYYNYNNNYNThe New Guy
81YYYYYYYYYYThe New Guy
82YNYNYNYNYNThe President
82YNNYYNNYYNThe President
83YYYYYNNYNNThe New Guy
83NYNYNYNYNYThe New Guy
83YYYYYYYYYYThe President
83YYYYYYYYNYThe President
83YYYNNYYYYYThe President
84NNYYYNYYYYThe New Guy
84NYYNNNYYYYThe President
85YYNNYYYNNYThe President
86YYNYNYYYYYThe President
87YNYNYNYNYNThe New Guy
89YNYNYNYNYNThe President
89YNNYYYNYYNThe President
90YYNYNYYNYNThe New Guy
91NYNYNNYNYNThe President
92YYYYNNYNYNThe New Guy
96NYNYYYYNYNThe New Guy
96YYYYYNNYYNThe President
97NYNYNNYNYNThe New Guy
97NYNYYYYNYNThe President
98NYYYYYYNNYThe New Guy
99NYYYYYYNNYThe New Guy
#2. Posted:
Xaldin
  • TTG Addict
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
read it into an array of strings using getline(). check for duplicates using a loop and the string.at() function. Youll probably need multiple loops since the strings go up to double digits

while (i > number of lines in file)
{
     if(votes[i].at(0) == counter)
          array[i] = votes[i];
     i--;
}

im in a rush this is a pretty basic set up for it and youll need multiple to deal with the second character for double digit integers at the start. If you have questions ill try to answer later.
#3. Posted:
Cyimking
  • TTG Senior
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
HasTime wrote
[spoiler]
Hello, I need help for a challenge problem. I am at a beginning level of programming, and I recently started working with arrays.

For this problem I need to read the contents of a file called "ballots.txt" then store the contents of the file in an array, then store the non duplicates in a file called "counted.txt". while separating the duplicates. The file contains votes for an election.

The duplicates are the ones that have the same beginning integer.

For example: (from the ballots.txt file)

2YNYNYNYNYNThe President //original
2YNNYYNNYYNThe President //duplicate vote
2NNNNNNNNNNThe New Guy //duplicate vote

Im looking for help on how to set this up.

Thank you so much.

HERE IS THE FULL FILE:
1YYYYYYYYYYThe New Guy
2YNYNYNYNYNThe President
2YNNYYNNYYNThe President
2NNNNNNNNNNThe New Guy
3NYNYNYNYNYThe New Guy
3YYYYYYYYYYThe President
3YYYYYYYYNYThe President
3YYYNNYYYYYThe President
4NNYYYNYYYYThe President
4NYYYYYYYYYThe President
5YYYYYYYNNYThe President
6YYNYNYYYYYThe President
7YNYNYNYNYNThe New Guy
9YNYNYNYNYNThe President
9YNNYYNNYYNThe President
10NYNYNNYNYNThe New Guy
11NYNYNNYNYNThe President
12NYYYNNYNYNThe New Guy
16NYNYYYYNYNThe New Guy
16NYNYNNYNYNThe President
16NYNYNNYNYNThe New Guy
17NYNYYYYNYNThe President
18NYNYNNYNYNThe New Guy
19NYNYNNYNYNThe President
20NYNYYYYNYNThe New Guy
21YNYNYNYYYYThe President
22YYYNYNNNYNThe New Guy
31YNYNYNYYYYThe President
32YYYNYNNNYNThe New Guy
41YYYYYYYYYYThe New Guy
42YNYNYNYNYNThe President
42YNNYYNNYYNThe President
42NNNNNNNNNNThe New Guy
43NYNYNYNYNYThe New Guy
43YYYYYYYYYYThe President
43YYYYYYYYNYThe President
43YYYNNYYYYYThe President
44NNYYYNYYYYThe New Guy
44NYYNNNYYYYThe President
45YYYYYYYNNYThe President
46YYNYNYYYYYThe President
47YNYNYNYNYNThe New Guy
49YNYNYNYNYNThe President
49YNNYYYNYYNThe President
50NYNYNYYNYNThe New Guy
51NYNYNNYNYNThe President
52NYYYNNYNYNThe New Guy
56NYNYYYYNYNThe New Guy
56NYNYYNYNYNThe President
57NYNYNNYNYNThe New Guy
57NYNYYYYNYNThe President
58NYNYNNYNYNThe New Guy
59NYNYNNYNYNThe President
60NYNYYYYNYNThe President
61YNYNYNYYYYThe President
62YYYNYNNNYNThe New Guy
76YNYNYNYYYYThe President
79YYYNYNNNYNThe New Guy
81YYYYYYYYYYThe New Guy
82YNYNYNYNYNThe President
82YNNYYNNYYNThe President
83YYYYYNNYNNThe New Guy
83NYNYNYNYNYThe New Guy
83YYYYYYYYYYThe President
83YYYYYYYYNYThe President
83YYYNNYYYYYThe President
84NNYYYNYYYYThe New Guy
84NYYNNNYYYYThe President
85YYNNYYYNNYThe President
86YYNYNYYYYYThe President
87YNYNYNYNYNThe New Guy
89YNYNYNYNYNThe President
89YNNYYYNYYNThe President
90YYNYNYYNYNThe New Guy
91NYNYNNYNYNThe President
92YYYYNNYNYNThe New Guy
96NYNYYYYNYNThe New Guy
96YYYYYNNYYNThe President
97NYNYNNYNYNThe New Guy
97NYNYYYYNYNThe President
98NYYYYYYNNYThe New Guy
99NYYYYYYNNYThe New Guy
[/spoiler]


My two cents:
- Create a function that returns the beginning integers.
- Create an array of non duplicates.
- If the line is not in the non duplicated array, store it in the array.
- If the line is in the array then do nothing.
- After the last row, write each element of the array in the counted.txt file! (A loop)
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.