alan2 Δημοσ. 22 Φεβρουαρίου 2012 Δημοσ. 22 Φεβρουαρίου 2012 (επεξεργασμένο) προσπαθω να φτάξω τη συνάρτηση που θα υπολογίζει το μέγεθος λέξεων στο πίνακα char document_table[][M] και θα δημιουργεί ιστόγραμμα συχνοτήτων με τα μεγέθη τους.Εχω γράψει >void word_histogram(char document_table[][M],int size){ int c,x=0, freq[9]; for(c=0;c<=9;c++)freq[c]=0; char *pch; pch=strtok(document_table[M]," "); for (x=0;x<=5;x++) while (pch!=NULL) { pch=strtok(NULL," "); c=strlen(pch); freq[c]++; } printf("word zize.......frequency"); for(c=1;c<=9;c++){ printf("%d.....",freq[c]); for(x=0;x<=freq[c];x++) printf("*"); printf("/n"); } } την καλώ με word_histogram(document_table, size); απο main() αλλα δεν γίνεται κατι... επισης void initialize (char document_table[][M],int size ) είναι ο τρόπος με τον οποιο πρέπει να δηλωθεί η συνάρτηση σύμφωνα με την εκφώνηση της άσκησης.οπου size το πληθος των γραμων του πινακα. N o αριθμός των αλφαριθμητικών Μ-1 το μεγιστο μήκος καθενός τους. Επεξ/σία 23 Φεβρουαρίου 2012 από alan2
virxen75 Δημοσ. 22 Φεβρουαρίου 2012 Δημοσ. 22 Φεβρουαρίου 2012 για αρχή for(c=0;c<=9;c++) ----> for(c=0;c<9;c++) και for(c=1;c<=9;c++){ ---->for(c=0;c<9;c++){ και αυτό δεν είναι ασφαλές---------->c=strlen(pch); freq[c]++; για c>=9 το for τι ακριβώς κάνει?----->for (x=0;x<=5;x++)
Stavros_Ribo Δημοσ. 23 Φεβρουαρίου 2012 Δημοσ. 23 Φεβρουαρίου 2012 προσπαθω να φτάξω τη συνάρτηση που θα υπολογίζει το μέγεθος λέξεων στο πίνακα char document_table[][M] και θα δημιουργεί ιστόγραμμα συχνοτήτων με τα μεγέθη τους.Εχω γράψει > void word_histogram(char document_table[][M],int size){ int c,x=0, freq[9]; for(c=0;c<=9;c++)freq[c]=0; char *pch; pch=strtok(document_table[M]," "); for (x=0;x<=5;x++) while (pch!=NULL) { pch=strtok(NULL," "); c=strlen(pch); freq[c]++; } printf("word zize.......frequency"); for(c=1;c<=9;c++){ printf("%d.....",freq[c]); for(x=0;x<=freq[c];x++) printf("*"); printf("/n"); } } την καλώ με word_histogram(document_table, size); απο main() αλλα δεδ γίνεται κατι... Αν κι έχεις κάνει κάποια σοβαρά λάθη, εννοείς κάτι τέτοιο? : >void word_histogram(char document_table[][M],int size){ int c,x=0, freq[9],i; char *pch; pch=NULL; for(c=0;c<9;c++) freq[c]=0; for(i=0;i<size;i++){ pch=strtok(document_table[i]," "); while (pch!=NULL) { c=strlen(pch); freq[c-1]++; pch=strtok(NULL," "); } } printf("word zize.......frequency"); for(c=0;c<9;c++){ printf("%d.....",freq[c]); for(x=0;x<=freq[c];x++) printf("*"); printf("/n"); } }
alan2 Δημοσ. 23 Φεβρουαρίου 2012 Μέλος Δημοσ. 23 Φεβρουαρίου 2012 το for τι ακριβώς κάνει?----->for (x=0;x<=5;x++) o πινακας char document_table[][M] κανονικά εχει 5 γραμμες, το while loop πρεπει να εκτελείται μια φορά για καθε γραμμή νομίζω
imitheos Δημοσ. 23 Φεβρουαρίου 2012 Δημοσ. 23 Φεβρουαρίου 2012 για αρχή for(c=0;c<=9;c++) ----> for(c=0;c<9;c++) και for(c=1;c<=9;c++){ ---->for(c=0;c<9;c++){ και αυτό δεν είναι ασφαλές---------->c=strlen(pch); freq[c]++; για c>=9 το for τι ακριβώς κάνει?----->for (x=0;x<=5;x++) Μπράβο στην υπομονή σου Εγώ μόλις είδα ότι γείωσε τις προτάσεις που του κάναμε στο άλλο νήμα και συνεχίζει το άθλιο indentation, ούτε κοίταξα τον κώδικα.
migf1 Δημοσ. 23 Φεβρουαρίου 2012 Δημοσ. 23 Φεβρουαρίου 2012 Δες αν σε βοηθάει κι αυτό το ποστ: http://www.insomnia.gr/topic/424639-%ce%b5%cf%85%ce%ba%ce%bf%ce%bb%ce%b7-%ce%ac%cf%83%ce%ba%cf%83%ce%b7-%cf%83%cf%84%ce%b7%ce%bd-c/page__view__findpost__p__4370574 (ή/και το νήμα γενικότερα). ...Εγώ μόλις είδα ότι γείωσε τις προτάσεις που του κάναμε στο άλλο νήμα και συνεχίζει το άθλιο indentation, ούτε κοίταξα τον κώδικα... Ομοίως κι εγώ (και είναι κρίμα, γιατί θα μπορούσε να είχε επωφεληθεί)... εξού και το παραπάνω link που είναι γενικότερο από ότι η απορία του
alan2 Δημοσ. 24 Φεβρουαρίου 2012 Μέλος Δημοσ. 24 Φεβρουαρίου 2012 κατι σαν αυτο θελω πιο κατω να κανω, αλλα να τρεχει οταν καλείται να κανει ιστογραμμα που να δειχνει τη συχνότητα των λέξεων αναλογα με το μεγεθός τους πχ 1....** 2....***** void initialize (char document_table[][M],int size ) είναι ο τρόπος με τον οποιο πρέπει να δηλωθεί η συνάρτηση σύμφωνα με την εκφώνηση της άσκησης.οπου size το πληθος των γραμων του πινακα. N o αριθμός των αλφαριθμητικών Μ-1 το μεγιστο μήκος καθενός τους. >void word_histogram(char document_table[][M],int size){ int c,x=0; int freq[9]={0}; char *pch; printf("klithike"); for (x=0;x<size;x++) { pch=strtok(document_table[x]," "); while (pch!=NULL) { pch=strtok(NULL," "); c=StringLength(pch); freq[c]++; } } printf("word zize.......frequency"); for(c=1;c<9;c++){ printf("%d...........%d",c,freq[c]); for(x=0;x<freq[c];x++) printf("*"); printf("\n"); } }
migf1 Δημοσ. 25 Φεβρουαρίου 2012 Δημοσ. 25 Φεβρουαρίου 2012 (επεξεργασμένο) > #include <stdio.h> #include <stdlib.h> #include <string.h> #define HISTOLINE "*************************************************************" #define MAXLINES 5 #define LNLEN (100+1) #define MAXWORDS 100 #define WLEN (25+1) #define SEPAR " \t.,!;?" /*********************************************************//** * ************************************************************/ int wlookup( const char *w, char words[MAXWORDS][WLEN] ) { int i; if ( !w || !*w || !words ) return -1; for (i=0; i < MAXWORDS; i++) if ( !strcmp(w, words[i]) ) return i; return -1; } /*********************************************************//** * ************************************************************/ int main( void ) { unsigned short int i = 0U, wlast = 0U; char text[MAXLINES][LNLEN] = { "did the hen make the egg or did the egg make the hen?", "that is the question!", "or is it really?", "let the science speak.", "do not hold your breath, though." }; char words[ MAXWORDS ][ WLEN ] = {{'\0'}}; unsigned short int wfreqs[ MAXWORDS ] = {0}; for (i=0, wlast=0; text[i] && i < MAXLINES; i++) { char *w = NULL; char line[LNLEN] = {'\0'}; strncpy( line, text[i], LNLEN-1 ); for (w=strtok(line, SEPAR); w && wlast < MAXWORDS; w=strtok(NULL, SEPAR) ) { int wi = wlookup(w, words); if ( wi == -1 ) { wi = wlast++; strncpy( words[wi], w, WLEN-1 ); } wfreqs[wi]++; } } for (i=0; *words[i] && i < MAXWORDS; i++) { printf( "%s %*c (%d) %.*s\n", words[i], WLEN - strlen(words[i]),' ', wfreqs[i], wfreqs[i],HISTOLINE ); } exit( EXIT_SUCCESS ); } > did (2) ** the (6) ****** hen (2) ** make (2) ** egg (2) ** or (2) ** that (1) * is (2) ** question (1) * it (1) * really (1) * let (1) * science (1) * speak (1) * do (1) * not (1) * hold (1) * your (1) * breath (1) * though (1) * Επεξ/σία 26 Φεβρουαρίου 2012 από migf1
virxen75 Δημοσ. 26 Φεβρουαρίου 2012 Δημοσ. 26 Φεβρουαρίου 2012 εγώ πάντως θα το έγραφα κάπως έτσι > #include <stdio.h> #include <string.h> #define M 50//maximum input length #define LINES 5 void word_histogram(char document_table[][M],int size){ int c,x=0; int freq[M]={0};//maximum word length=M char *pch; for (x=0;x<size;x++){ pch=strtok(document_table[x]," "); while (pch!=NULL){ c=strlen(pch); freq[c]++; pch=strtok(NULL," "); } } printf("%15s %10s","word zize","frequency\n"); for(c=1;c<M;c++){//0 as word length is not valid if (freq[c]!=0){ printf("%10d %10d ",c,freq[c]); for(x=0;x<freq[c];x++) printf("*"); printf("\n"); } } } int main(void){ char document[LINES][M]={"hello world", "money exists", "the sun shines", "c c++ java", "vb4 vb6 visual studio", }; word_histogram(document,LINES); getchar(); return 0; }
migf1 Δημοσ. 26 Φεβρουαρίου 2012 Δημοσ. 26 Φεβρουαρίου 2012 εγώ πάντως θα το έγραφα κάπως έτσι... Δοκίμασε να τυπώσεις τα περιεχόμενα του document στον κώδικά σου μετά την κλήση της word_histogram() ΥΓ. Εγώ μάλλον δεν κατάλαβα σωστά τι αντιπροσωπεύουν τα νούμερα κι οι αστερίσκοι, τα έχω βάλει να δείχνουν και τα 2 τη συχνότητα της λέξης.
virxen75 Δημοσ. 26 Φεβρουαρίου 2012 Δημοσ. 26 Φεβρουαρίου 2012 Δοκίμασε να τυπώσεις τα περιεχόμενα του document στον κώδικά σου μετά την κλήση της word_histogram() σωστός! > #include <stdio.h> #include <string.h> #define M 50//maximum input length #define LINES 5 void word_histogram(char document_table[][M],int size){ int c,x=0; int freq[M]={0};//maximum word length=M char *pch; char document[size]; for (x=0;x<size;x++){ strcpy(document,document_table[x]); pch=strtok(document," "); while (pch!=NULL){ c=strlen(pch); freq[c]++; pch=strtok(NULL," "); } } printf("%15s %10s","word zize","frequency\n"); for(c=1;c<M;c++){//0 as word length is not valid if (freq[c]!=0){ printf("%10d %10d ",c,freq[c]); for(x=0;x<freq[c];x++) printf("*"); printf("\n"); } } } int main(void){ char document[LINES][M]={"hello world", "money exists", "the sun shines", "c c++ java", "vb4 vb6 visual studio", }; word_histogram(document,LINES); getchar(); return 0; }
migf1 Δημοσ. 26 Φεβρουαρίου 2012 Δημοσ. 26 Φεβρουαρίου 2012 Είχα κι εγώ ένα θεματάκι στον δικό μου κώδικα, με ένα strcpy() που έπρεπε να είναι μέσα στο if, για να μην διπλο-τριπλο-nπλο () αντιγράφει συνεχώς λέξεις μέσα στον words. ΥΓ. Τελικά τα αστέρια και τα νούμερα τι πρέπει να απεικονίζουν;
virxen75 Δημοσ. 26 Φεβρουαρίου 2012 Δημοσ. 26 Φεβρουαρίου 2012 ΥΓ. Τελικά τα αστέρια και τα νούμερα τι πρέπει να απεικονίζουν; -βρίσκεις τα μήκη(strlen) των λέξεων που διαβάζεις από τον πίνακα (π.χ. στο δικό μου υπάρχουν λέξεις με 1,3,4,5,6 χαρακτήρες.) -μετα βρίσκεις πόσες φορές υπάρχει στον πίνακα μια λέξη με 1,3,4,5,6 χαρακτήρες (π.χ. στο δικό μου: υπάρχει 1(frequency) λέξη με 1(strlen) χαρακτήρα, υπάρχoυν 5(frequency) λέξεις με 3(strlen) χαρακτήρες, υπάρχει 1(frequency) λέξη με 4(strlen) χαρακτήρες, υπάρχουν 3(frequency) λέξεις με 5(strlen) χαρακτήρες, υπάρχουν 4(frequency) λέξεις με 6(strlen) χαρακτήρες ) -και τέλος τα αστεράκια για εξάσκηση όσα και το frequency
migf1 Δημοσ. 26 Φεβρουαρίου 2012 Δημοσ. 26 Φεβρουαρίου 2012 Αχα, άρα ένας ακόμα πίνακας με συχνότητα μηκών... > #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXLINES 5 #define LNLEN (100+1) #define MAXWORDS 100 #define WLEN (25+1) #define SEPAR " \t.,!;?" #define NPUTCHAR(n,c) do { while (n--) putchar(c); putchar('\n'); } while(0) /*********************************************************//** * ************************************************************/ int wlookup( const char *w, char words[MAXWORDS][WLEN] ) { int i; if ( !w || !*w || !words ) return -1; for (i=0; i < MAXWORDS; i++) if ( !strcmp(w, words[i]) ) return i; return -1; } /*********************************************************//** * ************************************************************/ int main( void ) { unsigned short int i = 0U, wlast = 0U; char text[MAXLINES][LNLEN] = { "did the hen make the egg or did the egg make the hen?", "that is the question!", "or is it really?", "let the science speak.", "do not hold your breath, though." }; char words[ MAXWORDS ][ WLEN ] = {{'\0'}}; unsigned short int wfreqs[ MAXWORDS ] = {0}; unsigned short int wlenfreqs[ WLEN ] = {0}; /* fill the word & word-length frequency arrays */ for (i=0U, wlast=0U; text[i] && i < MAXLINES; i++) { char *w = NULL; char line[LNLEN] = {'\0'}; strncpy( line, text[i], LNLEN-1 ); for (w=strtok(line,SEPAR); w && wlast < MAXWORDS; w=strtok(NULL,SEPAR) ) { int wi = wlookup(w, words); if ( -1 == wi ) { wi = wlast++; strncpy( words[wi], w, WLEN-1 ); } wfreqs[wi]++; wlenfreqs[ strlen(w) ]++; } } /* print word-length frequencies // could be a re-usable function */ for (i=1U; i < WLEN; i++) { if ( 0U == wlenfreqs[i] ) continue; printf( "%2hd-letter words:\t%-3hd", i, wlenfreqs[i] ); NPUTCHAR( wlenfreqs[i], '*'); } putchar('\n'); /* print word frequencies // could be a re-usable function */ for (i=0; *words[i] && i < MAXWORDS; i++) { printf( "%s %*c (%hd) ", words[i], WLEN - strlen(words[i]),' ', wfreqs[i] ); NPUTCHAR( wfreqs[i], '*' ); } exit( EXIT_SUCCESS ); } > 2-letter words: 6 ****** 3-letter words: 14 ************** 4-letter words: 5 ***** 5-letter words: 1 * 6-letter words: 3 *** 7-letter words: 1 * 8-letter words: 1 * did (2) ** the (6) ****** hen (2) ** make (2) ** egg (2) ** or (2) ** that (1) * is (2) ** question (1) * it (1) * really (1) * let (1) * science (1) * speak (1) * do (1) * not (1) * hold (1) * your (1) * breath (1) * though (1) *
Προτεινόμενες αναρτήσεις
Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε
Πρέπει να είστε μέλος για να αφήσετε σχόλιο
Δημιουργία λογαριασμού
Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!
Δημιουργία νέου λογαριασμούΣύνδεση
Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.
Συνδεθείτε τώρα