CS 133U
150 POINTS
DUE announced in class, check web
Purpose: To implement functions and the switch statement in your program.
The switch structure will work so that the user will make a selection from a well designed menu, offering the user an option to enter any of your 7 (seven) math functions.
Problem:
You are to implement in your "calculator" program, functions (preferably value returning and passing arguments but not required) for each math section of your program. So, the Addition, subtraction, etc. will be a function. Also, you will add the switch control structure as part of your menu selection so that the user can select which operation he/she wants to perform and go to that procedure or function on your program. The user will be able to go to any section of your program with a main menu such as the one below.
You will have your program setup similar to:
switch (choice)
'a' : call your add function/procedure.
's' : call your subtractions/procedure.
.
.
'x': out of the programThink about what implementation would be most effective, reducing the size of your current code. Such as, if the reading of the input values should take place inside of the value returning function or inside the switch statement. Avoid repetition of code, meaning that if you see parts of code that repeat on your program, most likely you will use functions. For instance the feedback for correctness of the user's answer " you got it right x ? x is ?" it will repeat in several parts of your program, so this could be a function call.
You will have the menu in a loop so that the user will have the choice to do as many calculations as he/she wishes by making the selection on the menu, this can be implemented with a while loop. So, every time that the user does an addition for example. It will go back to the main menu and allow the user to make a new selection. (do while or while loop)The user will perform the addition for instance, then the result will be displayed and the menu would show up on the screen again for another choice until user selects x to exit.
Your code should be much shorter than the previous assignment, if you notice that your code is longer, it probably means that you are not making enough use of functions on your program.
Here are some examples and links to help you with this assignment. DO NOT WAIT, start on it as soon as possible, explore the links, try out mini programs, etc.
Extra point: Add a string. Ask the user for his/her name and when you give answer feedback the user will see it such as:
Welcome Jim to the math drill....
Good job Jim, you got it right ...
Submit:
- Use proper style, well documented (with lots of comments on the various sections of the program, variables, etc.). Do not send an attachment, just copy/paste your code in the body of your email message. Use the subject CS133U - YOUR NAME - Assignment 4
- Submit a hard copy of your code with your name/date/assignment number on the header of your program as explained in class.
Sample Code
/* Name:Walter Morales
Date:/xx/xx/xx
Compiler: TC Lite
Program:This program shows a simple interaction using a do while
loop and the switch statement.
For your assignment, you need to add the additional functions
required by the assignment.
Make sure to add an intro so the user knows what the program will
do, limitations, etc.
Do not forget the other feature that you need to implement
on this program.
*/
#include <stdio.h>
#include <conio.h> // for clrsrc(), getch()
void Add(); // function prototype
int main()
{
int mychoice; // user selection
clrscr();
do
{
printf (" Choose option and press enter \n");
printf (" 1. Add \n");
printf (" 2. quit \n\n");
scanf ("%d", &mychoice);
switch (mychoice)
{
case 1: {
Add();
break;
}
} //switch
} while ((mychoice != 2));
return 0;
}
void Add()
{
int num1; // First number for addition
int num2; // Second number for addition
printf ("enter number 1 ");
scanf ("%d", &num1);
printf ( "enter number 2 ");
scanf ("%d", &num2);
printf ("%d", num1 + num2) ;
printf ("\n Press any key to continue \n");
getch();
clrscr();
}