SlideShare a Scribd company logo
1 of 3
Download to read offline
Exercise #1: Write a C program that contains the following steps (make sure all variables are int).
Read carefully each step as they are not only programming steps but also learning topics that
explain how functions in C really work. Ask the user for a number between 10 and 99. Write an
input validation loop to make sure it is within the prescribed range and ask again if not.
Commenting out the existing coding, write the code to divide a two digit integer number by 10 (pick
any number you want between 10 and 99; hard code it right into the program rather than input it).
Output the result as an integer. Try this with a few different initial numbers. For example, 59 would
give an output of 5. Next write the code to find the remainder when a two digit number (again, hard
code it into your program) is divided by 10 [note that this is done in a single statement]. Output the
result as an integer. Try this with a few different numbers. For example, 59 would give an output of
9. Comment the code from steps 4 and 5 out of your program, and put the coding to find the
number of 10's in a number into a user defined (helper) function named f1. Set the initial number
in a variable named x in the main function and print it out to check that it was set correctly). Your
code will then pass x to a variable named y in f1 - the syntax for the call is of course f1( x ); . (That
is, you are copying the value in the argument variable x into the parameter variable y.) f1 should
return the number of 10's in y. After f1 returns, the main function should print the returned value.
You should see the original number printed from the main function and the returned number of
10's in that original number, also printed from the main function. Try this with a couple of different
initial numbers. Repeat step 6 but this time put the coding of the step 5 into another user defined
(helper) function named f2. Again use y as the name of the parameter in f2; it will not get confused
with the y in f1 - they are declared in separate functions and are thus completely different
variables. (f1 has its own y and f2 has its own y.) f2 should return the remainder when the passed
(copied) number in y is divided by 10. After the setting of the initial number and printing it to check
it, the main function should invoke (call) f2, and after f2 returns the main function should print the
returned value. You should see the original number printed from the main function and the
returned remainder when that original number is divided by 10, also printed from the main
function. Try this with a couple of different initial numbers. Comment out the calls for f1 and f2.
Keep the assignment of a value to x and the print to check that it was properly set. Write a void (no
return value will be given) function f3 which will be passed the address of your initial number (x).
The syntax for the call to this function is, of course f3(&x); The function f3 will have a single
parameter, named x_address, and that parameter must be able to hold the ADDRESS of an
integer value, that address being copied from the &x. Check with you notes Ch 3 slide 7 to see
how this should be declared. By dereferencing an address (a pointer) we are accessing the value
AT that address (either getting it or setting it). Inside f3, dereference the parameter x_address (the
dereference syntax is "*x_address", with no quotes of course) and then (still inside f3) print out the
result of the dereferencing. You will see that you have obtained the value of x, which is in the main
function. After returning, the main function should print out x again. All three prints of x (the original
check in the main, inside f3 after dereferencing, and in the main after returning from f3) will be the
same - all three printfs will be printing the value of the x which is located in the memory area
belonging to the main function. Be sure to label each printing of x with a few words so you can tell
them apart on your output. Modify f3 to add 4 to x ensuring that in the main function you are
printing out the value of x both before and after the call to f3. Drop the print inside f3. Again note
that x is in the main function (it's a word - at some location - in the memory area associated only
with the main function). You want to change the value of x, but you need to do that work in the
function f3. f3 has its own memory area, but it can't directly use the variables in the main function's
memory area (where x lives). BUT, in f3 you DO have the address of where x is located in the
main function's memory area. To emphasize, x is in the main function, x_address is in function f3.
You have that address of x inside f3 because it was copied ("passed") into x_address when in the
call to f3 you passed &x as an argument. So to access x (either to get it or to set it) in f3 you have
to dereference the x_address. Again, by dereferencing the x_address in the f3 code, you are
working (in f3) with the memory word x which is only in the main function. Important aside: It is
important to recognize that using addresses (pointers) is not directly dependent on whether or not
you are using functions. If you had both x and x_address declared in a main function and were to
put the address of x into the variable x_address in the main function code with the statement
x_address = &x; , then the following two lines are absolutely equivalent in the main function and
you could use either: x = x + 4; and *x_address = *x_address + 4; Why do we go through all this
rigamarole with addresses when we want to change a value in a function? Because in C (and
many other computer languages) argument values are only ever **passed into** (copied into)
function parameters. Nothing (!!) is EVER "passed back". So if you just copied in x to y, as in the
earlier steps of this lab, you could change y all you wanted in the function... and x in the main
function would still be the same old x it always was. You would only be changing the COPY of x in
f3 (y would be the copy as you used it in the earlier steps 6 or 7). So, if we want a change to be
made to a value in the main function, we pass the address of WHERE we want the change made
(in the main function) to the helper function (such as f3); the helper function accesses that
address/location in the main function's memory, which it does have [of course it does, we passed
that address to the function] and the called function (f3) makes the change there (in the calling
main function). Note also that using a return statement to bring something "back" is a completely
separate issue, and it can only return a single value - whereas in a great many cases far more
than one item from a function is wanted. In those cases, such as you did in this step and will also
do in the step below, you have to pass an address and then use dereferencing in the called
function. Now that you've seen how to pass in an address of a variable to a function and in that
function use that address to make a change to the variable, back in the calling (main in this case)
function, you can comment all the preceding function calling and write and call another void
function f4 which has three parameters. The first, an integer variable named y, will be passed the
initial number x from the main function, as seen above. The other two parameters will receive
addresses from the call. The second parameter, named addr1, will be used to receive the address
of a variable named num_of_tens which you will declare in the main function. The third parameter,
named addr2, will be used to receive the address of a variable named remainder also declared in
the main function . In the main function, the value will first be set for x, as above, and then f4 is to
be called, passing it the value of x and the value of the address of num_of_tens (written as
&num_of_tens) and the value of the address of remainder (written as &remainder). Neither
num_of_tens or remainder have had anything put into them at the point where f4 is called. In fact,
nothing in the main function will set them. f4 will, using the code you wrote earlier, determine the
number of 10s in y (copied from x) and the remainder when y is divided by 10. By dereferencing
addr1 and addr2, f4 will then set these calculated values into the variables num_of_tens and
remainder (they are both in the main function's memory area or as we often term in "they are in
the main function") ... and then f4 will quit. Nothing is returned, nothing is "sent back". You passed
in two addresses, made changes at those two addresses, and then the function is finished and
control passes back to the main function. Immediately after the call to f4, in the main function write
code so that it will print out the values of num_of_tens and remainder, properly labeled. Try this
with a few different initial numbers. You final program should look like this. The user enters a
number between 10 and 99 (validated), then the program calls the function f3 that gives both the
quotient and the remainder of a division by 10. The two values are printed out (not in the f3
function, but in the main program).

More Related Content

Similar to Exercise 1 Write a C program that contains the following s.pdf

Chapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer ScienceChapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer ScienceKrithikaTM
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdfNehaSpillai1
 
C language presentation
C language presentationC language presentation
C language presentationbainspreet
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docxJoeyDelaCruz22
 
ForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptxForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptxAaliyanShaikh
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptxRhishav Poudyal
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes NUST Stuff
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxvekariyakashyap
 

Similar to Exercise 1 Write a C program that contains the following s.pdf (20)

User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Function in c
Function in cFunction in c
Function in c
 
Chapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer ScienceChapter Functions for grade 12 computer Science
Chapter Functions for grade 12 computer Science
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
 
Cpp functions
Cpp functionsCpp functions
Cpp functions
 
C function
C functionC function
C function
 
C language presentation
C language presentationC language presentation
C language presentation
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docx
 
Pro
ProPro
Pro
 
ForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptxForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptx
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 

More from mail354931

Vacationers enter Giovannis Gelateria that prides itself on.pdf
Vacationers enter Giovannis Gelateria that prides itself on.pdfVacationers enter Giovannis Gelateria that prides itself on.pdf
Vacationers enter Giovannis Gelateria that prides itself on.pdfmail354931
 
Daniel LLC malzeme iin 45000 iilik iin 25000 ve fa.pdf
Daniel LLC malzeme iin 45000  iilik iin 25000  ve fa.pdfDaniel LLC malzeme iin 45000  iilik iin 25000  ve fa.pdf
Daniel LLC malzeme iin 45000 iilik iin 25000 ve fa.pdfmail354931
 
Dr Rogers is a new general practitioner at Pritter City Hos.pdf
Dr Rogers is a new general practitioner at Pritter City Hos.pdfDr Rogers is a new general practitioner at Pritter City Hos.pdf
Dr Rogers is a new general practitioner at Pritter City Hos.pdfmail354931
 
4 and CAM plants use a cifferentstrategy then C 3 plantslin .pdf
4 and CAM plants use a cifferentstrategy then C 3 plantslin .pdf4 and CAM plants use a cifferentstrategy then C 3 plantslin .pdf
4 and CAM plants use a cifferentstrategy then C 3 plantslin .pdfmail354931
 
American International Group AIG stood at the centre of th.pdf
American International Group AIG stood at the centre of th.pdfAmerican International Group AIG stood at the centre of th.pdf
American International Group AIG stood at the centre of th.pdfmail354931
 
AFN Required additional assets Increase in Spontaneous Li.pdf
AFN Required additional assets  Increase in Spontaneous Li.pdfAFN Required additional assets  Increase in Spontaneous Li.pdf
AFN Required additional assets Increase in Spontaneous Li.pdfmail354931
 
Australia In February Uber lost a court challenge against a.pdf
Australia In February Uber lost a court challenge against a.pdfAustralia In February Uber lost a court challenge against a.pdf
Australia In February Uber lost a court challenge against a.pdfmail354931
 
ACME Corp 205110014302001DB8CAFE11564 2051.pdf
ACME Corp 205110014302001DB8CAFE11564 2051.pdfACME Corp 205110014302001DB8CAFE11564 2051.pdf
ACME Corp 205110014302001DB8CAFE11564 2051.pdfmail354931
 
Under standards you may include areas such as the following.pdf
Under standards you may include areas such as the following.pdfUnder standards you may include areas such as the following.pdf
Under standards you may include areas such as the following.pdfmail354931
 
This assignment is intended to help you learn to do the foll.pdf
This assignment is intended to help you learn to do the foll.pdfThis assignment is intended to help you learn to do the foll.pdf
This assignment is intended to help you learn to do the foll.pdfmail354931
 
Theffamous iris dataset the first sheet of the spreadsheet .pdf
Theffamous iris dataset the first sheet of the spreadsheet .pdfTheffamous iris dataset the first sheet of the spreadsheet .pdf
Theffamous iris dataset the first sheet of the spreadsheet .pdfmail354931
 
5 Let Y1Y2Yn be independent exponentially distributed .pdf
5 Let Y1Y2Yn be independent exponentially distributed .pdf5 Let Y1Y2Yn be independent exponentially distributed .pdf
5 Let Y1Y2Yn be independent exponentially distributed .pdfmail354931
 
STEP 3 Create the May 31 Unadjusted Trial Balance from the .pdf
STEP 3 Create the May 31 Unadjusted Trial Balance from the .pdfSTEP 3 Create the May 31 Unadjusted Trial Balance from the .pdf
STEP 3 Create the May 31 Unadjusted Trial Balance from the .pdfmail354931
 
Problem 2 Features grow on branches Filtercpp program can .pdf
Problem 2 Features grow on branches Filtercpp program can .pdfProblem 2 Features grow on branches Filtercpp program can .pdf
Problem 2 Features grow on branches Filtercpp program can .pdfmail354931
 
5 10 pts We assume that following MIPS code is executed o.pdf
5 10 pts We assume that following MIPS code is executed o.pdf5 10 pts We assume that following MIPS code is executed o.pdf
5 10 pts We assume that following MIPS code is executed o.pdfmail354931
 
51 Using the annual financial report obtained for Exercise.pdf
51 Using the annual financial report obtained for Exercise.pdf51 Using the annual financial report obtained for Exercise.pdf
51 Using the annual financial report obtained for Exercise.pdfmail354931
 
Identify potential strategies nurses might use to navigate c.pdf
Identify potential strategies nurses might use to navigate c.pdfIdentify potential strategies nurses might use to navigate c.pdf
Identify potential strategies nurses might use to navigate c.pdfmail354931
 
Myca Corpun nakit aklar aadaki gibi olan bir projesi vardr.pdf
Myca Corpun nakit aklar aadaki gibi olan bir projesi vardr.pdfMyca Corpun nakit aklar aadaki gibi olan bir projesi vardr.pdf
Myca Corpun nakit aklar aadaki gibi olan bir projesi vardr.pdfmail354931
 
Intracranial Regulation Case Study Mr James Hobson is a 69.pdf
Intracranial Regulation Case Study Mr James Hobson is a 69.pdfIntracranial Regulation Case Study Mr James Hobson is a 69.pdf
Intracranial Regulation Case Study Mr James Hobson is a 69.pdfmail354931
 
Hester is configuring syslog on her companys network She s.pdf
Hester is configuring syslog on her companys network She s.pdfHester is configuring syslog on her companys network She s.pdf
Hester is configuring syslog on her companys network She s.pdfmail354931
 

More from mail354931 (20)

Vacationers enter Giovannis Gelateria that prides itself on.pdf
Vacationers enter Giovannis Gelateria that prides itself on.pdfVacationers enter Giovannis Gelateria that prides itself on.pdf
Vacationers enter Giovannis Gelateria that prides itself on.pdf
 
Daniel LLC malzeme iin 45000 iilik iin 25000 ve fa.pdf
Daniel LLC malzeme iin 45000  iilik iin 25000  ve fa.pdfDaniel LLC malzeme iin 45000  iilik iin 25000  ve fa.pdf
Daniel LLC malzeme iin 45000 iilik iin 25000 ve fa.pdf
 
Dr Rogers is a new general practitioner at Pritter City Hos.pdf
Dr Rogers is a new general practitioner at Pritter City Hos.pdfDr Rogers is a new general practitioner at Pritter City Hos.pdf
Dr Rogers is a new general practitioner at Pritter City Hos.pdf
 
4 and CAM plants use a cifferentstrategy then C 3 plantslin .pdf
4 and CAM plants use a cifferentstrategy then C 3 plantslin .pdf4 and CAM plants use a cifferentstrategy then C 3 plantslin .pdf
4 and CAM plants use a cifferentstrategy then C 3 plantslin .pdf
 
American International Group AIG stood at the centre of th.pdf
American International Group AIG stood at the centre of th.pdfAmerican International Group AIG stood at the centre of th.pdf
American International Group AIG stood at the centre of th.pdf
 
AFN Required additional assets Increase in Spontaneous Li.pdf
AFN Required additional assets  Increase in Spontaneous Li.pdfAFN Required additional assets  Increase in Spontaneous Li.pdf
AFN Required additional assets Increase in Spontaneous Li.pdf
 
Australia In February Uber lost a court challenge against a.pdf
Australia In February Uber lost a court challenge against a.pdfAustralia In February Uber lost a court challenge against a.pdf
Australia In February Uber lost a court challenge against a.pdf
 
ACME Corp 205110014302001DB8CAFE11564 2051.pdf
ACME Corp 205110014302001DB8CAFE11564 2051.pdfACME Corp 205110014302001DB8CAFE11564 2051.pdf
ACME Corp 205110014302001DB8CAFE11564 2051.pdf
 
Under standards you may include areas such as the following.pdf
Under standards you may include areas such as the following.pdfUnder standards you may include areas such as the following.pdf
Under standards you may include areas such as the following.pdf
 
This assignment is intended to help you learn to do the foll.pdf
This assignment is intended to help you learn to do the foll.pdfThis assignment is intended to help you learn to do the foll.pdf
This assignment is intended to help you learn to do the foll.pdf
 
Theffamous iris dataset the first sheet of the spreadsheet .pdf
Theffamous iris dataset the first sheet of the spreadsheet .pdfTheffamous iris dataset the first sheet of the spreadsheet .pdf
Theffamous iris dataset the first sheet of the spreadsheet .pdf
 
5 Let Y1Y2Yn be independent exponentially distributed .pdf
5 Let Y1Y2Yn be independent exponentially distributed .pdf5 Let Y1Y2Yn be independent exponentially distributed .pdf
5 Let Y1Y2Yn be independent exponentially distributed .pdf
 
STEP 3 Create the May 31 Unadjusted Trial Balance from the .pdf
STEP 3 Create the May 31 Unadjusted Trial Balance from the .pdfSTEP 3 Create the May 31 Unadjusted Trial Balance from the .pdf
STEP 3 Create the May 31 Unadjusted Trial Balance from the .pdf
 
Problem 2 Features grow on branches Filtercpp program can .pdf
Problem 2 Features grow on branches Filtercpp program can .pdfProblem 2 Features grow on branches Filtercpp program can .pdf
Problem 2 Features grow on branches Filtercpp program can .pdf
 
5 10 pts We assume that following MIPS code is executed o.pdf
5 10 pts We assume that following MIPS code is executed o.pdf5 10 pts We assume that following MIPS code is executed o.pdf
5 10 pts We assume that following MIPS code is executed o.pdf
 
51 Using the annual financial report obtained for Exercise.pdf
51 Using the annual financial report obtained for Exercise.pdf51 Using the annual financial report obtained for Exercise.pdf
51 Using the annual financial report obtained for Exercise.pdf
 
Identify potential strategies nurses might use to navigate c.pdf
Identify potential strategies nurses might use to navigate c.pdfIdentify potential strategies nurses might use to navigate c.pdf
Identify potential strategies nurses might use to navigate c.pdf
 
Myca Corpun nakit aklar aadaki gibi olan bir projesi vardr.pdf
Myca Corpun nakit aklar aadaki gibi olan bir projesi vardr.pdfMyca Corpun nakit aklar aadaki gibi olan bir projesi vardr.pdf
Myca Corpun nakit aklar aadaki gibi olan bir projesi vardr.pdf
 
Intracranial Regulation Case Study Mr James Hobson is a 69.pdf
Intracranial Regulation Case Study Mr James Hobson is a 69.pdfIntracranial Regulation Case Study Mr James Hobson is a 69.pdf
Intracranial Regulation Case Study Mr James Hobson is a 69.pdf
 
Hester is configuring syslog on her companys network She s.pdf
Hester is configuring syslog on her companys network She s.pdfHester is configuring syslog on her companys network She s.pdf
Hester is configuring syslog on her companys network She s.pdf
 

Recently uploaded

Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 

Recently uploaded (20)

TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 

Exercise 1 Write a C program that contains the following s.pdf

  • 1. Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. Commenting out the existing coding, write the code to divide a two digit integer number by 10 (pick any number you want between 10 and 99; hard code it right into the program rather than input it). Output the result as an integer. Try this with a few different initial numbers. For example, 59 would give an output of 5. Next write the code to find the remainder when a two digit number (again, hard code it into your program) is divided by 10 [note that this is done in a single statement]. Output the result as an integer. Try this with a few different numbers. For example, 59 would give an output of 9. Comment the code from steps 4 and 5 out of your program, and put the coding to find the number of 10's in a number into a user defined (helper) function named f1. Set the initial number in a variable named x in the main function and print it out to check that it was set correctly). Your code will then pass x to a variable named y in f1 - the syntax for the call is of course f1( x ); . (That is, you are copying the value in the argument variable x into the parameter variable y.) f1 should return the number of 10's in y. After f1 returns, the main function should print the returned value. You should see the original number printed from the main function and the returned number of 10's in that original number, also printed from the main function. Try this with a couple of different initial numbers. Repeat step 6 but this time put the coding of the step 5 into another user defined (helper) function named f2. Again use y as the name of the parameter in f2; it will not get confused with the y in f1 - they are declared in separate functions and are thus completely different variables. (f1 has its own y and f2 has its own y.) f2 should return the remainder when the passed (copied) number in y is divided by 10. After the setting of the initial number and printing it to check it, the main function should invoke (call) f2, and after f2 returns the main function should print the returned value. You should see the original number printed from the main function and the returned remainder when that original number is divided by 10, also printed from the main function. Try this with a couple of different initial numbers. Comment out the calls for f1 and f2. Keep the assignment of a value to x and the print to check that it was properly set. Write a void (no return value will be given) function f3 which will be passed the address of your initial number (x). The syntax for the call to this function is, of course f3(&x); The function f3 will have a single parameter, named x_address, and that parameter must be able to hold the ADDRESS of an integer value, that address being copied from the &x. Check with you notes Ch 3 slide 7 to see how this should be declared. By dereferencing an address (a pointer) we are accessing the value AT that address (either getting it or setting it). Inside f3, dereference the parameter x_address (the dereference syntax is "*x_address", with no quotes of course) and then (still inside f3) print out the result of the dereferencing. You will see that you have obtained the value of x, which is in the main function. After returning, the main function should print out x again. All three prints of x (the original check in the main, inside f3 after dereferencing, and in the main after returning from f3) will be the same - all three printfs will be printing the value of the x which is located in the memory area belonging to the main function. Be sure to label each printing of x with a few words so you can tell them apart on your output. Modify f3 to add 4 to x ensuring that in the main function you are printing out the value of x both before and after the call to f3. Drop the print inside f3. Again note
  • 2. that x is in the main function (it's a word - at some location - in the memory area associated only with the main function). You want to change the value of x, but you need to do that work in the function f3. f3 has its own memory area, but it can't directly use the variables in the main function's memory area (where x lives). BUT, in f3 you DO have the address of where x is located in the main function's memory area. To emphasize, x is in the main function, x_address is in function f3. You have that address of x inside f3 because it was copied ("passed") into x_address when in the call to f3 you passed &x as an argument. So to access x (either to get it or to set it) in f3 you have to dereference the x_address. Again, by dereferencing the x_address in the f3 code, you are working (in f3) with the memory word x which is only in the main function. Important aside: It is important to recognize that using addresses (pointers) is not directly dependent on whether or not you are using functions. If you had both x and x_address declared in a main function and were to put the address of x into the variable x_address in the main function code with the statement x_address = &x; , then the following two lines are absolutely equivalent in the main function and you could use either: x = x + 4; and *x_address = *x_address + 4; Why do we go through all this rigamarole with addresses when we want to change a value in a function? Because in C (and many other computer languages) argument values are only ever **passed into** (copied into) function parameters. Nothing (!!) is EVER "passed back". So if you just copied in x to y, as in the earlier steps of this lab, you could change y all you wanted in the function... and x in the main function would still be the same old x it always was. You would only be changing the COPY of x in f3 (y would be the copy as you used it in the earlier steps 6 or 7). So, if we want a change to be made to a value in the main function, we pass the address of WHERE we want the change made (in the main function) to the helper function (such as f3); the helper function accesses that address/location in the main function's memory, which it does have [of course it does, we passed that address to the function] and the called function (f3) makes the change there (in the calling main function). Note also that using a return statement to bring something "back" is a completely separate issue, and it can only return a single value - whereas in a great many cases far more than one item from a function is wanted. In those cases, such as you did in this step and will also do in the step below, you have to pass an address and then use dereferencing in the called function. Now that you've seen how to pass in an address of a variable to a function and in that function use that address to make a change to the variable, back in the calling (main in this case) function, you can comment all the preceding function calling and write and call another void function f4 which has three parameters. The first, an integer variable named y, will be passed the initial number x from the main function, as seen above. The other two parameters will receive addresses from the call. The second parameter, named addr1, will be used to receive the address of a variable named num_of_tens which you will declare in the main function. The third parameter, named addr2, will be used to receive the address of a variable named remainder also declared in the main function . In the main function, the value will first be set for x, as above, and then f4 is to be called, passing it the value of x and the value of the address of num_of_tens (written as &num_of_tens) and the value of the address of remainder (written as &remainder). Neither num_of_tens or remainder have had anything put into them at the point where f4 is called. In fact, nothing in the main function will set them. f4 will, using the code you wrote earlier, determine the number of 10s in y (copied from x) and the remainder when y is divided by 10. By dereferencing
  • 3. addr1 and addr2, f4 will then set these calculated values into the variables num_of_tens and remainder (they are both in the main function's memory area or as we often term in "they are in the main function") ... and then f4 will quit. Nothing is returned, nothing is "sent back". You passed in two addresses, made changes at those two addresses, and then the function is finished and control passes back to the main function. Immediately after the call to f4, in the main function write code so that it will print out the values of num_of_tens and remainder, properly labeled. Try this with a few different initial numbers. You final program should look like this. The user enters a number between 10 and 99 (validated), then the program calls the function f3 that gives both the quotient and the remainder of a division by 10. The two values are printed out (not in the f3 function, but in the main program).