Lab 1
Lab 1
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
9 int main ()
10 {
11 // Main function starts here
12 cout << " CSC10012 ! " << endl ; // Statements , expressions are here
13 return 0; // Return statement
14 }
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
16 return 0;
17 }
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: = += -= *= /= %=
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;
• Always close the file using close() once the work on it is complete.
fin.close();
Exercises
Exercise 1. Hello, World!
Write a program that prints the line “Hello, World!”.
Input Output
5 2 5 + 2 = 7
5 - 2 = 3
5 * 2 = 10
5 / 2 = 2
5 % 2 = 1
Input Output
1 2 3 4 27
input.txt output.txt
2 2 4 2.66667
Input Output
5 10 Perimeter: 30
Area: 50
Input Output
7 Circumference: 43.96
Area: 153.86
9
F = C + 32
5
Example:
Input Output
28 82.4
input.txt output.txt
123 6