Tải bản đầy đủ (.pdf) (5 trang)

Supplementary tasks variables and basic data types

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (255.55 KB, 5 trang )

Chapter 3: Variables and Basic Data Types

A. Review
A.1. What is a data type? Give at least three examples of data types.
A.2. What are the data types built-in in the C programming language? Give at least three examples of
data of each built-in data type in C.
A.3. What are derived data types in the C programming language? Discuss the benefits of derived data
types in application development. Give at least three examples of data of each derived data type in C.
A.4. What is a variable in a C program? Distinguish between variable declaration and variable definition.
Give at least three examples of variable declaration with each built-in data type in C.
A.5. Distinguish between global variables and local variables. Give at least three examples of global
variable declaration with each built-in data type in C. Similarly, give at least three examples of local
variable declaration with each built-in data type in C.
A.6. What is a constant in a C program? Give at least three constants and their use’s context. How is a
constant defined in a C program? Give at least three examples of each way of constant definition. List
advantages/disadvantages of each way of constant definition.
A.7. What is a variable’s scope? Give an example of demonstrating the scope of each variable type.
A.8. What is a storage class of each variable? Describe each storage class along with variable definitions.
A.9. Distinguish between type promotion and truncation in expression value determination. Give an
example of expression value determination where type promotion occurs and another example of
expression value determination where truncation occurs.
A.10. How is a structure of several members (fields) of different (same) data types defined in the C
programming language? Discuss benefits of the derived struct data type in C. Give at least three
examples of different structures defined with the derived struct data type. Describe the use context of
each structure in our real world.
A.11. What is typedef? Give at least three examples of demonstrating a use for typedef.
B. Practice
B.1. Determine inputs and outputs of each problem in chapter 1: B.1..B.15. Define the variables with
appropriate data types corresponding to the inputs and outputs of each problem B1..B15. Describe their
scopes and storage classes.
B.2. Write appropriate variable definitions/declarations for each following description.


1


B.2.1. Define 3 global variables of data type int, whose names are intVar1, intVar2, and intVar3.
B.2.2. Define 2 local variables of data type float, whose names are floatVar1 and floatVar2, in the main
function. The floatVar1 variable is initialized with a value 2.8.
B.2.3. Define a static global variable of data type double, whose name is doubleVar.
B.2.4. Define a static local variable of data type char, whose name is charVar, in the main function.
B.2.5. Declare a global variable of data type int, whose name is intVar4, so that this variable can be
referred and used in the main function. A definition of intVar4 has been made in another source code
file.
B.2.6. Define a global variable of data type enum, whose name is enumVar. A new enum data type is
defined with three status values: COMPLETE, INCOMPLETE, UNSPECIFIED.
B.2.7. Define a structure using the derived data type struct to represent courses. The members (fields)
of this structure are: identifier with data type char [10], name with data type char[100], credit with data
type short, group with data type short, and type with data type enum which is defined with two values:
REQUISITE and SELECTIVE. After that, define a local variable of this structure, whose name is aCourse, in
the main function. Initialized values for the members of the variable aCourse are: “CO1003”,
“Introduction to Computer Programming”, 3, 1, REQUISITE.
B.3. Given a C program below. What will be printed on the screen? Give a short explanation based on
the scope of each variable.

2


B.4. Given a C program below. State which variable declarations are valid and which ones are invalid. For
the valid variable declarations, describe their scopes and storage classes. For the invalid variable
declarations, describe why they are invalid and make appropriate changes so that they can become
valid.


3


B.5. Given a following program. List all the expressions available in the program. Which expressions are
invalid? Make appropriate changes on these expressions so that they can become valid.

B.6. It is supposed that a location of each object is represented with a positive value pair (x, y). Using the
Euclidean distance between two locations, we can know how far an object is from another one. Student
B currently stands at location (x0, y0) whose value will be input on demand once student B travels
around the city. The city has several beautiful places that should be visited. They are:
- Walking street at location (23.5, 10)
- Post office at location (2.8, 4.3)
- Church at location (5.1, 17)
- Independence palace at location (1.6, 2.9)
Recommend the nearest places that student B should visit first from his/her current location. Print the
information about the aforementioned places along with your recommendation (YES or NO) in the form
of a table whose header is: Place, X, Y, Recommendation. Each column is presented with left
justification.

4


B.7. Student A studies three courses in this semester. Using the structure defined in B.2.7, the
information of the three courses can be described as follows.
Course1: “CO1003”, “Introduction to Computer Programming”, 3, 1, REQUISITE
Course2: “XXXXX2”, “Course2”, 3, 1, REQUISITE
Course3: “XXXXX3”, “Course3”, 4, 2, SELECTIVE
Write a program to populate the aforementioned information into the variables and then, calculate the
total number of credits which the student A has registered for and let us know how many requisite
courses the student A is studying. Print all the course information in the form of a table whose header is

the member names of the structure: identifier, name, credit, group, type. The column for course names
is displayed with left justification. Print all the computed results line by line on the screen.
B.8. Now extend the structure defined in B.2.7 with two additional members: grade of data type double
and ranking of data type char. Member grade is used to keep a grade of a corresponding course that a
student has obtained after studying the course. Member ranking is used to show grades in a 4-scale
grading system: ‘A’ for grade in [8.5, 10], ‘B’ for grade in [6.5, 8.5), ‘C’ for grade in [5.0, 6.5), and ‘D’ for
grade in [0, 5). Calculate a semester GPA of student A using a weighted mean such as:
GPA 

Course1.grade* Course1.credit  Course2.grade* Course2.credit  Course3.grade* Course3.credit
Course1.credit  Course2.credit  Course3.credit

It is supposed that student A has got 8, 7.5, and 8.8 for Course1, Course2, and Course3, respectively.
GPA is then:
GPA 

8 * 3  7.5 * 3  8.8 * 4
 8.17
33 4

Now receive the grades of student A from your keyboard and then print the grades along with the
course information in the form of a table whose header includes: identifier, name, credit, group, type,
grade, ranking. Two columns name and grade are presented with left justification. After that, print the
semester GPA of student A with 2 places before the decimal point and 1 place after the decimal point. If
GPA < 5.0 then print “Umh! Umh! Umh!”. If GPA >= 5.0 and GPA < 8.0 then print “You are a good
student.” Otherwise, print “Wow! Wow! Wow!”. Please note that grades are from 0 to 10. If an invalid
grade is input, the program won’t proceed with GPA calculation.
B.9. Check your coding style with the programs you have written. Should anything be changed with your
programs so that they can be not only executable but also readable? Rewrite your programs if yes.
NOTE: none of if, if..else.., switch, for, while, do..while statements is allowed to be used in your

programs in this chapter.

5



×