C++ help because im terrible at it :(

Status
Not open for further replies.

elemental523

Junior Member
Mar 1, 2011
2
0
0
So I have to finish a program as a project for my C++ class. I have to input a sentence and have the program count the number of words in the sentence and then count how many of each character there are in that sentence. Example: I Love Pie

The program needs to print:
There are three words.
(I have this part done)

There are the count of these characters.
(EVIL!!!)
I=2
L=1
O=1
V=1
E=2
P=1

I have read my book and tried so many different ways. Please help me. If possible can you write that piece of the code and then break it down explaining what action does what. Please and thank you

============================
Guys/Gals - Please see my post at #28 when trying to assist

Common Courtesy
At Admin
 
Last edited by a moderator:

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,591
5
0
setup an array of 26 characters.
In your word processing loop; you must be looking at each character to see if it is a space or word delimiter.

check if the character is a space - apparently you are able to do this already from your post.

If it is not a space; then increment the value in the array slot that corresponds to the character.

I am being vague; because these are the two key concepts that you need to figure out for this assignment.
  • character determination
  • indexing
 
Last edited by a moderator:

tvdang7

Platinum Member
Jun 4, 2005
2,242
5
81
dang sounds like it requires a lil thinking . i kinda want to do this just to test my self.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
Ah, misread it initially. I thought you where counting number of words used (which is a bit harder.)

What characters are supposed to be counted? Alpha-numeric, or everything (IE, spaces, commas, and perhaps quotations.

and some hints.

'a' + 2 = 'c' (you should know this.)
If you aren't allowed to use toupper, you'll need to implement one.
In standard ASCII, there are 128 characters. If you are counting everything, this is important. (256 if you are using extended ascii).
You most certainly can do comparisons like (value == '\n'), in fact, I would recommend it for checking delimiters. In other words, don't put in ascii values.
It is perfectly proper to have two spaces right next to each other in english. You should check for that.
Most of this should fit nicely in a for loop. You should only have to visit each character in the sentence once.
While string::find() might look nice, don't use it for this. You are going to have to visit each character, using find could be very costly.
In computers, EVERYTHING is a number. letters are really numbers, use this to your advantage.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
Does it care if its caps? I'd do the following:

string input = "this is your input";

int totals[26];

int count = 0;

for(char i = 'a'; i <= 'z'; i++){
totals[count] = 0;
count++;
}
//this sets your initial counts to 0

char temp;

for(int i = 0; i < input.length(); i++){
temp = to_lower(input);
count = 0; //reset count
for(char j = 'a'; j <= 'z'; j++){
if(j == temp)
totals[count]++;
}
count++;
}
}

//time to print
count = 0;
for(char i = 'a'; i <= 'z'; i++){
cout << i << "=" << totals[count] << endl;
count++;
}

system("pause");

Something like that
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
Does it care if its caps? I'd do the following:

/* Removed code */

Something like that

Op, I would suggest not doing it this way. While it looks like it may work... it is VERY inefficient. (at a first glance).

Tweak155 was this a serious attempt at this problem?
 
Last edited:

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
Op, I would suggest not doing it this way. While it looks like it may work... it is VERY inefficient. (at a first glance).

Tweak155 was this a serious attempt at this problem?

No he needed it tonight. And really are you serious? You think a double for loop defined to not go higher than 26 is that inefficient? Where is your code that helped this guy? High horse much? Jebus.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
No he needed it tonight. And really are you serious? You think a double for loop defined to not go higher than 26 is that inefficient? Where is your code that helped this guy? High horse much? Jebus.

Dead serious. This didn't need a double for loop. And yes, it is HIGHLY inefficient, you are searching, needlessly, though your array of hits. That is inefficient.

I did not post code because this is a homework assignment. Just giving an answer is NEVER productive for teaching someone how to learn to program. PM me if you really want to see an efficient solution to this.
 

Merad

Platinum Member
May 31, 2010
2,586
19
81
Efficiency is kind of a silly point to argue in a program this simple. But your approach is extremely brute force, when all you really need is a quick bit of math (and I guess a bit of knowledge of ASCII codes).

Edit: I took my solution out since OP is still looking for the answer today.
 

masteryoda34

Golden Member
Dec 17, 2007
1,399
3
81
As a general rule, consideration of efficiency should always be second to correctness, because A) even if a piece of code is inefficient, it is very likely the computer will still do it far faster than required, and B) for a number of reasons including compiler optimization, programmers are notoriously wrong at predicting what sections of their program will be the actual bottlenecks at runtime with a real workload.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
As a general rule, consideration of efficiency should always be second to correctness, because A) even if a piece of code is inefficient, it is very likely the computer will still do it far faster than required, and B) for a number of reasons including compiler optimization, programmers are notoriously wrong at predicting what sections of their program will be the actual bottlenecks at runtime with a real workload.

Readable code should always take place above everything, even code that works. Readable code is maintainable, and contrary to popular belief, often fairly efficient.

I disagree with the general attitude of "Oh, you can't call the inefficient! Efficiency isn't even that important!" I feel it leads to crappy code and creates an atmosphere where code can't properly be reviewed.

Some programmers can't tell the difference between efficient and inefficient code, this is true. But that is not a general rule.

Good code and efficiency should always be recognized. Bad code should always be pointed out. lest the bad coder goes on thinking his code is perfect.

Some of the crappiest code generated comes from the "Just get something out there that works" attitude. Crappy code is expensive code to maintain.
 
Last edited:

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,284
3,905
75
While it's said, and it may be true, that, "Premature optimization is the root of all evil," an understanding and implementation of simple algorithms is not premature optimization. I would point you to the counting sort algorithm in this case. Though their analysis is a little overblown for this application. It should be possible to solve this problem in a single pass.

I do have a question, though. Is "counting-sort" counted as one word or two, for instance?

Edit: On the other hand, this sentence - written strangely as it is - should not have its hyphens counted as words.
 
Last edited:

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
Dang guys, seriously. This code isn't that important.

I didn't use the ascii values because I couldn't remember where 'a' started and I didn't feel like starting up a compiler to make sure the math was right. Some of you guys take this way too seriously. When it is someone else's homework I don't try to make it efficient because they most likely wouldn't know how anyway so what is the point?

I'm seriously amazed at these responses.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
Dead serious. This didn't need a double for loop. And yes, it is HIGHLY inefficient, you are searching, needlessly, though your array of hits. That is inefficient.

I did not post code because this is a homework assignment. Just giving an answer is NEVER productive for teaching someone how to learn to program. PM me if you really want to see an efficient solution to this.

Haha! Thanks for the laugh bro, seriously. I missed this the first time around.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
Haha! Thanks for the laugh bro, seriously. I missed this the first time around.

Seriously. Your code is bad. If you want to see a good solution, I'm willing to share (feel free to tell me how sucky it is).

I will not, however, post the solution to HW in a HW thread.
 
Last edited:

masteryoda34

Golden Member
Dec 17, 2007
1,399
3
81
Readable code should always take place above everything, even code that works. Readable code is maintainable, and contrary to popular belief, often fairly efficient.

I disagree with the general attitude of "Oh, you can't call the inefficient! Efficiency isn't even that important!" I feel it leads to crappy code and creates an atmosphere where code can't properly be reviewed.

Some programmers can't tell the difference between efficient and inefficient code, this is true. But that is not a general rule.

Good code and efficiency should always be recognized. Bad code should always be pointed out. lest the bad coder goes on thinking his code is perfect.

Some of the crappiest code generated comes from the "Just get something out there that works" attitude. Crappy code is expensive code to maintain.


I'll agree that readability is very important. But I disagree that readability is more important than correctness. However, readability and correctness often go hand in hand.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
I'll agree that readability is very important. But I disagree that readability is more important than correctness. However, readability and correctness often go hand in hand.

While I agree, they do often go hand in hand. Which would you rather work with, code that is not correct, but setup in such a way that navigating it is a snap, or code that absolutely works, but is a nightmare to navigate.

Remember code like this is technically correct, but not really readable.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
Seriously. You code is bad. If you want to see a good solution, I'm willing to share (feel free to tell me how sucky it is).

I will not, however, post the solution to HW in a HW thread.

You English is bad.
 

Born2bwire

Diamond Member
Oct 28, 2005
9,840
6
71
While I agree, they do often go hand in hand. Which would you rather work with, code that is not correct, but setup in such a way that navigating it is a snap, or code that absolutely works, but is a nightmare to navigate.

Remember code like this is technically correct, but not really readable.

I have a feeling that one's personal preference here is going to be largely influenced by whether or not you have done a lot of legacy code maintainance or have inherited large projects. Personally, I would value efficiency and readability at the top. If a code is readable but not correct, at least I can damn well fix it easily. Efficiency is important because I have seen how little efficiencies and poor practices add up and unnecessarily bog down a program. When it can take a day to get results, even small performance improvements make a difference. But this is just my preference because I have had to inherit a lot of projects that I had to add functionality and debug. Poor readability greatly increases the amount of time needed to start working with a code. Sometimes it's bad enough that it takes me less time to rewrite the entire code than to understand it. If it comes to that point then it's just a wash. Hell, I can't remember what I did last week half the time so even if I was looking at my own code I need to rely on good commenting and readability to understand it again.

Dang guys, seriously. This code isn't that important.

I didn't use the ascii values because I couldn't remember where 'a' started and I didn't feel like starting up a compiler to make sure the math was right. Some of you guys take this way too seriously. When it is someone else's homework I don't try to make it efficient because they most likely wouldn't know how anyway so what is the point?

I'm seriously amazed at these responses.

You don't need to know the ASCII. Just subtract off the ASCII value for 'a' or 'A' where appropriate (or better yet just convert them to all lowercase first) and use this as your index to your array. Easy-peasy.
 
Last edited:

Cogman

Lifer
Sep 19, 2000
10,278
126
106
You English is bad.

lol, . This is what happens when you rely totally on red lines for correct grammar.

Either way, I'll still happily show you a good and near complete solution to the OPs problem. After all, my goal isn't to berate you, my goal is to raise your programming skills.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
lol, . This is what happens when you rely totally on red lines for correct grammar.

Either way, I'll still happily show you a good and near complete solution to the OPs problem. After all, my goal isn't to berate you, my goal is to raise your programming skills.

Well to be perfectly honest, your failure is in assuming this is how I would write my own program. In reality, I rarely need anything this simple. Should I have cared enough for this person's program, I would have opened my compiler and made sure the code worked at the very least.

No, I didn't even do that. I just wrote some lines that would work and that was that. If I really felt like it I could have optimized the code just like you're suggesting I can't do, and quite honestly I'm not sure why you think this. I've stated repeatedly this is just some program I couldn't care less about so I just typed something out for him.

If I really want to optimize code I like to make sure it works, otherwise I'll just write something for someone else that helps them out with an assignment. As people grow, you realize if they are really interested in something, they will take the time to learn it. Obviously this person just wanted help. Why bother providing coding techniques and etc? Should the person request such code out of sincere interest, I'll do it.

In either event, I can write optimized code. Quit making assumptions.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
Well to be perfectly honest, your failure is in assuming this is how I would write my own program. In reality, I rarely need anything this simple. Should I have cared enough for this person's program, I would have opened my compiler and made sure the code worked at the very least.

No, I didn't even do that. I just wrote some lines that would work and that was that. If I really felt like it I could have optimized the code just like you're suggesting I can't do, and quite honestly I'm not sure why you think this. I've stated repeatedly this is just some program I couldn't care less about so I just typed something out for him.

If I really want to optimize code I like to make sure it works, otherwise I'll just write something for someone else that helps them out with an assignment. As people grow, you realize if they are really interested in something, they will take the time to learn it. Obviously this person just wanted help. Why bother providing coding techniques and etc? Should the person request such code out of sincere interest, I'll do it.

In either event, I can write optimized code. Quit making assumptions.

Meh, It comes from me being overly obsessive about some things. It can be both a blessing and a curse.
 
Status
Not open for further replies.
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |