Workshop: Menu-Driven Programs and Grade Calculators

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

← The switch StatementNext lesson: Loop Fundamentals in C →

Earn 3 college credits for Programming in CRecommended for credit by NCCRS · Computer Science 240 · self-paced · one payment, lifetime access

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.

Compare: C vs C++

Why take it here

Why students take Programming in C with UPI Study

How programs work close to the machineProgramming in C teaches how a program uses memory: variables and data types, control flow, functions, arrays and strings, pointers, files, structures and dynamic memory. C is the base for operating systems, embedded devices and many computer-science courses.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

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.

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?
A menu program shows the choices with printf, reads the user's number with scanf, and routes it with a switch. Each case performs one action and ends with break, and default answers any number that is not on the menu, such as 9. Building in steps helps: first show the menu and echo the choice back, then replace the echo with the switch.
How do you calculate a weighted grade average in C?
Multiply each score by its weight and add the results. With a quiz worth 40 percent and an exam worth 60 percent, the average is quiz * 0.4 + exam * 0.6. A quiz of 78 and an exam of 91 give 31.2 + 54.6 = 85.8, which is a B. Store the result in a double, and reject scores outside 0 to 100 before doing any math.
Why use an else-if chain for letter grades but a switch for the comment?
A letter grade depends on a range of averages, and switch cannot test ranges or doubles, so an else-if chain checks 90, then 80, 70 and 60, highest cut-off first. Once the letter exists, it is an exact character, which suits a switch. A switch on the average itself would be rejected, because a selector must have a whole-number type.
How can a switch handle Saturday and Sunday with one block of code?
Stack the case labels with no code between them, as in case 6: case 7:, followed by the shared action and a break. Both values land in the same block. In a ticket counter, that block can add $2 for the weekend, while case 2 takes $1 off for Tuesday. Wrapping the switch in if (price > 0) keeps free tickets free on every day.

About the Programming in C course

How many college credits is Programming in C?
Programming in C (Computer Science 240) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Programming in C cost and what is included?
$250, paid once, with lifetime access: all 78 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Programming in C take?
It is self-paced with no deadlines. The 78 lessons total about 9 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

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.

Programming in C
3 credits · NCCRS
Enroll →