IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 3: [Algorithms] – Linear Search, Binary Search, Bubble Sort, Selection Sort, Recursion, Merge Sort › Code failed to compile
- This topic is empty.
Viewing 1 post (of 1 total)
-
AuthorPosts
-
November 9, 2022 at 1:12 pm #924
While the program is running and giving valid ouput for at least some inputs, with Check50, it shows for 🙁 plurality compiles
Cause
code failed to compileThis is the code:
[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 <string.h> #define MAX 9 typedef struct { int vote; string name; } candidate; candidate candidates[MAX]; int maxcandidate; int noofcandidate; bool vote (string nameentered); void print_winner(void); int main (int argc, string argv[]) { if(argc < 4) { printf("less than 4 and so invalid"); return 0; } if(argc > 9) { printf("more than 9 and so invalid"); return 0; } noofcandidate = argc - 1; for (int c = 0; c < noofcandidate; c++) { candidates[c].name = argv[c+1]; candidates[c].vote = 0; } int noofvoters = get_int("Number of voters: "); printf("Number of voters: %i\n", noofvoters); for (int t = 0; t < noofvoters; t++) { string nameentered = get_string("Vote: "); if (!vote(nameentered)) { printf("invalid name"); return 1; } } print_winner(); } bool vote (string nameentered) { int z; for (z = 0; z < noofcandidate; z++) { if (strcmp(nameentered, candidates[z].name) == 0) { candidates[z].vote = candidates[z].vote + 1; return true; } } return false; } void print_winner(void) { int maxvote = 0; for (int z = 0; z < noofcandidate; z++) { if (candidates[z].vote > maxvote) { maxvote = candidates[z].vote; } } for (int z = 0; z < noofcandidate; z++) { if (maxvote == candidates[z].vote) { printf ("%s\n", candidates[z].name); } } return; }
[/dm_code_snippet]
Reply
https://edstem.org/us/courses/176/discussion/2117204?comment=4860019
[learn_press_profile]
-
AuthorPosts
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.