0% found this document useful (0 votes)
7 views10 pages

Lab Report 11

The document contains a lab report on programming fundamentals using C++, detailing various tasks that involve pointer manipulation, swapping values, and working with arrays. Each task includes C++ code snippets and expected outputs, demonstrating concepts such as memory addresses, input/output operations, and array traversal. The report covers a total of eight lab tasks, each focusing on different aspects of C++ programming.

Uploaded by

tahajalil1074
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)
7 views10 pages

Lab Report 11

The document contains a lab report on programming fundamentals using C++, detailing various tasks that involve pointer manipulation, swapping values, and working with arrays. Each task includes C++ code snippets and expected outputs, demonstrating concepts such as memory addresses, input/output operations, and array traversal. The report covers a total of eight lab tasks, each focusing on different aspects of C++ programming.

Uploaded by

tahajalil1074
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/ 10

1/14/2025 Taha Jalil

240201010

Lab Report 11
PROGRAMMING FUNDAMENTALS
Lab Task 1 :

C++ Statement :
#include <iostream>

using namespace std;

int main() {

int var1, *ptr;

ptr = &var1;

var1 = 10;

cout << var1 << endl;

*ptr = 20;

cout << *ptr << endl;

return 0;

Output :
Lab Task 2 :

Output :
Lab Task 3 :

C++ Statement :
#include <iostream>
using namespace std;

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int x = 10, y = 20;

cout << "Before Swap: x = " << x << ", y = " << y << endl;

swap(&x, &y);

cout << "After Swap: x = " << x << ", y = " << y << endl;

return 0; }

Output :
Lab Task 4 :
Write a C++ program that inputs four floating-point values and displays the memory
address of all the values using pointers. Also, display the values using pointers.

C++ Statement :
#include <iostream>
using namespace std;

int main() {
float val1, val2, val3, val4;

cout << "Enter four floating-point values: " << endl;


cin >> val1 >> val2 >> val3 >> val4;

float *ptr1 = &val1;


float *ptr2 = &val2;
float *ptr3 = &val3;
float *ptr4 = &val4;

cout << "\nMemory Addresses:" << endl;


cout << "Address of val1: " << ptr1 << endl;
cout << "Address of val2: " << ptr2 << endl;
cout << "Address of val3: " << ptr3 << endl;
cout << "Address of val4: " << ptr4 << endl;

cout << "\nValues using pointers:" << endl;


cout << "Value of val1: " << *ptr1 << endl;
cout << "Value of val2: " << *ptr2 << endl;
cout << "Value of val3: " << *ptr3 << endl;
cout << "Value of val4: " << *ptr4 << endl;

return 0;
}
Output :

Lab Task 5 :
Write a program that inputs a string value from user and displays it using pointer.

C++ Statement :
#include <iostream>
using namespace std;

int main() {
string str;

cout << "Enter a string: ";


getline(cin, str);

string *ptr = &str;

cout << "\nString using pointer: " << *ptr << endl;

return 0;
}
Output :

Lab Task 6 :
Write a C++ program that:
Creates an array of integers with 5 elements.
Uses a pointer to sum the elements of the array.
Prints the sum of the array.

C++ Statement :
#include <iostream>
using namespace std;

int main() {
int arr[5];
int *ptr = arr;
int sum = 0;

cout << "Enter 5 integers: ";


for (int i = 0; i < 5; i++) {
cin >> arr[i];
}

for (int i = 0; i < 5; i++) {


sum += *(ptr + i);
}
cout << "Sum of the array elements: " << sum << endl;

return 0;
}
Output :

Lab Task 7 :
Write a C++ program that:
Creates an array of integers with 10 elements.
Uses a pointer to traverse through the array and print each element.
Uses pointers to reverse the array and print the reversed array.

C++ Statement :
#include <iostream>
using namespace std;

int main() {
int arr[10];

cout << "Enter 10 integers: ";


for (int i = 0; i < 10; i++) {
cin >> arr[i];
}

int *ptr = arr;

cout << "\nOriginal Array: ";


for (int i = 0; i < 10; i++) {
cout << *(ptr + i) << " ";
}

int *start = arr;


int *end = arr + 9;
while (start < end) {
int temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}

cout << "\nReversed Array: ";


for (int i = 0; i < 10; i++) {
cout << *(ptr + i) << " ";
}

return 0;
}

Output :
Lab Task 8 :
Write a C++ program that:
Defines a function swap() that takes two integer pointers and swaps the values of the
integers.
In the main() function, initialize two integer variables and pass their addresses to the
swap() function to swap their values.
Print the values before and after the swap

C++ Statement :

#include <iostream>
using namespace std;

void swap(int* a, int* b) {


int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int num1 = 10, num2 = 20;

cout << "Before swap:" << endl;


cout << "num1 = " << num1 << ", num2 = " << num2 << endl;

swap(&num1, &num2);

cout << "After swap:" << endl;


cout << "num1 = " << num1 << ", num2 = " << num2 << endl;

return 0; }

Output :

You might also like