UPI Study › Courses › Programming in C › Free lessons › Lesson 4.7 Free with a free account
Workshop: Menu-Driven Programs and Grade Calculators
Programming in C · Chapter 4: Conditional Logic, Decision Structures, and Practical ApplicationsSection 4.7 · lesson 25 of 786 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Programming in C.
Lesson complete
Earn the 3 college credits for Programming in C
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Loop Fundamentals in C →← The switch StatementNext lesson: Loop Fundamentals in C →
In this lesson
Building a menu, a grade calculator and a ticket counter in C.
- Build a menu that reads a choice with scanf and routes it with switch
- Compute a weighted average and map it to a letter with an else-if chain
- Combine a range-based chain with an exact-value switch
- Test programs at every cut-off and the values around it
Free study notes
Get this lesson's notes by email
The key points and 4 practice questions with answers, in your inbox. One email, free.
One email. Unsubscribe in one click.
Transcript
Everything said in the video, section by section (815 words). Read along, or search it.
Workshop Plan
Three Programs for the Arcade
Walk up to any machine with a screen, and it asks the same thing: what do you want to do? Press one for this, two for that. Behind every menu is a decision. And behind every grade report and every ticket price is a pile of rules. In this workshop you build three small programs for the Starlight Arcade: a menu, a grade calculator, and a ticket counter. Let's start building.
Step One
Showing the Menu and Reading a Choice
Program one, the menu. Plan first: a menu has three jobs. Show the choices, read the player's choice, and run the matching action. Step one is only the first two jobs. Four printf lines show the menu, and scanf reads the number the player types. A last line echoes the choice back, just to prove the read worked. Run it, and type two. You chose two. The input arrives. Now it can go somewhere.
Step Two
Routing Choices With a Switch
Step two. The choices are exact numbers, one, two, or three, which is the perfect job for a switch. Delete the echo line, add a token balance of twenty, and route the choice. Every case ends with break. And default answers anything that isn't one, two, or three. Run it with two again. The player started with twenty tokens, bought ten, and now has thirty. Then try a choice that doesn't exist, like nine. Default catches it: sorry, option nine does not exist.
Planning
Weighted Grades From Two Scores
Program two, the grade calculator. The quiz counts for forty percent of the grade, and the exam for sixty. The program reads both scores, and rejects any score outside zero to a hundred. Then it finds the weighted average. A letter depends on a range, so the letter comes from an else-if chain, highest cut-off first. Then a switch on the letter picks a comment, because letters are exact characters.
Worked Example
Guard, Average, Letter and Comment
Here's the code. Right after the read, a guard checks both scores. A bad one prints the rule, and returns before any math is done. Then the average: quiz times zero point four, plus exam times zero point six. The chain tests ninety, then eighty, then seventy, then sixty, and else gives F. And the switch turns the letter into a comment. A switch on the average itself would be refused, since a selector must be a whole number. Run it with a quiz of seventy-eight and an exam of ninety-one. Weighted average, eighty-five point eight. Letter B. Good work. Check it by hand: thirty-one point two, plus fifty-four point six, is eighty-five point eight. At least eighty, below ninety, so B.
Quick Check
A Weak Quiz and a Perfect Exam
Quick check. A student scores fifty on the quiz, and a perfect hundred on the exam. Which letter grade does the program print? Pause and predict. Run it. Fifty times zero point four is twenty, and a hundred times zero point six is sixty. The average is exactly eighty. Eighty is at least eighty, so the letter is B, and the comment is good work. A perfect exam can't rescue a weak quiz.
Planning
Ticket Rules by Age and Day
Program three, the ticket counter. Two sets of rules combine. By age: under three is free, three to twelve pays six dollars, sixty-five and older pays seven, and everyone else pays twelve. By day: Tuesday takes a dollar off, and Saturday and Sunday add two. Free tickets stay free on every day.
Worked Example
An Age Chain Plus a Day Switch
The age rule is a range question, so it's an else-if chain. The day rule is exact day numbers, so it's a switch. Case six is stacked over case seven, so the weekend days share one action. And the if around the switch, price greater than zero, keeps free tickets free. Run it for a seventy-year-old on day six, a Saturday. Seven dollars as a senior, plus two for the weekend: nine dollars.
Quick Check
A Senior on a Tuesday
Quick check. A sixty-five-year-old comes in on day two, a Tuesday. What price does the program print? Pause and predict. Run it. It prints six dollars. Sixty-five isn't under three, and isn't twelve or under. But it is at least sixty-five, so the base price is seven. Day two is Tuesday, so a dollar comes off. Six dollars.
Edge Testing
Every Cut-Off Test Passes
That's exactly how Sam tests: right at the edges. Age twelve on a Tuesday gives five dollars. Age thirteen gives eleven. Ages two, three, sixty-four and sixty-five, and days one, two, five, six and seven. The rules are the answer key. Priya smiled. All of Sam's edge tests passed on the first run, which had never happened before.
Key Takeaways
Menus and Rule Calculators in Review
To sum up. A menu shows choices, reads one with scanf, and routes it with a switch, with default for anything else. Rules that depend on ranges become an else-if chain, highest cut-off first, and exact values become a switch. A guard rejects bad input before any math. And you test every cut-off, plus the values around it. Something to try. Add a fourth menu option, Leave, that prints a goodbye line. It takes one new printf, and one new case.
About Programming in C at UPI Study
Programming in C (Computer Science 240) is a self-paced online course of 78 video lessons in 11 chapters, from pseudocode and your first program to data types, decisions, loops, functions, arrays, strings, pointers, files, structures, dynamic memory and linked lists. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: C vs C++
Why take it here
Why students take Programming in C with UPI Study
Where the credit goes
Universities that work with NCCRS
Programming in C carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Workshop: Menu-Driven Programs and Grade Calculators
How do you write a menu-driven program in C?
How do you calculate a weighted grade average in C?
Why use an else-if chain for letter grades but a switch for the comment?
How can a switch handle Saturday and Sunday with one block of code?
About the Programming in C course
How many college credits is Programming in C?
How much does Programming in C cost and what is included?
Which universities accept UPI Study credits?
How long does Programming in C take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Programming in C.
78 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
3 credits · NCCRSEnroll →