UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING CMPE13/L: INTRODUCTION TO PROGRAMMING IN C SPRING 2013 Lab 1 - Calculatr Intrductin Reading Cncepts In this lab yu will be writing yur first prgram frm scratch. This prgram will read user input, perfrm mathematical calculatins, and then return the result t the user. It will rely n knwledge frm utside f class such as data types and printf() & scanf(), and s pay clse attentin t the reading list belw. This lab will als intrduce declaring, defining, and implementing functins. A brief verview f these cncepts is available in this dcument and there is additinal infrmatin in K&R. K&R All f Chapter 1, 7.4, and Appendix B1.2 Iterative Cde Design handut Style Guidelines handut Prvided Files printf() and scanf() Functins C standard library Iterative Cde Design lab1.c - This file cntains the prgram template where yu will implement a simple calculatr. Calculatr functinality will g within the cmments in main(), functin prttypes that will be implemented in the calculatr will be declared right befre main() with their definitins fllwing main() as demnstrated with the rund() functin stub. Assignment requirements Prgram Requirements This assignment has the fllwing requirements: Welcme the user t yur calculatr prgram with a nice greeting
Prmpt the user fr first a mathematical peratin t perfrm in the frm f a single character, s the user nly needs t type a single character t pass this prmpt. This prmpt shuld als display all peratins that are available t the user. These include the fur basic math peratins (multiplicatin, divisin, additin, subtractin), five additinal peratins (Abslute Value, Celsius, Fahrenheit, Average, Tangent), and ptinally fr extra credit the Rund functin. All f these peratins must handle negative values crrectly. a - Abslute value calculates the abslute value f its argument. c - The Celsius functin treats its argument as a value in degrees Fahrenheit and cnverts it t degrees Celsius. f - The Fahrenheit functin treats its argument as a value in degrees Celsius and cnverts it t degrees Fahrenheit. v The Average functin returns the average f its tw arguments. t - The Tangent functin takes in a value in degrees and calculates the tangent value and returns the result. Extra Credit: r. The Rund functin runds a number twards 0 if its fractinal part is less than 0.5, and runds away therwise. Yu must use yur new knwledge f functins (after reading the rest f the lab manual) t implement all f the nn-basic peratins f yur calculatr: Abslute Value, Fahrenheit t Celsius, Celsius t Fahrenheit, Tangent in degrees, Average (and Rund if implemented) using the functin names given in the Operatins with Functins sectin. The characters given with each peratin shuld be used in yur calculatr t identify the crrespnding peratr. Each f these peratins must be implemented with the described implementatin f functins utlined in the Functins sectin f this lab manual (declaratin, implementatin, and usage). All required calculatins and user input & utput shuld be dne with values f type duble. All functin based calculatins shuld return the result with a return statement. If the peratr is a binary peratr (relies n tw perands), then yur prgram shuld prmpt the user further fr tw perands n which t perfrm the peratin, ne at a time. If the peratr is a unary peratr (relies n nly ne perand), then yur prgram shuld prmpt the user further fr nly ne perand n which t perfrm the peratin. After each perand is written the user shuld be able t press enter and the prmpt will finish. Finally print ut the result f the mathematical peratin alng with what peratin was perfrmed. The fllwing example will suffice fr basic
peratins: Result f (3.25 * 4): 13. Ntice that yu will need tw different printf() frmats fr results that are calculated frm a unary peratr versus thse frm a binary peratr. Unary peratrs will require a frmat that lks mre like Result f -5.3 : 5.3 (that is an example f an abslute value calculatin). Example utput: Result f (4.5 deg->f): 40.099998 Result f (57 deg->c): 13.888889 Result f tan(3.7): 0.0647 Result f rund(5.8): 6.000000 Result f (3 * 3): 9.000000 This is what it lks like in the terminal: Return t prmpting the user fr anther mathematical peratin t perfrm. This shuld result in an infinite lp f prmpting the user fr anther calculatin after displaying the results f the prir calculatin. Yur prgram must use functins t implement the nn-arithmetic peratins: abslute value, Celsius t Fahrenheit, Fahrenheit t Celsius, average, and tangent. NOTE: yu may nt use any functins frm the standard math library save tan() fr the tangent functin. Extra Credit: implementatin f a rund functin alng with it being usable by the user f yur calculatr prgram.
Cde style: Fllw the standard style frmatting prcedures fr syntax, variable names, and cmments. Add the fllwing t the tp f every file yu submit as cmments: Yur name The names f clleagues wh yu have cllabrated with Submissin: Submit lab1.c. Grading This assignment cnsists f 10 pints: 1 Pint - Calculatr displays a greeting message nly nce at the start f executin. 1 Pint - Crrect implementatin f the fur basic mathematical peratins (+,-,*,/). 1 Pint - Calculatr prmpts fr input apprpriately. 4 Pints - Crrect implementatin f the fur functin-based calculatins (1 pint each, with the tw temperature cnversins treated as a single pint). 1 Pint - Calculatr behaves crrectly fr unary peratrs versus binary peratrs. 1 Pint - Calculatr displays an acceptable result message and lps cntinuusly asking fr anther peratr after each calculatin. 1 Pint - Yur prgram fllws the prvided style guidelines and cntains less than 5 errrs. 1 Pint (extra credit) - Crrect implementatin f a rund functin based n the implementatin described belw. Yu will lse pints fr the fllwing: -2: Didn't submit exactly lab1.c -2: Surce files dn t include yur name and cllabratrs as a cmment as the first line f the lab1.c file -2: Warnings displayed n cmpilatin (excluding thse issues abut "duble" datatypes when using scanf()) N credit if lab1.c desn't cmpile printf() and scanf() Yu will be using bth f these functins in yur prgram t interact with the user. This is dne with what is called standard input and utput. Bth f these functins are included within the C standard library that is available t all C prgrams. They are declared in the header file stdi.h. Yu will need t add an include statement t
include the stdi.h standard library header belw the cmment stating "include any additinal libraries here. Example usage f these functins fllws: char g; printf( Type in any character: ); scanf( %c, &g); printf( Yu input %c, g); Please nte the ampersands (&) in frnt f the variables passed as arguments t scanf(). These are very imprtant! Fr the cde that yu're writing yu will need ne befre all f the variable arguments t scanf(). Yu dn't need t knw the details f this right nw and it will be cvered later when we get t Array and Pinters. Fr mre infrmatin abut these tpics refer t chapter 5 f K&R. Nte that scanf() is a little finicky abut hw it handles input. If yu use scanf( %f, &x) t read in a duble and type a number and press Return, nt all f the characters will be prcessed. All f the numbers will end up parsed and placed int the x variable, but the newline character will nt have been prcessed and can be captured by future calls t scanf(). T slve this use scanf( %f%c, &x, &c) (where x is f type duble and c is f type char) s that the Return character is placed int the c variable and will nt end up being prcessed by fllwing calls t scanf(). Nte that there is a cmpiler bug where a warning will be generated if the tken "%f" is used with variable f type duble with scanf(). Yu can safely ignre this warning fr this lab and yu will nt lse credit fr it. There is additinal cmpiler bug where a heap size must be specified when the stdi.h library is used (lk fr the '#include' statement at the tp f the files t check). Future labs will require a heap and s yu'll need t specify nn-zer sizes fr thse, but fr this lab setting it t "0" is fine. This ptin is under "Run" "Set Prject Cnfiguratin..." "Custmize..." "xc16-ld" "Heap size". Functins Declaring functins befre a functin can be used, it must first be declared (just like a variable). These declaratins are als referred t as functin prttypes. They are used t describe everything abut the functin EXCEPT what it actually des (the part in between the curly-braces). These declaratins need t ccur in the surce cde BEFORE the functin is first referenced. This means if yu call a functin in main(), but the functin is implemented after main(), yu ll need t put a functin prttype befre main(). An example f a functin prttype is as fllws: duble SumOf(duble a, duble b); This prttype states that the functin SumOf() takes in tw values f type duble and returns a duble as well. Implementing Functins The definitin f a functin actually defines what a functin des. An example f a functin definitin is as fllws:
duble SumOf(duble p1, duble p2) { return p1 + p2; } This creates a summatin functin that is called with tw variables f type duble. The functin returns a value f type duble that is the sum f the values passed in as its arguments. Using Functins Functins can be used fr varius things, but in this lab all that is necessary t knw is hw t stre the return value f a functin int a variable. This is dne just like string any value int the variable. On the leff-hand side f the assignment peratr is the variable that will hld the value and n the right-hand side is the functin call. An example, f hw this is dne is as fllws: duble result, perand1 = 1, perand2 = 2; result = SumOf(perand1, perand2); With result being the variable hlding the return value f the functin, SumOf() being the functin itself, and perand1 & perand2 are its arguments. Operatins with functins Abslute Value - This can be dne with testing whether r nt the value is psitive and if it is nt then return the psitive value. Nte: Yu CANNOT utilize the abslute value functin frm math.h. Functin shuld be named AbsluteValue() and take in a duble and return a duble. Fahrenheit t Celsius & Celsius t Fahrenheit Cmbined with yur knwledge frm Lab 0 yu shuld be able t implement bth f these calculatins. Functins shuld be named FahrenheitTCelsius() and CelsiusTFahrenheit() and take in a duble and return a duble. Tangent (in Degrees) - This functin can rely n the tangent functin frm the standard math library which uses radians (search/brwse the C30 standard library help t find this functin and the header that declares it). Yu must perfrm the necessary cnversins t receive input in degrees. Fr this functin yu must use the cnstant M_PI defined fr yu at the tp f lab1.c (just utilize the cnstant name like yu wuld use any number). Functin shuld be named Tangent()and take in a duble and return a duble. Average - This functin returns the average f its tw inputs. Functin shuld be named Average()and take in a duble and return a duble. Fr Extra Credit: Rund This functin must rund a number dwn if the decimal value is belw 0.5 and up therwise. Yu will have t think abut
hw this can be dne. One methd utilizes type casting (described in sectin 2.7 f K&R, page 42). Anther wuld be t a while lp that cunts dwn t find the fractinal part. This functin stub has already been created fr yu in lab1.c, s just replace the bdy f that functin. Prgram flw Yur prgram will lp cntinuusly while reading and writing frm the terminal. This cncept is utlined fr yu within the `while (1)` lp in pseud cde belw. The basic utline f yur prgram lks as fllws: Output greeting t the user while (1) get peratr as a char if peratr is invalid set peratr t 0 if peratr is valid (at this pint nt 0) get perand1 if peratr is a binary peratr get perand2 if peratr is additin result <- sum f perands else if peratr is subtractin result <- difference f perands else if peratr is multiplicatin result <- prduct f perands else if peratr is divisin result <- qutient f perands else if peratr is a v result <- average functin else if peratr is an a result <- abslute value functin else if peratr is a c result <- Fahrenheit t Celsius functin else if peratr is an f result <- Celsius t Fahrenheit functin else if peratr is a t result <- tangent functin else if peratr is a r result <- rund functin if peratr is a unary peratr print the result f a unary peratin else print the result f a binary peratin else if peratr is invalid print invalid an invalid peratr message Prgram utput
Example utput fr ne calculatin is given belw. Ding this lab The Iterative Cde Design handut describes a very pwerful way t apprach any prgramming prject. Belw yu will see we have given yu an example methd f cmpleting this lab with the practices described in the Iterative Cde design handut. Remember, it is imprtant t stp and test yur cde fr crrect functinality befre mving nt the next step. Step 1 Display a greeting message. Step 2 Prmpt the user t input a character. Testing wuld invlve printing that character back ut t cnfirm it wrks as expected. Step 3 Cntinuusly prmpt the user fr a character within an infinite lp Testing shuld ech this character ut every lp Step 4 Nw add an invalid peratr checker Checking fr this shuld set yur peratr variable t a standard errr value (-1) if peratr is nt ne f yur valid peratr s (at this pint yu can just use + ). Nw print the peratr if it is nt equal t yur standard errr value, and print an errr message therwise ( Errr, nt a valid peratr ). Step 5 Cntinuusly prmpt the user fr an peratr and tw perands. Step 6 Cntinuusly: Prmpt the user fr an peratr and tw perands. If the user enters a +, calculate the result and print it Step 7 Expand cde t wrk fr all 4 basic peratrs: +, -, /, * Nte: this will require yu d update yur valid peratr checker as well. Step 8 Display the result nicely as the requirements describe. Step 9 Add an peratr fr an abslute value calculatin a.
Add checking fr ne r tw perands. This checking shuld make it s yur prgram nly prmpts fr ne perand when given the abslute value peratr (dn t d the calculatin just print smething t shw it wrks). Step 10 Define an abslute value functin. Test that it wrks with cde. Use test cases: -3, -8.63, 0, and 13.67 printf( %f\n, AbsluteValue(-3)); printf( %f\n, AbsluteValue(-8.63)); printf( %f\n, AbsluteValue(0)); printf( %f\n, AbsluteValue(13.67)); Yur utput shuld be 3, 8.63, 0, and 13.67 crrespndingly. Step 11 Implement the abslute value peratr in yur calculatr by updating yur peratr checkers, and calling the functin in the apprpriate place. Nw yu will als need a new result message with a printf() frmatted t display a calculatin with nly ne perand (by nw yu shuld knw hw t d this with an peratr checker). Step 12 Define an Average functin. Test t see if it wrks with cde. Use Test cases: (55.5, 0), (0.00, -10), (-36.49, 36.49) Yur utput shuld be 27.75, -5, and 0.0 crrespndingly. Step 13 Take ut any tests fr yur Average functin. Implement the Average peratr in yur calculatr. Step 14 Define a Celsius cnversin functin. Test t see that it wrks with cde. Use test cases: 32, -27, 0 Yur utput shuld be 89.599995, -16.599998, and 32 crrespndingly. Step 15 Take ut any test fr yur Celsius cnversin functin. Implement the Celsius cnversin functin in yur calculatr. Step 16 Define a Fahrenheit cnversin functin. Test t see that it wrks with cde. Use test cases 98, -12, 0 Yur utput shuld be 36.666668, -24.444445, and -17.777779 crrespndingly. Step 17 Take ut any tests fr yur Fahrenheit cnversin functin. Implement the Fahrenheit cnversin functin in yur calculatr. Step 18 Define a Tangent in Degrees functin. Test t see it wrks with cde. Use test cases: 57, 1.5, -33, 0 Yur utput shuld be 1.5399, 0.0262, -0.6494, and 0 crrespndingly. Step 19 Take ut any tests fr yur Tangent functin. Implement the Tangent functin in yur calculatr.