0% found this document useful (0 votes)
44 views6 pages

Lab 1

Bài 1 - Thực hành Cơ sở lập trình trường Đại học Khoa Học Tự Nhiên
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views6 pages

Lab 1

Bài 1 - Thực hành Cơ sở lập trình trường Đại học Khoa Học Tự Nhiên
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab 1.

Basic Guide Fundamentals of Programming CSC10012

Lab 1

Basic Guide
The first lab will introduce some basic C/C++ programming concepts including program structure,
data types and variables, basic input/output, basic operators, and handling text files.

Instruction
1. Program Structure
A basic C/C++ program follows a structured layout with the following components:
• Preprocessor Directives: These are commands that provide instructions to the compiler
to preprocess the information before actual compilation begins. For example, including
standard libraries using #include <iostream> or defining constants using #define.

• Main Function (int main()): Every C/C++ program must have a main() function where
the program execution starts.

• Statements and Expressions: These are the actual instructions within the functions that
carry out specific tasks (e.g., calculations, input/output, condition checking).

• Return Statement: In main(), the return value is typically an integer, where return 0,
indicates that the program ended successfully.
Example:
1 // Preprocessor Directive
2 # include < iostream >
3

4 /* The below statement allows us to use elements


5 * from the std namespace without the prefix std ::
6 */
7 using namespace std ;
8

9 int main ()
10 {
11 // Main function starts here
12 cout << " CSC10012 ! " << endl ; // Statements , expressions are here
13 return 0; // Return statement
14 }

University of Science Faculty of Information Technology Page 1


Lab 1. Basic Guide Fundamentals of Programming CSC10012

2. Data Types and Variables


Data types define the type of data that a variable can hold. Some built-in data types in C/C++:
Data Type Typical Size Typical Range
short 2 bytes −215 to 215 − 1
int 4 bytes −231 to 231 − 1
long 4 or 8 bytes −231 to 231 − 1 (4 bytes) or −263 to 263 − 1 (8 bytes)
long long 8 bytes −263 to 263 − 1
float 4 bytes −3.4 × 1038 to 3.4 × 1038
double 8 bytes −1.7 × 10308 to 1.7 × 10308
char 1 byte −27 to 27 − 1
bool 1 byte true or false

Notes: unsigned is used to define integer types that can only represent non-negative values, al-
lowing for a larger positive range compared to their default.

A variable must be declared with its data type and name prior to usage. The syntax is as follows:
data_type variable_name;
Variables can be initialized at the time of declaration. The syntax is as follow:
data_type variable_name = initial_value;
Example:
1 # include < iostream >
2 using namespace std ;
3

4 int main ()
5 {
6 int age = 30; // Integer variable
7 float salary = 50000.50; // Float variable
8 char grade = ’A ’; // Char variable
9 bool isEmployed = true ; // Boolean variable
10

11 cout << " Age : " << age << endl ;


12 cout << " Salary : " << salary << endl ;
13 cout << " Grade : " << grade << endl ;
14 cout << " Employed : " << ( isEmployed ? " Yes " : " No " ) << endl ;
15

16 return 0;
17 }

University of Science Faculty of Information Technology Page 2


Lab 1. Basic Guide Fundamentals of Programming CSC10012

3. Basic Input/Output
In C/C++, basic input and output are primarily performed using the standard library iostream.

• Input: To read data from the keyboard, use cin with extraction operator >>.

• Output: To write data to the console, use cout with insertion operator <<.

Example:
1 int number ; // Declare a variable to hold the integer
2

3 // Input number
4 cout << " Enter an integer : " ;
5 cin >> number ; // Read an integer
6

7 // Output information
8 cout << " You entered : " << number << endl ;

4. Basic Operators
C/C++ supports various operators for performing operations on data, including:

• Arithmetic Operators: + - * / %

• Assignment Operators: = += -= *= /= %=

• Comparison Operators: == != < > <= >=

• Logical Operators: && || !

• Bitwise Operators: & | << >> ∼ ∧

• Increment and Decrement Operator: ++ --

Example:
1 int a = 1 , b = 2;
2

3 cout << " The sum of a and b is : " << a + b << endl ;
4

5 int c = a * b ;
6 cout << " The product of a and b is : " << c << endl ;
7

8 return 0;

University of Science Faculty of Information Technology Page 3


Lab 1. Basic Guide Fundamentals of Programming CSC10012

5. Text File Handling


In C/C++, the fstream library is used for handling file stream, which provides functionalities to
read from and write to files. The main components used for file handling are:

• ifstream: Used to read data from a file.

• ofstream: Used to write data to a file.

• fstream: Used for both reading from and writing to a file.

To work with a file, follow these steps:

• Include the <fstream> library.


#include <fstream>

• Create an object of ifstream, ofstream, or fstream. For example:


ifstream fin;

• Open the file using open().


fin.open("input.txt");
File can be directly opened through the constructor.
ifstream fin("input.txt");

• Always check if the file was opened successfully using is_open().


1 if (! fin . is_open () )
2 {
3 cout << " Cannot open file ! " << endl ;
4 return 1;
5 }

• Perform reading or writing operations.

– Input: To read data from file, use extraction operator >>.


fin >> n; // Read n from file
– Output: To write data to the console, use insertion operator <<.
fout << "Open file successfully!" << endl; // Write a message to file

• Always close the file using close() once the work on it is complete.
fin.close();

University of Science Faculty of Information Technology Page 4


Lab 1. Basic Guide Fundamentals of Programming CSC10012

Exercises
Exercise 1. Hello, World!
Write a program that prints the line “Hello, World!”.

Exercise 2. Arithmetic of two numbers


Write a program to input two integers from the keyboard, then calculate and print their sum,
difference, product, quotient and remainder.
Example:

Input Output
5 2 5 + 2 = 7
5 - 2 = 3
5 * 2 = 10
5 / 2 = 2
5 % 2 = 1

Exercise 3: Value of an quadratic expression


Write a program to input a, b, c and x, then calculate and print value of ax2 + bx + c.
Example:

Input Output
1 2 3 4 27

Exercise 4. Average of three numbers


Write a program to read three integers from a file named as input.txt, then calculate and write
their average to a file named as output.txt.
Example:

input.txt output.txt
2 2 4 2.66667

University of Science Faculty of Information Technology Page 5


Lab 1. Basic Guide Fundamentals of Programming CSC10012

Exercise 5: Calculate the perimeter and area of a rectangle


Write a program to input the length and width of a rectangle, then print its perimeter and area.
Example:

Input Output
5 10 Perimeter: 30
Area: 50

Exercise 6: Calculate the circumference and area of a circle


Write a program to input the radius of a circle, then print its circumference and area, with π = 3.14.
Example:

Input Output
7 Circumference: 43.96
Area: 153.86

Exercise 7: Convert Celsius to Fahrenheit


Write a program to input a temperature in Celsius and convert it to Fahrenheit using the formula:

9
F = C + 32
5
Example:

Input Output
28 82.4

Exercise 8: Sum of the digits of a three digit number


Write a program to read a three digit integer from a file named as input.txt, then calculate and
write their sum of digits to a file named as output.txt.
Example:

input.txt output.txt
123 6

University of Science Faculty of Information Technology Page 6

You might also like