C++ Crash Course: a fast-Paced Introduction



Yüklə 7 Mb.
Pdf görüntüsü
səhifə61/71
tarix20.09.2023
ölçüsü7 Mb.
#145939
1   ...   57   58   59   60   61   62   63   64   ...   71
C Crash Course A Fast-Paced Introduction by Josh Lospinoso

E XERCISES
Try these exercises to practice what you’ve learned in this chapter. (The book's 
companion code is available at 
https://ccc.codes
.)
1-1. 
Create a function called 
absolute_value
that returns the absolute value of 
its single argument. The absolute value of an integer 
x
is the following: 
x
(itself) 
if 
x
is greater than or equal to 0; otherwise, it is 
x
times −1. You can use the 
program in Listing 1-9 as a template:
#include
int absolute_value(int x) {
// Your code here
}
int main() {
int my_num = -10;
printf("The absolute value of %d is %d.\n", my_num, 
absolute_value(my_num));
}
Listing 1-9: A template for a program that uses an 
absolute_value
 function
1-2. 
Try running your program with different values. Did you see the values you 
expect?
1-3. 
Run your program with a debugger, stepping through each instruction.


Up and Running
29
1-4. 
Write another function called 
sum
that takes two 
int
arguments and returns 
their sum. How can you modify the template in Listing 1-9 to test your new 
function?
1-5. 
C++ has a vibrant online community, and the internet is awash with excel-
lent C++ related material. Investigate the CppCast podcast at 
http://cppcast 
.com/
. Search for CppCon and C++Now videos available on YouTube. Add 
https://cppreference.com/
 and 
http://www.cplusplus.com/
to your browser’s 
bookmarks.
1-6. 
Finally, download a copy of the International Organization for Standard-
ization (ISO) C++ 17 Standard from 
https://isocpp.org/std/the-standard/

Unfortunately, the official ISO standard is copyrighted and must be purchased. 
Fortunately, you can download a “draft,” free of charge, that differs only cos-
metically from the official version.
Note 
Because the ISO standard’s page numbers differ from version to ver-
sion, this book will refer to specific sections using the same naming 
schema as the standard itself. This schema cites sections by enclosing 
the section name with square brackets. Subsections are appended with 
period separation. For example, to cite the section on the C++ Object 
Model, which is contained in the Introduction section, you would write 
[intro.object]. 

Yüklə 7 Mb.

Dostları ilə paylaş:
1   ...   57   58   59   60   61   62   63   64   ...   71




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin