|
c++ hw(off topic)
|
View this Thread in Original format
| ASFSE |
| thought i'd ask here, since most of the technical ppl are in the production forum. anyway, does anyone have good experiance with c++ and can give me a hand with some introductory array homework?? my aim is breaksnbeatsrgr8 , please help me out!! |
|
|
| ASFSE |
sorry but, my assignment is a bit more complicated lol...here it is:
Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must be ignored. Any characters entered after the period ('.') should be left in the input stream unprocessed. No limit may be placed on the length of the input. Use an array with a struct type as its base type so that each array element can hold both a letter and an integer. The table should not be case sensitive -- for example, lower case 'a' and upper case 'A' should be counted as the same letter.
edit: so if you can help me get started plz msg me on aim, im desperate!! i'll make it worth your while!!:D |
|
|
| Low Profile |
From and old book, but the language hasn't changed a whole lot since it was published (most noticeable is the lack of the using namespace statement):
http://newdata.box.sk/bx/c/htm/ch11.htm - chapter on arrays
If you don't know how to do this already I suggest you do your homework more often and read the textbooks, this is not a hard thing to do :D |
|
|
| ASFSE |
| ah but i've done all my homework! and i've read the text book, i've gotten perfect scores on all assigments thus far....it's just that i opted to take this as an online course, and unfortuntaly that was a stupid thing to do as it's harder to ask questions and stuff!! |
|
|
| alanzo |
I would creaet a 28-way If/else statement.. one for each of the letters one for the '.' and one for the print out frequency command
In each else, would increment a counter int. You can do counting easily with Maps, but the assignment doesn't say to use a map... so that's the only way I can think to count letter frequency without a map.
If you use MS Visual Studio, the command box and cin/cout works quite well for your command prompt.
creating a struct is like creating a class...
struct input {
int count;
string letter;
}
I think you can use input[26] to create an array of 26 of them (one for each letter)... not positive, though... I would of just used a Vector..
you can use a tolower command from the STL to convert a string to it's lower case equivelent.. that way A == a and a == a if lowercase a is input.
//Example reads in a character and makes up lowercase
#include
#include
using namespace std;
int main()
{
char x;
cin>>x;
x=tolower(x);
cout<
} |
|
|
| alanzo |
on second thought... do this... one if input == '.' one if input == frequency and one if input == any letter
that is, first create an array of 26 structs.. one for each letter with the letter in the string part and a 0 for the int part
then take the input and do a tolower(input);
then have
if(input == ".") { do this }
else if(input == "frequency" { do this }
else { search for input in the array and increment it,
or if it's not in the array, then it's not a valid character, cout error msg }
simple problem solving.. gotta love computer science ;) |
|
|
| Four_On_Four-er |
My C++ is very rusty, but as long as you go character by character on the sentence string, you could possibly write a for loop that tests the numbers on an ASCII reference:
// Let's start with the uppercase letters
for(char_number == 65 ; char_number <= 90 ; char_number++)
{
if(sentence[char_number] == ASCII(char_number)) //write function
{
// increment the specific array triggered by char_number
// Maybe something like ALPHABET[char_number]++;
}
}
// Now for the lowercase letters
for(char_number == 97 ; char_number <= 122 ; char_number++)
{
if(sentence[char_number] == ASCII(char_number)) // write function
{
// increment the specific array triggered by char_number
// Maybe something like ALPHABET[char_number]++;
}
}
I'm simply illustrating how ASCII values could help.
Like I said, my C++ is rusty. |
|
|
| alanzo |
I was unaware of this ASCII function... very cool!
here is some more info on it.. http://faculty.ed.umuc.edu/~rmetz/H...oi_function.htm
Could just use the tolower() function, like I said, to not require 2 for loops.. just convert it to lowercase before running the ASCII function |
|
|
| ASFSE |
| thanks for all the help guys. |
|
|
| Four_On_Four-er |
No no... I said "//write function"...
I don't think the ASCII function exists... you'll have to give me some time on how to write it.
Actually, you may NOT have to write it, or do anything with that. If all we are doing is comparing numerical values of characters, it may be pointless. |
|
|
| ASFSE |
ok so i've gotten the program to count the letters entered by using a very lengthy switch, wondering if there was an easier way to write it out? also, any tips for outputing index's that have a value greater than 0? i might be able to figure that part out if i could simplify my existing code...
#include
#include
using namespace std;
int main()
{
int alphabet[27] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
char letter;
bool display;
int n;
cout << "Enter a sequence of characters (end with '.'):";
while (letter != '.'){
cin >> letter;
switch (letter)
{
case 'a' : alphabet[1]++;
true;
break;
case 'b' : alphabet[2]++;
true;
break;
case 'c' : alphabet[3]++;
true;
break;
case 'd' : alphabet[4]++;
true;
break;
case 'e' : alphabet[5]++;
true;
break;
case 'f' : alphabet[6]++;
true;
break;
case 'g' : alphabet[7]++;
true;
break;
case 'h' : alphabet[8]++;
true;
break;
case 'i' : alphabet[9]++;
true;
break;
case 'j' : alphabet[10]++;
true;
break;
case 'k' : alphabet[11]++;
true;
break;
case 'l' : alphabet[12]++;
true;
break;
case 'm' : alphabet[13]++;
true;
break;
case 'n' : alphabet[14]++;
true;
break;
case 'o' : alphabet[15]++;
true;
break;
case 'p' : alphabet[16]++;
true;
break;
case 'q' : alphabet[17]++;
true;
break;
case 'r' : alphabet[18]++;
true;
break;
case 's' : alphabet[19]++;
true;
break;
case 't' : alphabet[20]++;
true;
break;
case 'u' : alphabet[21]++;
true;
break;
case 'v' : alphabet[22]++;
true;
break;
case 'w' : alphabet[23]++;
true;
break;
case 'x' : alphabet[24]++;
true;
break;
case 'y' : alphabet[25]++;
true;
break;
case 'z' : alphabet[26]++;
true;
break;
}
}
} |
|
|
|
|