|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Frequency Distribution
I am just in my first year of programming and I am still a little confused and I was wondering if anyone could help on this program I am trying to do. Thx.
Description: Frequency Distribution Program – Write a program that will process an input file and determine the frequency distribution for the length of words in the file. For our purpose a word is defined as 1 or more alphanumeric characters. The length of the word will not exceed 78 characters in length. Output should consist of a summary of the distribution list and output should be directed to the screen as well as an output file. Code your algorithm for your solution to include the following: Prompt and accept the names of the input and output files. When printing the summary, only print results for lengths of words totals that are greater than zero. Example: 1 character words = 13 2 character words = 21 3 character words = 7 4 character words = 14 5 character words = 5 6 character words = 6 7 character words = 1 10 character words =3 Use good use of whitespace, and internal documentation. Use functions and procedures as needed. |
|
#2
|
|||
|
|||
|
Drop out now.
Getting others to do your homework is: 1) Stupid 2) Against your university's policies 3) Against everything I stand for 4) Not going to teach you a damned thing. You need to sit down and put forth a valiant effort, then come on back with SPECIFIC questions regarding your assignment. Only then will I (and most others) help you out. Good luck. edit: Doesn't your university have course assistants or instructors able to better guide you? I know mine does. |
|
#3
|
|||
|
|||
|
First of all I am only 15. I am learning C++ on my own, because I really like computers. I found this program on the internet so I thought I would mess around and try to figure it out just for teh heck of it. So, I don't attend a university and I don't have any teachers or tutors to help me. So, I thought I would turn to one of these forums for help, but undoubtley yall like to make assumptions. Thanks for all your wonderful help.
|
|
#4
|
|||
|
|||
|
I do put forth effort. I did this code the other day, and I know all I have to do is manipulate it to get to work.The code below is to count the number of vowels in a file. Just can't figure out the problem I have already stated and that rudely replied to. Can you get that your head now, I AM NOT A STUDENT IN A C++ CLASS! I just need help.
#include <iostream.h> #include <fstream.h> int main () { int a; int e; int i; int o; int u; char ch; char filename [12]; cout << "What file would like to run?\n"; cin >> filename; ifstream infile (filename); ofstream outfile ("file.dat"); a=0; e=0; i=0; o=0; u=0; while (infile.get(ch)) { outfile << ch; switch (ch) { case 'a' : a=a+1; break; case 'e' : e=e+1; break; case 'i' : i=i+1; break; case 'o' : o=o+1; break; case 'u' : u=u+1; break; case 'A' : a=a+1; break; case 'E' : e=e+1; break; case 'I' : i=i+1; break; case 'O' : o=o+1; break; case 'U' : u=u+1; break; } } cout << "A = " << a <<"\n"; cout << "E = " << e <<"\n"; cout << "I = " << i <<"\n"; cout << "O = " << o <<"\n"; cout << "U = " << u <<"\n"; outfile << "\n\n\n-----\n"; outfile << "A = " << a <<"\n"; outfile << "E = " << e <<"\n"; outfile << "I = " << i <<"\n"; outfile << "O = " << o <<"\n"; outfile << "U = " << u <<"\n"; return 0; } |
|
#5
|
||||
|
||||
|
The solution to your problem would be similar to the solution to the vowel counting problem, in that you'd have separate variables to keep track of the counts of different length words; one variable for each length. To keep things simple, you'd probably just want to keep track of say, words between 1 and 10 characters in length -- although there are many possible ways to allow for words of any length at all.
Then, the only problem left is how to read one word at a time and count the words based on length. Reading a word at a time is simple, and can be done by using the >> ifstream operator to read into a 'string' or a char[] array. If you are using a 'string', then the length() method of string will help you. For char[] arrays or char * pointers, the strlen() function will do what you need. So you'd basically just read one word at a time, figure out that word's length, then increment the appropriate acount variable depending on the length. You can have 10 separate count variables (assuming you are looking at words from 1 to 10 chars in length) named something like n1, n2, n3, ..., or you could use an array of 10 integers, perhaps declared as "int counts[10];". Either way would work; the advantage of an array is that you can index it with another variable (e.g. counts[len] = counts[len] + 1), which will save you some typing. Hope that helps, J.C. Edit: Also, if you are using >> to read from an ifstream, you can the good() method of ifstream to check to see if more data can be read or if you are at the end of the file (or there is an error). Last edited by peenie : December 1st, 2003 at 11:09 PM. |
|
#6
|
|||
|
|||
|
Eh, so I assumed a bit too much. No big deal. I gave you a life lesson for when you want to do the same thing later on.
Either way, your wants were too many. I would've much rather seen your failed attempts in order to help narrow it down. Coming to solutions on your own is wonderful. Being able to implement them, even more so. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Frequency Distribution |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|