IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2: [Arrays] – Functions, Variable and Scope, Debugging, Arrays, and Command Line Arguments › Problem computing Coleman-Liau Index/Getting the output in decimal form with float
Tagged: arrays, c, float type, Integer divison
- This topic is empty.
-
AuthorPosts
-
January 1, 2022 at 7:53 am #83
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#include <stdio.h> #include <cs50.h> #include <ctype.h> #include <string.h> int main(void) { string enterword = get_string("input: "); int t = strlen(enterword); printf("%i\n", t); int counter = 0; for(int i = 0; i<= t; i++) { if (enterword[i] == '.'|| enterword[i] == '!' || enterword[i] == '?'){ counter = counter + 1;} } printf("number of lines %i\n",counter); int wordcounter = 1; for (int i = 0; i<= t; i++) { if (enterword[i] == ' ' ){ wordcounter = wordcounter + 1; } } printf("total words %i\n", wordcounter); int lettercounter = 0; for (int i = 0; i <= t; i++){ if (isalpha (enterword[i])) { lettercounter++; } } printf("number of letters %i\n", lettercounter); int L = (lettercounter / wordcounter) * 100; printf("L index: %i\n",L); int S = (counter / wordcounter) * 100; printf("S index: %i\n",S); int index = (0.0588 * L) - (0.296 * S) - 15.8; printf("CI: %i\n",index); }
[/dm_code_snippet]
For the above code, while I could see the number of letters, words, sentences computed correctly, but not Coleman-Liau Index. Instead of Grade 5, it is showing Grade I (CI) in this example: https://www.canva.com/design/DAE0L-QIgQ0/M19PsHu5Di-MsEyNgmT-WQ/view?utm_content=DAE0L-QIgQ0&utm_campaign=designshare&utm_medium=link&utm_source=sharebutton
Reply
https://edstem.org/us/courses/176/discussion/978225?comment=2213954
Getting the output in decimal form with float
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
int lettercounter(string enteredword); int wordcounter(string enteredword); int sentencecounter(string enteredword); int main(void) { string enteredword = get_string("enter word:"); int y = lettercounter(enteredword); printf("number of alphabetical characters: %i\n", y); int f = wordcounter(enteredword); printf("number of words: %i\n", f); int s = sentencecounter(enteredword); printf("number of sentences: %i\n",s); float L = ((float)(y/f)) * 100; printf("average letter per 100 word:%f",L); }
[/dm_code_snippet]
I guess there is something wrong with:
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
float L = ((float)(y/f)) * 100;
[/dm_code_snippet]
Not getting the output in decimal form.
Reply
https://edstem.org/us/courses/176/discussion/1884170?comment=4306771[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.