|
c++ hw(off topic) (pg. 2)
|
View this Thread in Original format
| alanzo |
You're not using a struct in that.. you're not keeping track of the letters.. just the number frequency.. the point of using a struct was so you could keep track of both.
Also, using an ascii to int function as explained earlier will eliminate the need for such a lengthy if chain |
|
|
| dj_kane |
| just give up :tongue2 |
|
|
| ASFSE |
| quote: | Originally posted by alanzo
You're not using a struct in that.. you're not keeping track of the letters.. just the number frequency.. the point of using a struct was so you could keep track of both.
Also, using an ascii to int function as explained earlier will eliminate the need for such a lengthy if chain |
ah, i'm not too clear on struct usage...ya i should just give up lol this is hurtin my head... |
|
|
| alanzo |
It gets 100 times worse... if you can't handle this, you're done for :p
using a struct is easy... look it up in your book or google it
struct entry {
string letter;
int count;
} |
|
|
| ASFSE |
| quote: | Originally posted by alanzo
It gets 100 times worse... if you can't handle this, you're done for :p
using a struct is easy... look it up in your book or google it
struct entry {
string letter;
int count;
} |
lol ya, i just wish i had takin this class in-person rather than online, i'm really regreting it now. |
|
|
| ASFSE |
| quote: | Originally posted by 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 ;) |
hehe, i didnt see this post!! good info here, one question i have tho, is you say create an array of 26 structs for each letter...im not quite sure what you mean by that...creat an actual 26 diffrent structsfor the letters, or...? sorry i'm just retarded when it comes to word comprehension!:D |
|
|
| alanzo |
a struct consists of any number and type of variables that you desire... and since arrays are a templated data structure, you can have an array of whatever you want
so you're going to create a struct that consists of a single string and a single int... this is like creating a variable type... and you're going to have an array of 26 copies of that variable type
so array[0] will have struct 0
array[1] will have struct 1
etc etc.. |
|
|
| ASFSE |
ok so obviously im missing somthing here, i keep getting a compiler error. im just trying to get something simple to work. alanzo i really appreciate the help big time!!!:
#include
#include
#include
using namespace std;
struct entry {
string letter;
int count;
};
int main()
{
cout << "Enter a sequence of characters (end with '.'):";
if (entry == '.') {
cout << "success";
}
system("PAUSE");
return 0;
} |
|
|
| azndragon0613 |
| Whoa this is a tight thread. Sorry I can't help but just wanted to say...I failed my AP Computer Science exam....:( |
|
|
| alanzo |
| msg me on AIM if you want... TeknoOdysee |
|
|
| halo |
Oh! My! God!...
All I've read in this thread is so way off any cleverness. Sorry to say that. :toothless
Just some things to get you started:
the struct needs to contain:
one long entry for endless counting
one char entry for uselessly holding exact one charakter
create an array with 25 entries
create one endless while(true)-loop
-> test for (char)input == '.'
--> break loop if true
// have a look at the ASCII-table to understand the following
-> if (input > 65 && input < 123)
--> input -= 65; // sets 'A' to 0
--> input &= 31; // wraps 'a' to 0
-> if (input < 26)
-> array[input].counter ++;
output your array
There you go...
So what do we do? First we understand that the input character is nothing more than a BYTE value mapped to the ASCII table. If the input is in the range where alphanumerics are mapped, we ramap this to have our alphabet started with index 0. ASCII defines 'A' to be byte-value 65, 32 rows later the lower case alphabet starts. This is really cool as when we use only the lowest 5 bits of our remapped index, 'a' gets the exact same value as 'A'. Now all we need is an array containing 26 counters starting with index 0 that is by now the same as out value for 'a','A'... so all we need to do is incement the array element indexed by our remapped input.
The output can be calculated by the same logic: cycle throught your 26 array elements, add 65 to the current index and interpret as character, then output. (so no real need to remember the counted character in the array... bu as the task said so :rolleyes: ) |
|
|
| ASFSE |
yo halo, thanks for the help, but i finally figured out a way to do it(finally after hours of in around with it)
but here is my final code...any suggestions to improve it would be cool. it's rather sloppy and it doesnt sort by frequency, and i dont think it works if u input numbers...anyway check it!
#include
#include
#include
#include
using namespace std;
int main()
{
char input;
int letter[25] = {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};
int index;
int numItems;
int count;
char finalLetter;
cout << "Enter a sequence of characters (end with '.'):";
while (input != '.'){
cin >> input;
index = (tolower(input)) - 97;
if (index < 26 && index >= 0){
letter[index]++;
}
}
for (count = 0; count < 26; count++){
if(letter[count] != 0){
if (count + 97 > 96 && count + 97 < 122){
finalLetter = (count + 97);
cout << endl;
cout << finalLetter << " occured " << letter[count] << " times " << endl;
cout << endl;
}
}
}
system("PAUSE");
return 0;
} |
|
|
|
|