0% found this document useful (0 votes)
17 views28 pages

SOP 1st Lab Experiment

The document is a laboratory report for a Systems Programming course at SRM Institute of Science and Technology. It includes details about the student, experiments conducted using C++ programming, and the objectives of each program, such as demonstrating operators, conditional statements, pointers, and functions. The report also contains program outputs, pre-lab and post-lab questions, and a conclusion stating that the desired outputs were achieved.

Uploaded by

shahishnurahul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views28 pages

SOP 1st Lab Experiment

The document is a laboratory report for a Systems Programming course at SRM Institute of Science and Technology. It includes details about the student, experiments conducted using C++ programming, and the objectives of each program, such as demonstrating operators, conditional statements, pointers, and functions. The report also contains program outputs, pre-lab and post-lab questions, and a conclusion stating that the desired outputs were achieved.

Uploaded by

shahishnurahul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Laboratory Report Cover Sheet

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY


College of Engineering and Technology
Department of Electronics and Communication Engineering
21ECC112J– Systems Programming
II Semester, 2023 - 24 (Even Semester)

Name : R Shahishnu Rahul

Register No. : RA2411043010041

Class : I Year - BTech Electronics and Computer Engg – Section

Venue :

Title of Experiment :

Date of Conduction :

Date of Submission :

Marks
Particulars Max. Marks
Obtained

Pre Lab 05

Lab Performance 05

Post Lab 05

05
Record Submission
20
Total

REPORT VERIFICATION

Staff Name :

Signature :

1
Experiment No. 1

1 Develop and practice basic level C++ application programs

Aim / Objective: To write and execute basic C++ programs

Program 1: Explore all basic operators available in C++ using code


// R Shahishnu Rahul RA2411043010041
#include <iostream>
using namespace std;

int main() {
// Declare variables
int a = 10, b = 5;
bool x = true, y = false;

// Arithmetic operators
cout << "Arithmetic Operators:" << endl;
cout << "a + b = " << (a + b) << endl;
cout << "a - b = " << (a - b) << endl;
cout << "a * b = " << (a * b) << endl;
cout << "a / b = " << (a / b) << endl;
cout << "a % b = " << (a % b) << endl;

// Relational operators
cout << "\nRelational Operators:" << endl;
cout << "a == b: " << (a == b) << endl;
cout << "a != b: " << (a != b) << endl;
cout << "a > b: " << (a > b) << endl;
cout << "a < b: " << (a < b) << endl;
cout << "a >= b: " << (a >= b) << endl;
cout << "a <= b: " << (a <= b) << endl;

// Logical operators
cout << "\nLogical Operators:" << endl;
cout << "x && y: " << (x && y) << endl;
cout << "x || y: " << (x || y) << endl;
cout << "!x: " << (!x) << endl;

// Bitwise operators
cout << "\nBitwise Operators:" << endl;
cout << "a & b: " << (a & b) << endl;
cout << "a | b: " << (a | b) << endl;
cout << "a ^ b: " << (a ^ b) << endl;
cout << "~a: " << (~a) << endl;
cout << "a << 1: " << (a << 1) << endl;
2
cout << "a >> 1: " << (a >> 1) << endl;

// Assignment operators
cout << "\nAssignment Operators:" << endl;
int c = a;
cout << "c = " << c << endl;
c += b; cout << "c += b: " << c << endl;
c -= b; cout << "c -= b: " << c << endl;
c *= b; cout << "c *= b: " << c << endl;
c /= b; cout << "c /= b: " << c << endl;
c %= b; cout << "c %= b: " << c << endl;
c &= b; cout << "c &= b: " << c << endl;
c |= b; cout << "c |= b: " << c << endl;
c ^= b; cout << "c ^= b: " << c << endl;
c <<= 1; cout << "c <<= 1: " << c << endl;
c >>= 1; cout << "c >>= 1: " << c << endl;

// Ternary operator
cout << "\nTernary Operator:" << endl;
cout << "(a > b ? a : b): " << (a > b ? a : b) << endl;

// Increment and Decrement operators


cout << "\nIncrement and Decrement Operators:" << endl;
cout << "a++: " << a++ << " (post-increment)" << endl;
cout << "++a: " << ++a << " (pre-increment)" << endl;
cout << "b--: " << b-- << " (post-decrement)" << endl;
cout << "--b: " << --b << " (pre-decrement)" << endl;

return 0;
}

Program Output

Arithmetic Operators:
a + b = 15
a-b=5
a * b = 50
a/b=2
a%b=0

Relational Operators:
a == b: 0
a != b: 1
a > b: 1
a < b: 0
a >= b: 1
3
a <= b: 0

Logical Operators:
x && y: 0
x || y: 1
!x: 0

Bitwise Operators:
a & b: 0
a | b: 15
a ^ b: 15
~a: -11
a << 1: 20
a >> 1: 5

Assignment Operators:
c = 10
c += b: 15
c -= b: 10
c *= b: 50
c /= b: 10
c %= b: 0
c &= b: 0
c |= b: 5
c ^= b: 0
c <<= 1: 0
c >>= 1: 0

Ternary Operator:
(a > b ? a : b): 10

Increment and Decrement Operators:


a++: 10 (post-increment)
++a: 12 (pre-increment)
b--: 5 (post-decrement)
--b: 3 (pre-decrement)

4
Simulation Output

5
6
Program 2: Write a program in C++ to demonstrate all conditional statements.

//"SHAHISHNU RAHUL S RA2411043010041"


#include <iostream>
using namespace std;

int main() {
int number;
cout << "Enter a number: ";
cin >> number;

// Simple if statement
if (number > 0) {
cout << "The number is positive." << endl;
}

// If-else statement
if (number % 2 == 0) {
cout << "The number is even." << endl;
} else {
cout << "The number is odd." << endl;
}

// If-else if-else statement


if (number > 0) {
cout << "The number is positive." << endl;
} else if (number < 0) {
cout << "The number is negative." << endl;
} else {
cout << "The number is zero." << endl;
}

// Nested if statement
if (number != 0) {
if (number > 0) {
cout << "The number is positive and non-zero." << endl;
} else {
cout << "The number is negative and non-zero." << endl;
}
}

// Switch statement
cout << "Enter a choice (1-3): ";
int choice;
cin >> choice;
switch (choice) {
7
case 1:
cout << "You selected option 1." << endl;
break;
case 2:
cout << "You selected option 2." << endl;
break;
case 3:
cout << "You selected option 3." << endl;
break;
default:
cout << "Invalid choice." << endl;
break;
}

return 0;
}

Program Output

Enter a number: 8
The number is positive.
The number is even.
The number is positive.
The number is positive and non-zero.
Enter a choice (1-3): 2
You selected option 2.

8
SimulationOutput

9
10
11
Program 3: Write a program in C++ to demonstrate References and Pointers

//"SHAHISHNU RAHUL S RA2411043010041"


#include <iostream>
using namespace std;

void modifyWithPointer(int *ptr) {


*ptr = *ptr + 10; // Modify value using pointer
}

void modifyWithReference(int &ref) {


ref = ref + 10; // Modify value using reference
}

int main() {
int num = 20;

// Demonstrating pointers
int *ptr = &num; // Pointer stores the address of num
cout << "Using Pointers:" << endl;
cout << "Value of num: " << num << endl;
cout << "Address of num: " << &num << endl;
cout << "Value using pointer: " << *ptr << endl;
cout << "Pointer address: " << ptr << endl;

*ptr = 30; // Modifying value using pointer


cout << "Value of num after modification via pointer: " << num << endl;

modifyWithPointer(ptr);
cout << "Value of num after function call via pointer: " << num << endl;

// Demonstrating references
int &ref = num; // Reference to num
cout << "\nUsing References:" << endl;
cout << "Value of num: " << num << endl;
cout << "Reference value: " << ref << endl;

ref = 40; // Modifying value using reference


cout << "Value of num after modification via reference: " << num << endl;

modifyWithReference(ref);
cout << "Value of num after function call via reference: " << num << endl;

// Comparing pointer and reference properties


int anotherNum = 50;
ptr = &anotherNum; // Pointers can be reassigned
cout << "\nPointer can be reassigned to another variable, ptr now points to: " << *ptr << endl;
12
// References cannot be changed to refer to another variable
cout << "Reference still refers to num: " << ref << endl;

return 0;
}

Program Output

Using Pointers:
Value of num: 20
Address of num: 0x7fff191b682c
Value using pointer: 20
Pointer address: 0x7fff191b682c
Value of num after modification via pointer: 30
Value of num after function call via pointer: 40

Using References:
Value of num: 40
Reference value: 40
Value of num after modification via reference: 40
Value of num after function call via reference: 50

Pointer can be reassigned to another variable, ptr now points to: 50


Reference still refers to num: 50

13
Simulation Output

14
15
Pre lab Questions:
1. What are the different data types present in C++?
2. What is the difference between C and C++?
3. What is polymorphism in C++?
4. What is the use of pointers?
5. How do references help in simplifying the code?
Post lab Questions:
1. Why do we include namespace std?
2. Differentiate between cout and cin functions.
3. Why do we use return 0 at the end of main function
4. Suppose, we want a pointer varPoint to point to the address of var. Then, which of the
following is correct. If wrong, give the reason and correct all the wrong statements.

a) int var, *varPoint;


b) varPoint = var;
c) *varPoint = &var;
d) varPoint = &var;
e) *varPoint = var;

Results and Conclusion


The basic C++ Programs were executed and desired output was obtained.

16
Experiment No.3

3 Develop and practice C++ application programs using functions

Aim / Objective: To write and execute C++ programs to demonstrate the use functions.
Simulate the program in C++ compiler.

Program 1: Write a C++ program using functions (Call by value method)


//RA2411043010041 SHAHISHNU RAHUL
#include <iostream>
using namespace std;

int main() {
int num;

// Input a number from the user


cout << "Enter a number: ";
cin >> num;

// 1. If statement
if (num > 0) {
cout << "The number is positive." << endl;
}

// 2. If-Else statement
if (num % 2 == 0) {
cout << "The number is even." << endl;
} else {
cout << "The number is odd." << endl;
}

// 3. If-Else If ladder
if (num > 0) {
cout << "The number is positive." << endl;
} else if (num < 0) {
cout << "The number is negative." << endl;
} else {
cout << "The number is zero." << endl;
}

// 4. Switch statement
cout << "\nEnter a day number (1-7): ";
int day;
cin >> day;
switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:

17
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
default:
cout << "Invalid day number!" << endl;
}

return 0;
}

Program Output

Enter a number: 2
The number is positive.
The number is even.
The number is positive.

Enter a day number (1-7): 4


Thursday

=== Code Execution Successful ===

18
Simulation Output

19
20
21
Experiment No.2

2 Develop and practice C++ application programs using functions

Aim / Objective: To write and execute C++ programs to demonstrate the use functions.
Simulate the program in C++ compiler.

Program 1: Write a C++ program using functions (Call by value method)

//RA2411043010041 Shahishnu Rahul


#include <iostream>
using namespace std;
void swap(int x, int y)
{
int t = x;
x = y;
y = t;
cout << "After Swapping in function x: " << x
<< ", y: " << y << endl;
}
int main()
{
int x = 1, y = 2;

cout << "Before Swapping: ";


cout << "x: " << x << ", y: " << y << endl;

swap(x, y);

cout << "After Swapping: ";


cout << "x: " << x << ", y: " << y << endl;

return 0;
}

Program Output
Before Swapping: x: 1, y: 2
After Swapping in function x: 2, y: 1
After Swapping: x: 1, y: 2

=== Code Execution Successful ===

22
Simulation Output

23
Program 2: Write a C++ program using functions (Call by reference method)
//RA2411043010041 Shahishnu Rahul
#include <iostream>
using namespace std;
void swapNumbers(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}

int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Before swapping: num1 = " << num1 << ", num2 = " << num2 << endl;

swapNumbers(num1, num2);

cout << "After swapping: num1 = " << num1 << ", num2 = " << num2 << endl;

return 0;
}

Program Output
Enter two numbers: 1 2
Before swapping: num1 = 1, num2 = 2
After swapping: num1 = 2, num2 = 1

=== Code Execution Successful ===

24
Simulation Output

25
Write a program for npr factorial

26
27
Pre lab Questions
1. Why do we need functions?
2. What are function arguments?
3. What is function overloading?
4. Can we have a function with no return value?
Post lab questions
1. Differentiate between user defined and library functions with examples.
2. What is the return value for main()?
3. What is difference in precision between float and double data type?

Results and Conclusion


The C++ Programs on functions were executed and desired output was obtained.

28

You might also like