Counting not equal strings in array in C++
I want to count all the different string elements in an array.
So my input would be:
5 Lemon Orange Lemon Mango Lemon
And the output should be this:
3
The problem with my code is, that my code counts all the elements, and not just the different and I can't figure out why.
Here is my code:
#include <iostream>
using namespace std;
int main()
int N;
cin >> N;
string Tname;
string data[N];
int counter = 0;
for(int i = 0; i<N; i++)
cin >> Tname;
data[i] = Tname;
for(int l = 0; l<N; l++)
int k = 0;
while(k<N && (data[l] != data[k]))
k++;
if(k<N)
counter += 1;
cout << counter << endl;
return 0;
c++ arrays string count
add a comment |
I want to count all the different string elements in an array.
So my input would be:
5 Lemon Orange Lemon Mango Lemon
And the output should be this:
3
The problem with my code is, that my code counts all the elements, and not just the different and I can't figure out why.
Here is my code:
#include <iostream>
using namespace std;
int main()
int N;
cin >> N;
string Tname;
string data[N];
int counter = 0;
for(int i = 0; i<N; i++)
cin >> Tname;
data[i] = Tname;
for(int l = 0; l<N; l++)
int k = 0;
while(k<N && (data[l] != data[k]))
k++;
if(k<N)
counter += 1;
cout << counter << endl;
return 0;
c++ arrays string count
3
Thisstring data[N];
is not valid C++.
– Neil Butterworth
Nov 10 '18 at 21:36
1
I suppose just loading astd::unordered_set<std::string>
and reporting thesize()
upon loop completion is out of bounds. Like this.
– WhozCraig
Nov 10 '18 at 21:53
add a comment |
I want to count all the different string elements in an array.
So my input would be:
5 Lemon Orange Lemon Mango Lemon
And the output should be this:
3
The problem with my code is, that my code counts all the elements, and not just the different and I can't figure out why.
Here is my code:
#include <iostream>
using namespace std;
int main()
int N;
cin >> N;
string Tname;
string data[N];
int counter = 0;
for(int i = 0; i<N; i++)
cin >> Tname;
data[i] = Tname;
for(int l = 0; l<N; l++)
int k = 0;
while(k<N && (data[l] != data[k]))
k++;
if(k<N)
counter += 1;
cout << counter << endl;
return 0;
c++ arrays string count
I want to count all the different string elements in an array.
So my input would be:
5 Lemon Orange Lemon Mango Lemon
And the output should be this:
3
The problem with my code is, that my code counts all the elements, and not just the different and I can't figure out why.
Here is my code:
#include <iostream>
using namespace std;
int main()
int N;
cin >> N;
string Tname;
string data[N];
int counter = 0;
for(int i = 0; i<N; i++)
cin >> Tname;
data[i] = Tname;
for(int l = 0; l<N; l++)
int k = 0;
while(k<N && (data[l] != data[k]))
k++;
if(k<N)
counter += 1;
cout << counter << endl;
return 0;
c++ arrays string count
c++ arrays string count
edited Nov 10 '18 at 21:57
Christophe
39k43474
39k43474
asked Nov 10 '18 at 21:36
BenedekBenedek
183
183
3
Thisstring data[N];
is not valid C++.
– Neil Butterworth
Nov 10 '18 at 21:36
1
I suppose just loading astd::unordered_set<std::string>
and reporting thesize()
upon loop completion is out of bounds. Like this.
– WhozCraig
Nov 10 '18 at 21:53
add a comment |
3
Thisstring data[N];
is not valid C++.
– Neil Butterworth
Nov 10 '18 at 21:36
1
I suppose just loading astd::unordered_set<std::string>
and reporting thesize()
upon loop completion is out of bounds. Like this.
– WhozCraig
Nov 10 '18 at 21:53
3
3
This
string data[N];
is not valid C++.– Neil Butterworth
Nov 10 '18 at 21:36
This
string data[N];
is not valid C++.– Neil Butterworth
Nov 10 '18 at 21:36
1
1
I suppose just loading a
std::unordered_set<std::string>
and reporting the size()
upon loop completion is out of bounds. Like this.– WhozCraig
Nov 10 '18 at 21:53
I suppose just loading a
std::unordered_set<std::string>
and reporting the size()
upon loop completion is out of bounds. Like this.– WhozCraig
Nov 10 '18 at 21:53
add a comment |
3 Answers
3
active
oldest
votes
The problem is algorithmic: every item is equal to itself, which will end your k loop prematurely. In addition, you only increment when the item is repeated.
I propose you to change the loops, so not to compare every items with every other items, but only items, to items previously processed:
for(int l = 0; l<N; l++)
int k = 0;
while(k<l && data[l] != data[k]) // only previous items
k++;
if(k==l) // if no identical, we can add this one
cout<<l<<" "<<data[l]<<endl;
counter += 1;
Not related: variable length arrays are not legal C++ even if some mainstream compilers accept it. I'd suggest to use a vector to emulate this feature: vector<string> data(N);
Online demo
This doesn't fix the logic. This is counting 3 lemons and it just happens to be the same as the answer.
– super
Nov 10 '18 at 22:00
@super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items
– Christophe
Nov 10 '18 at 22:10
1
Yeah, thank you, this resolved my code, and now I understand what was the problem in my logic. Thanks a lot.
– Benedek
Nov 10 '18 at 22:35
@Benedek glad to know that it helped :-) Thanks for this feedback
– Christophe
Nov 10 '18 at 22:48
add a comment |
if i understand well your problem , you want the value that has the max appearances in the array, some modifications are needed to achieve this :
#include <iostream>
using namespace std;
int main()
int N;
cin >> N;
string Tname;
string data[N];
int counter = 0;
for(int i = 0; i<N; i++)
cin >> Tname;
data[i] = Tname;
int tempCounter; // a temporary counter for each item of the array .
for(int l = 0; l < N; l++)
tempCounter = 0;
int k = 0;
while(k<N)
if(data[l] == data[k])
tempCounter++;
k++;
if(tempCounter > counter) // if the new counter is higher than the counter
counter = tempCounter;
cout << counter << endl;
return 0;
Not the max appearances. I wanted to count all the different elements in the array. Sorry if my explanation wasn't accurate. But thanks for the help, I think i learn from that as well.
– Benedek
Nov 10 '18 at 22:38
add a comment |
the last if should be if(k+1==N)
because you all the time stop the while before the k reach N
and k must start from l
Your logic is, you add 1 to the counter if it is not in the list's remaining part
But the code check the full list so you never count thw world whitch in the list twice.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243639%2fcounting-not-equal-strings-in-array-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is algorithmic: every item is equal to itself, which will end your k loop prematurely. In addition, you only increment when the item is repeated.
I propose you to change the loops, so not to compare every items with every other items, but only items, to items previously processed:
for(int l = 0; l<N; l++)
int k = 0;
while(k<l && data[l] != data[k]) // only previous items
k++;
if(k==l) // if no identical, we can add this one
cout<<l<<" "<<data[l]<<endl;
counter += 1;
Not related: variable length arrays are not legal C++ even if some mainstream compilers accept it. I'd suggest to use a vector to emulate this feature: vector<string> data(N);
Online demo
This doesn't fix the logic. This is counting 3 lemons and it just happens to be the same as the answer.
– super
Nov 10 '18 at 22:00
@super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items
– Christophe
Nov 10 '18 at 22:10
1
Yeah, thank you, this resolved my code, and now I understand what was the problem in my logic. Thanks a lot.
– Benedek
Nov 10 '18 at 22:35
@Benedek glad to know that it helped :-) Thanks for this feedback
– Christophe
Nov 10 '18 at 22:48
add a comment |
The problem is algorithmic: every item is equal to itself, which will end your k loop prematurely. In addition, you only increment when the item is repeated.
I propose you to change the loops, so not to compare every items with every other items, but only items, to items previously processed:
for(int l = 0; l<N; l++)
int k = 0;
while(k<l && data[l] != data[k]) // only previous items
k++;
if(k==l) // if no identical, we can add this one
cout<<l<<" "<<data[l]<<endl;
counter += 1;
Not related: variable length arrays are not legal C++ even if some mainstream compilers accept it. I'd suggest to use a vector to emulate this feature: vector<string> data(N);
Online demo
This doesn't fix the logic. This is counting 3 lemons and it just happens to be the same as the answer.
– super
Nov 10 '18 at 22:00
@super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items
– Christophe
Nov 10 '18 at 22:10
1
Yeah, thank you, this resolved my code, and now I understand what was the problem in my logic. Thanks a lot.
– Benedek
Nov 10 '18 at 22:35
@Benedek glad to know that it helped :-) Thanks for this feedback
– Christophe
Nov 10 '18 at 22:48
add a comment |
The problem is algorithmic: every item is equal to itself, which will end your k loop prematurely. In addition, you only increment when the item is repeated.
I propose you to change the loops, so not to compare every items with every other items, but only items, to items previously processed:
for(int l = 0; l<N; l++)
int k = 0;
while(k<l && data[l] != data[k]) // only previous items
k++;
if(k==l) // if no identical, we can add this one
cout<<l<<" "<<data[l]<<endl;
counter += 1;
Not related: variable length arrays are not legal C++ even if some mainstream compilers accept it. I'd suggest to use a vector to emulate this feature: vector<string> data(N);
Online demo
The problem is algorithmic: every item is equal to itself, which will end your k loop prematurely. In addition, you only increment when the item is repeated.
I propose you to change the loops, so not to compare every items with every other items, but only items, to items previously processed:
for(int l = 0; l<N; l++)
int k = 0;
while(k<l && data[l] != data[k]) // only previous items
k++;
if(k==l) // if no identical, we can add this one
cout<<l<<" "<<data[l]<<endl;
counter += 1;
Not related: variable length arrays are not legal C++ even if some mainstream compilers accept it. I'd suggest to use a vector to emulate this feature: vector<string> data(N);
Online demo
edited Nov 10 '18 at 22:08
answered Nov 10 '18 at 21:53
ChristopheChristophe
39k43474
39k43474
This doesn't fix the logic. This is counting 3 lemons and it just happens to be the same as the answer.
– super
Nov 10 '18 at 22:00
@super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items
– Christophe
Nov 10 '18 at 22:10
1
Yeah, thank you, this resolved my code, and now I understand what was the problem in my logic. Thanks a lot.
– Benedek
Nov 10 '18 at 22:35
@Benedek glad to know that it helped :-) Thanks for this feedback
– Christophe
Nov 10 '18 at 22:48
add a comment |
This doesn't fix the logic. This is counting 3 lemons and it just happens to be the same as the answer.
– super
Nov 10 '18 at 22:00
@super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items
– Christophe
Nov 10 '18 at 22:10
1
Yeah, thank you, this resolved my code, and now I understand what was the problem in my logic. Thanks a lot.
– Benedek
Nov 10 '18 at 22:35
@Benedek glad to know that it helped :-) Thanks for this feedback
– Christophe
Nov 10 '18 at 22:48
This doesn't fix the logic. This is counting 3 lemons and it just happens to be the same as the answer.
– super
Nov 10 '18 at 22:00
This doesn't fix the logic. This is counting 3 lemons and it just happens to be the same as the answer.
– super
Nov 10 '18 at 22:00
@super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items
– Christophe
Nov 10 '18 at 22:10
@super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items
– Christophe
Nov 10 '18 at 22:10
1
1
Yeah, thank you, this resolved my code, and now I understand what was the problem in my logic. Thanks a lot.
– Benedek
Nov 10 '18 at 22:35
Yeah, thank you, this resolved my code, and now I understand what was the problem in my logic. Thanks a lot.
– Benedek
Nov 10 '18 at 22:35
@Benedek glad to know that it helped :-) Thanks for this feedback
– Christophe
Nov 10 '18 at 22:48
@Benedek glad to know that it helped :-) Thanks for this feedback
– Christophe
Nov 10 '18 at 22:48
add a comment |
if i understand well your problem , you want the value that has the max appearances in the array, some modifications are needed to achieve this :
#include <iostream>
using namespace std;
int main()
int N;
cin >> N;
string Tname;
string data[N];
int counter = 0;
for(int i = 0; i<N; i++)
cin >> Tname;
data[i] = Tname;
int tempCounter; // a temporary counter for each item of the array .
for(int l = 0; l < N; l++)
tempCounter = 0;
int k = 0;
while(k<N)
if(data[l] == data[k])
tempCounter++;
k++;
if(tempCounter > counter) // if the new counter is higher than the counter
counter = tempCounter;
cout << counter << endl;
return 0;
Not the max appearances. I wanted to count all the different elements in the array. Sorry if my explanation wasn't accurate. But thanks for the help, I think i learn from that as well.
– Benedek
Nov 10 '18 at 22:38
add a comment |
if i understand well your problem , you want the value that has the max appearances in the array, some modifications are needed to achieve this :
#include <iostream>
using namespace std;
int main()
int N;
cin >> N;
string Tname;
string data[N];
int counter = 0;
for(int i = 0; i<N; i++)
cin >> Tname;
data[i] = Tname;
int tempCounter; // a temporary counter for each item of the array .
for(int l = 0; l < N; l++)
tempCounter = 0;
int k = 0;
while(k<N)
if(data[l] == data[k])
tempCounter++;
k++;
if(tempCounter > counter) // if the new counter is higher than the counter
counter = tempCounter;
cout << counter << endl;
return 0;
Not the max appearances. I wanted to count all the different elements in the array. Sorry if my explanation wasn't accurate. But thanks for the help, I think i learn from that as well.
– Benedek
Nov 10 '18 at 22:38
add a comment |
if i understand well your problem , you want the value that has the max appearances in the array, some modifications are needed to achieve this :
#include <iostream>
using namespace std;
int main()
int N;
cin >> N;
string Tname;
string data[N];
int counter = 0;
for(int i = 0; i<N; i++)
cin >> Tname;
data[i] = Tname;
int tempCounter; // a temporary counter for each item of the array .
for(int l = 0; l < N; l++)
tempCounter = 0;
int k = 0;
while(k<N)
if(data[l] == data[k])
tempCounter++;
k++;
if(tempCounter > counter) // if the new counter is higher than the counter
counter = tempCounter;
cout << counter << endl;
return 0;
if i understand well your problem , you want the value that has the max appearances in the array, some modifications are needed to achieve this :
#include <iostream>
using namespace std;
int main()
int N;
cin >> N;
string Tname;
string data[N];
int counter = 0;
for(int i = 0; i<N; i++)
cin >> Tname;
data[i] = Tname;
int tempCounter; // a temporary counter for each item of the array .
for(int l = 0; l < N; l++)
tempCounter = 0;
int k = 0;
while(k<N)
if(data[l] == data[k])
tempCounter++;
k++;
if(tempCounter > counter) // if the new counter is higher than the counter
counter = tempCounter;
cout << counter << endl;
return 0;
answered Nov 10 '18 at 22:00
Mohamed Ali RACHIDMohamed Ali RACHID
1,831316
1,831316
Not the max appearances. I wanted to count all the different elements in the array. Sorry if my explanation wasn't accurate. But thanks for the help, I think i learn from that as well.
– Benedek
Nov 10 '18 at 22:38
add a comment |
Not the max appearances. I wanted to count all the different elements in the array. Sorry if my explanation wasn't accurate. But thanks for the help, I think i learn from that as well.
– Benedek
Nov 10 '18 at 22:38
Not the max appearances. I wanted to count all the different elements in the array. Sorry if my explanation wasn't accurate. But thanks for the help, I think i learn from that as well.
– Benedek
Nov 10 '18 at 22:38
Not the max appearances. I wanted to count all the different elements in the array. Sorry if my explanation wasn't accurate. But thanks for the help, I think i learn from that as well.
– Benedek
Nov 10 '18 at 22:38
add a comment |
the last if should be if(k+1==N)
because you all the time stop the while before the k reach N
and k must start from l
Your logic is, you add 1 to the counter if it is not in the list's remaining part
But the code check the full list so you never count thw world whitch in the list twice.
add a comment |
the last if should be if(k+1==N)
because you all the time stop the while before the k reach N
and k must start from l
Your logic is, you add 1 to the counter if it is not in the list's remaining part
But the code check the full list so you never count thw world whitch in the list twice.
add a comment |
the last if should be if(k+1==N)
because you all the time stop the while before the k reach N
and k must start from l
Your logic is, you add 1 to the counter if it is not in the list's remaining part
But the code check the full list so you never count thw world whitch in the list twice.
the last if should be if(k+1==N)
because you all the time stop the while before the k reach N
and k must start from l
Your logic is, you add 1 to the counter if it is not in the list's remaining part
But the code check the full list so you never count thw world whitch in the list twice.
answered Nov 10 '18 at 22:03
Berecz BalázsBerecz Balázs
245
245
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243639%2fcounting-not-equal-strings-in-array-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
This
string data[N];
is not valid C++.– Neil Butterworth
Nov 10 '18 at 21:36
1
I suppose just loading a
std::unordered_set<std::string>
and reporting thesize()
upon loop completion is out of bounds. Like this.– WhozCraig
Nov 10 '18 at 21:53