0% found this document useful (0 votes)
6 views

Assignment 8 (1)

C++ code

Uploaded by

rayyanquddusi683
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)
6 views

Assignment 8 (1)

C++ code

Uploaded by

rayyanquddusi683
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/ 12

Shahmeer Haidrani DEE 24-28

Computer Fundamental and Programming

Fall-2024
Grading:
CLO1 CLO2 CLO3 Total

Comments:

1st semester Date of Submission:


26/12/2024
Title:
Assignment No. 08
Students’ Name:
Muhammad Shahmeer

Batch: Teacher:
BSEE 2024-28 Dr. M.Tufail

Section: “A” Roll no: BS-24-LB-103504

1
Shahmeer Haidrani DEE 24-28

Department of Electrical
Engineering

Q NO#01
CODE:
#include <iostream>
using namespace std;
int main() {
int num;
int* ptr;
cout << "Enter a number: ";
cin >> num;
ptr = &num;
cout << "The value of num: " << num << endl;
cout << "The value of num using the pointer: " << *ptr << endl;
cout << "The address of num: " << &num << endl;
cout << "The address of num using the pointer: " << ptr << endl;
int newValue;
cout << "Enter a new value for num: ";
cin >> newValue;
*ptr = newValue;
cout << "The updated value of num: " << num << endl;
return 0;
}

OUTPUT:

2
Shahmeer Haidrani DEE 24-28

Q NO#02
CODE:
#include <iostream>
using namespace std;
int main() {
int n;
float* arr;
cout << "Enter the number of elements: ";
cin >> n;
arr = new float[n];
cout << "Enter " << n << " numbers:" << endl;
for (int i = 0; i < n; ++i) {
cout << "Number " << i + 1 << ": ";
cin >> *(arr + i);
}
float sum = 0;
for (int i = 0; i < n; ++i) {
sum += *(arr + i);
}
float average = sum / n;
cout << "The average of the numbers is: " << average << endl;
return 0;

3
Shahmeer Haidrani DEE 24-28

OUTPUT:

Q NO#03
CODE:
#include <iostream>
using namespace std;
int main() {
float num1, num2, sum;
float* ptr1, *ptr2, *ptrSum;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
ptr1 = &num1;
ptr2 = &num2;
ptrSum = &sum;
*ptrSum = *ptr1 + *ptr2;
cout << "The sum of " << *ptr1 << " and " << *ptr2 << " is: " << *ptrSum << endl;
return 0;
}

OUTPUT:

4
Shahmeer Haidrani DEE 24-28

Q NO#04
CODE:
#include <iostream>
using namespace std;
int main() {
int num1, num2;
int *ptr1, *ptr2;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
ptr1 = &num1;
ptr2 = &num2;
cout << "Before swapping: " << endl;
cout << "num1 = " << *ptr1 << ", num2 = " << *ptr2 << endl;
int temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
cout << "After swapping: " << endl;
cout << "num1 = " << *ptr1 << ", num2 = " << *ptr2 << endl;
return 0;
}

OUTPUT:

Q NO#05

5
Shahmeer Haidrani DEE 24-28

CODE:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int* arr = new int[n];
cout << "Enter " << n << " elements:" << endl;
for (int i = 0; i < n; ++i) {
cout << "Element " << i + 1 << ": ";
cin >> *(arr + i);
}
cout << "Array elements are: " << endl;
for (int i = 0; i < n; ++i) {
cout << "Element " << i + 1 << ": " << *(arr + i) << endl;
}
return 0;
}

OUTPUT:

Q NO#06

6
Shahmeer Haidrani DEE 24-28

CODE:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int* arr1 = new int[n];
int* arr2 = new int[n];
cout << "Enter " << n << " elements:" << endl;
for (int i = 0; i < n; ++i) {
cout << "Element " << i + 1 << ": ";
cin >> *(arr1 + i);
}
for (int i = 0; i < n; ++i) {
*(arr2 + i) = *(arr1 + i);
}
cout << "Original array elements are: " << endl;
for (int i = 0; i < n; ++i) {
cout << "Element " << i + 1 << ": " << *(arr1 + i) << endl;
}
cout << "Copied array elements are: " << endl;
for (int i = 0; i < n; ++i) {
cout << "Element " << i + 1 << ": " << *(arr2 + i) << endl;
}
return 0;
}

OUTPUT:

7
Shahmeer Haidrani DEE 24-28

Q NO#07
CODE:
#include <iostream>
using namespace std;
int main() {
int n, searchElement;
bool found = false;
cout << "Enter the number of elements in the array: ";
cin >> n;
int* arr = new int[n];
cout << "Enter " << n << " elements:" << endl;
for (int i = 0; i < n; ++i) {
cout << "Element " << i + 1 << ": ";
cin >> *(arr + i);
}
cout << "Enter the element to search for: ";
cin >> searchElement;
for (int i = 0; i < n; ++i) {
if (*(arr + i) == searchElement) {
found = true;

8
Shahmeer Haidrani DEE 24-28

cout << "Element " << searchElement << " found at index " << i << endl;
break;
}
}
if (!found) {
cout << "Element " << searchElement << " not found in the array." << endl;
}
return 0;
}

OUTPUT#1:

OUTPUT#2:

Q NO#08
CODE:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements in the array: ";

9
Shahmeer Haidrani DEE 24-28

cin >> n;
int* arr = new int[n];
cout << "Enter " << n << " elements:" << endl;
for (int i = 0; i < n; ++i) {
cout << "Element " << i + 1 << ": ";
cin >> *(arr + i);
}
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
if (*(arr + j) > *(arr + j + 1)) {
int temp = *(arr + j);
*(arr + j) = *(arr + j + 1);
*(arr + j + 1) = temp;
}
}
}
cout << "Sorted array is: " << endl;
for (int i = 0; i < n; ++i) {
cout << *(arr + i) << " ";
}
cout << endl;
return 0;
}

OUTPUT:

10
Shahmeer Haidrani DEE 24-28

Q NO#09
CODE:
#include <iostream>
using namespace std;
void calculate(int a, int b, int* sum, int* diff, int* prod, float* div, int* rem) {
*sum = a + b;
*diff = a - b;
*prod = a * b;
if (b != 0) {
*div = static_cast<float>(a) / b;
*rem = a % b;
} else {
*div = 0;
*rem = 0;
}
}
int main() {
int num1, num2;
int sum, diff, prod, rem;
float div;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
calculate(num1, num2, &sum, &diff, &prod, &div, &rem);
cout << "Sum of " << num1 << " and " << num2 << " is: " << sum << endl;
cout << "Difference of " << num1 << " and " << num2 << " is: " << diff << endl;
cout << "Product of " << num1 << " and " << num2 << " is: " << prod << endl;
if (num2 != 0) {

11
Shahmeer Haidrani DEE 24-28

cout << "Division of " << num1 << " and " << num2 << " is: " << div << endl;
cout << "Remainder of " << num1 << " and " << num2 << " is: " << rem << endl;
} else {
cout << "Division by zero is not allowed!" << endl;
cout << "Remainder by zero is not defined!" << endl;
}
return 0;
}

OUTPUT#1:

OUTPUT#2:

**************************END**************************

12

You might also like