Assignment 1: Getting Started

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Assignment 1

C/C++ Programming I

General Information

Getting Started
Before starting your first assignment you must have the appropriate tools for developing your software,
and the best way to get them is to download and install one of the many free integrated development
environment (IDE) packages. These integrate the compiler, editor, and other necessary tools into a
convenient GUI application. Although you are free to use any tools you wish and any operating system
that will support them, I recommend Microsoft’s "Visual Studio Community" for Windows, "Xcode" for Max
OS X, and "Code::Blocks" for Linux. Information on obtaining, installing, and using them is available in the
appropriate version of the course document titled "Using the Compiler's IDE…", a link to which is located
on the "Assignments" page of the course Web site. I’m sorry but I don’t have information on other IDE’s
or operating systems.

Source Code Files: Header Files and Implementation Files


“Source code” files contain the code necessary for a program to be built without errors and are
divided into the two categories “header” files (.h, etc.) and “implementation” files (.c, .cpp, etc.). Not
all programs require header files but at least one implementation file is always required. Header files are
designed to be included in other files using the #include directive but implementation files are not. By
placing items that might be needed by multiple other files in header files and including them in those
files the bad practice of literally duplicating the needed items in each file can be avoided. Because of
their multiple usages, however, header files must never contain anything that will result in an error if
more than one file includes them. Files containing data that your program reads or writes are not
considered source code files but are instead “data files”.

Although some of the following terminology has not yet been discussed in this course it is placed here
for completeness and for future reference: Header files typically contain things like macro definitions,
inline function definitions, function prototypes, referencing declarations, typedefs, class/structure/union
descriptions, and templates, although any of these that will only ever be needed by one specific
implementation file may be placed in that file instead. Header files must not contain non-inline function
definitions or defining variable declarations; these must be placed in implementation files instead. The
header files that are supplied with a compiler provide the information it needs to properly compile code
that uses the various functions, macros, and data types available in the compiler’s libraries.

Too Easy?
If, after completing the three exercises in this document, you would like to gain more programming
experience please contact the instructor at [email protected] and request a
supplemental set of Assignment 1 exercises. Note, however, that these are only for your own benefit
and you will not receive any extra credit for doing them.

1992-2017 Ray Mitchell Page 1 of 5


Exercise 0 (6 points – 1 point per question – No program required)
Language standards compliance and appropriate header file inclusion is assumed. Testing code by
running it is sometimes misleading due to implementation dependence. These are not trick questions
and there is only one correct answer to each. Applicable notes from the course book are listed.

1. Which of the following is not a character 4. What data types are acceptable for x in the
literal? expression printf("%d", x)
(Note 1.5) (Note 1.11)
A. 'A' A. int only
B. 1 double quote between 2 single quotes B. int and double only
C. '\0' C. char, short, and int only
D. '\x5' D. char, short, int, and long only
E. 1 single quote between 2 single quotes E. any of the above

2. Which of the following is not a string literal? 5. What data types are acceptable for x in the
(Note 1.5) expression scanf("%d", &x)
A. nothing between 2 double quotes (Note 1.13)
B. spaces between 2 double quotes A. int only
C. 1 single quote between 2 double quotes B. int and double only
D. 1 double quote between 2 double C. char, short, and int only
quotes D. char, short, int, and long only
E. \t between 2 double quotes E. any of the above

3. Assuming the ASCII character set, predict 6. For the declaration char ch; expressions that
the output from: will input a character from the user into the
printf("\x49\146\155\155\x70\x0021"); variable ch, skipping all leading whitespace
(Note 1.5 & Note B.1 ASCII Code Chart) are:
A. nothing - The compiler will not allow this. (Note 1.15)
B. \x49\146\155\155\x70\x0021 A. scanf("%c", &ch) and cin >> ch
C. \xe\1)( B. ch = getchar() and cin.get()
D. Ifmmp! C. scanf("%c", &ch) and cin >> &ch
E. none of the above D. scanf("\n%c", &ch) and cin >> ch
E. scanf("%c", ch) and cin >> (char)&ch

Submitting your solution


Using the format below place your answers in a plain text file named C1A1E0_Quiz.txt and send it to the
Assignment Checker with the subject line C1A1E0_ID, where ID is your 9-character UCSD student ID.
-- Place an appropriate “Title Block” here --
1. A
2. C
etc.
See the course document titled “Preparing and Submitting Your Assignments” for additional exercise
formatting, submission, and Assignment Checker requirements.

1992-2017 Ray Mitchell Page 2 of 5


1 Exercise 1 (7 points – C Program)
2 Exclude any existing source code files that may already be in your IDE project and add a new one,
3 naming it C1A1E1_main.c. Write a program in that file to display the exact text below using printf:
4
5 In C/C++ the case of letters is significant.
6 main is where program execution begins.
7 A semicolon terminates most statements.
8 10% of "nothing" is 100% of "nothing".
9 Use \n to cause a newline; use \t to cause a tab.
10 Use \a to cause a beep (only on some platforms)!
11
12 Your program must:
13 1. not call printf more than once;
14 2. not use the underlying numeric value of any character;
15 3. not use the %c, %s, or %[] conversion specifications.
16
17
18 Submitting your solution
19 Send your source code file to the Assignment Checker with the subject line C1A1E1_ID, where ID is your
20 9-character UCSD student ID.
21 See the course document titled “Preparing and Submitting Your Assignments” for additional exercise
22 formatting, submission, and Assignment Checker requirements.
23
24
25 Hints:
26 To display a percent character located within a printf control string (the control string is the first
27 argument of printf) use two percent characters together. To represent a backslash character in any
28 string use two backslash characters together. The compiler automatically concatenates multiple
29 character literals separated only by zero or more whitespaces into one string, including string literals on
30 separate lines.

1992-2017 Ray Mitchell Page 3 of 5


1 Exercise 2 (7 points – C++ Program)
2 Exclude any existing source code files that may already be in your IDE project and add a new one,
3 naming it C1A1E2_main.cpp. Write a program in that file to display a value in decimal, octal, and
4 hexadecimal. Your program must:
5
6 1. not use cout more than twice;
7 2. declare one type int variable;
8 3. use cout to prompt the user to enter any decimal integer numeric value;
9 4. use cin obtain and store the user input value in the type int variable;
10 5. use cout to display the input value in decimal, octal, and hexadecimal using the following
11 format, where D represents the decimal value, O represents the octal value, and H represents the
12 hexadecimal value:
13 D decimal = O octal = H hexadecimal
14
15 For example, if the user were to enter 22 your program would display the following, all on one line:
16 22 decimal = 26 octal = 16 hexadecimal
17
18 Manually re-run your program several times, testing with at least the following 5 input values:
19 7 8 15 16 -1
20
21
22 Submitting your solution
23 Send your source code file to the Assignment Checker with the subject line C1A1E2_ID, where ID is your
24 9-character UCSD student ID.
25 See the course document titled “Preparing and Submitting Your Assignments” for additional exercise
26 formatting, submission, and Assignment Checker requirements.
27
28
29 Hints:
30 See note 1.12. Use the hex, oct, and dec manipulators to change the integer output format to
31 hexadecimal, octal, or decimal, respectively. The selected format will remain in effect until explicitly
32 changed. Upon program startup the format is always decimal.

1992-2017 Ray Mitchell Page 4 of 5


Get a Consolidated Assignment Report (optional)
If you would like to receive a consolidated report containing the results of the most recent version of
each exercise submitted for this assignment, send an empty email to the assignment checker with the
subject line C1A1_ID, where ID is your 9-character UCSD student ID. Inspect the report carefully since it is
what I will be grading. You may resubmit exercises and report requests as many times as you wish
before the assignment deadline.

1992-2017 Ray Mitchell Page 5 of 5

You might also like