0% found this document useful (0 votes)
4 views

Lab05 Pointers and Advanced Functions (1)

Uploaded by

mekalajeya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab05 Pointers and Advanced Functions (1)

Uploaded by

mekalajeya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

CPS188 > Lab 05 > Pointers and Advanced Functions

Pointers and Advanced Functions

I. Overview and Objectives

As you have seen in lectures, pointers (also known as pointer


variables) are special variables that are used to store
addresses rather than values. Some C programming tasks are
performed more easily with pointers, and other tasks, such as
having functions that produce multiple results, cannot be
performed without using pointers. So it becomes necessary to
learn how to work with pointers to become a proficient C
programmer.

The learning objective of this lab is to become familiar with


pointers and their use in writing functions that give multiple
results.

Reading and related topics: Course slides lesson 06. Book


chapter 6.

II. Lab Tasks and Submission Guideline

Write complete C programs to solve the following two


problems. Save the code you wrote to solve them, together
with the result of it in a report. Make sure you include enough
comments in your code.

For each problem, copy and paste the source code, copy and
paste the execution results of each of the scenarios
(screenshots are acceptable) into your lab report. Save your
CPS188 > Lab 05 > Pointers and Advanced Functions

report in .pdf format and submit it on D2L. You should


submit your lab at the end of your lab session or soon after.
In all cases it must be submitted before the deadline
indicated in the D2L dropbox or it will not be accepted for
marking.
CPS188 > Lab 05 > Pointers and Advanced Functions

Problem 1: Write a C program that asks a user for


three integer numbers. Your program will then call
one function that will give three results: the highest
value, the lowest value, and the middle value.
Then your program will print the three values in
ascending order.
Test your program with the following sets of input
data:
56 78 23
45 -20 22
-80 -40 -250
8 0 2
55 55 55
88 77 66
-8 33 -8
12 13 14
CPS188 > Lab 05 > Pointers and Advanced Functions

4
CPS188 > Lab 05 > Pointers and Advanced Functions

5
CPS188 > Lab 05 > Pointers and Advanced Functions

6
CPS188 > Lab 05 > Pointers and Advanced Functions

7
CPS188 > Lab 05 > Pointers and Advanced Functions

Problem 2: Let's assume that you could travel to


the Moon by car traveling at 100 km/h. How long
would that take to get there? How about going to
Mars or Venus? If you go there by rocket with a
higher speed, it would obviously take less time.
To solve this problem you will need to write three
user-defined functions, one for each destination.
Each of the functions will take the travelling speed
in km/h as the argument and give the time (in
hours) it will take to get to the destination.
Note that because there are minimum (perigee)
and maximum (apogee) distances between the
Earth, Moon, Mars and Venus, you will need to
calculate the minimum and maximum travel times
so the three functions will each produce two
results.
Distances:
Earth to Moon > Perigee: 363,104 km. Apogee:
405,696 km
Earth to Mars > Perigee: 54.6 million km. Apogee:
401 million km
Earth to Venus > Perigee: 38 million km. Apogee:
261 million km

Write a C program that will present a menu with


four options:
1. Traveling to the Moon
2.Traveling to Mars
3. Traveling to Venus
4. Exit program.
CPS188 > Lab 05 > Pointers and Advanced Functions

You will ask the user for the travelling speed after
the user has chosen options 1, 2 or 3. Your program
must keep presenting the menu until the user
chooses option 4.

Check your program with the following speeds: 100


km/h (car), 500 km/h (airplane), and 41000 km/h
(rocket).
CPS188 > Lab 05 > Pointers and Advanced Functions

10
CPS188 > Lab 05 > Pointers and Advanced Functions

11
CPS188 > Lab 05 > Pointers and Advanced Functions

12

Have fun!

You might also like