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

Document 1

The document contains C++ code snippets for solving various algorithm problems. It includes functions to find a missing number in an array, count instances of numbers in an array, swap two numbers without a temporary variable, check if a number is a power of two, find the student with the highest GPA, and implement binary search on a sorted array.

Uploaded by

mahnoorzahiid09
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)
15 views

Document 1

The document contains C++ code snippets for solving various algorithm problems. It includes functions to find a missing number in an array, count instances of numbers in an array, swap two numbers without a temporary variable, check if a number is a power of two, find the student with the highest GPA, and implement binary search on a sorted array.

Uploaded by

mahnoorzahiid09
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/ 9

ASSIGNMENT HOMEWORK

Question #1:

#include <iostream>

using namespace std;

// Function to find the missing number

int findMissingNumber(int arr[], int n) {

// XOR of all elements from 1 to N

int xor1 = 1;

for (int i = 2; i <= n; i++) {

xor1 ^= i;

// XOR of all elements in the array

int xor2 = arr[0];

for (int i = 1; i < n - 1; i++) {

xor2 ^= arr[i];

// XOR of both gives the missing number

return xor1 ^ xor2;

int main() {
int arr[] = {5, 4, 2, 1};

int n = sizeof(arr) / sizeof(arr[0]) + 1; // size of array + 1

int missingNumber = findMissingNumber(arr, n);

cout << "The missing number is: " << missingNumber << endl;

return 0;

Question #2:
#include <iostream>

#include <vector>

using namespace std;

// Function to count instances using bitwise operators

void countInstances(int arr[], int n, vector<int>& counts) {

int index = 0;

int count = 1;

for (int i = 1; i < n; i++) {

if (arr[i] == arr[i - 1]) {

count++;

} else {

counts[index++] = count;

count = 1;

counts[index] = count; // count of the last number

}
int main() {

int arr[] = {1, 1, 2, 2, 2, 3, 4, 4, 4, 4};

int n = sizeof(arr) / sizeof(arr[0]);

// Count the number of instances for each number

vector<int> counts(n, 0);

countInstances(arr, n, counts);

// Display the counts

cout << "Counts of instances for each number:" << endl;

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

cout << arr[i] << ": " << counts[i] << endl;

return 0;

Question #3:

#include <iostream>

using namespace std;

void swapNumbers(int &a, int &b) {

// XOR operation to swap numbers without temporary variable

a = a ^ b;

b = a ^ b;
a = a ^ b;

int main() {

int num1 = 5, num2 = 10;

cout << "Before swapping: " << endl;

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

swapNumbers(num1, num2);

cout << "After swapping: " << endl;

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

return 0;

Question #4:
#include <iostream>

using namespace std;

bool isPowerOfTwo(int num) {

// A number is a power of 2 if it has only one bit set to 1.

// So, we can use the bitwise AND operation with (num - 1).

// If the result is 0, then it's a power of 2.


// For example, if num = 8 (1000 in binary), then num - 1 = 7 (0111 in binary).

// Performing num & (num - 1) would result in 0.

return num > 0 && (num & (num - 1)) == 0;

int main() {

int num;
cout << "Enter a number: ";

cin >> num;

if (isPowerOfTwo(num)) {

cout << num << " is a power of 2." << endl;

} else {

cout << num << " is not a power of 2." << endl;

return 0;

Question# 8:
Part a:
#include <iostream>

using namespace std;

int findMaxGPAIndex(float arr[], int size) {


int maxIndex = 0;

for (int i = 1; i < size; ++i) {

if (arr[i] > arr[maxIndex]) {

maxIndex = i;

return maxIndex;

int main() {

float cgpa[] = {2.1, 3.8, 3.9, 3.7}; // Sample CGPA array

int size = sizeof(cgpa) / sizeof(cgpa[0]);

int maxIndex = findMaxGPAIndex(cgpa, size);

cout << "Roll number of student with highest GPA: " << maxIndex << endl;

return 0;

Part b:
1. 1. START
2. 2 A sorted array arr of floating-point numbers.
3. 3 An integer size indicating the size of the array arr.
4. 4 A floating-point target to find within the array.

PROCEDURE

1. 1 Initialize two pointers low and high at the beginning and end of the array,
respectively.
2. While low is less than or equal to high:
3. Determine the index of the middle element as mid = (low + high) / 2.
4. If arr[mid] equals target, return mid.
5. If arr[mid] is less than target, update low = mid + 1 to search in the right half.
6. If arr[mid] is greater than target, update high = mid - 1 to search in the left half.
7. If the target isn't found, return -1.
8.

3. OUTPUT

- Return the index of target in arr if found, otherwise return -1.

This algorithm efficiently discovers a target value within a sorted array by repeatedly dividing
the search space in half until the target is located or the search range is exhausted.

Part c:
#include <iostream>

using namespace std;

int binarySearch(float arr[], int size, float target) {

int low = 0, high = size - 1;

while (low <= high) {

int mid = low + (high - low) / 2;

if (arr[mid] == target) {
return mid;

} else if (arr[mid] < target) {

low = mid + 1;

} else {

high = mid - 1;

return -1; // Not found


}

int main() {

float cgpa[] = {3.7, 3.8, 3.9, 4.0}; // Sample sorted CGPA array

int size = sizeof(cgpa) / sizeof(cgpa[0]);

float target = 4.0; // GPA to search for

int index = binarySearch(cgpa, size, target);

if (index != -1) {

cout << "Roll number of student with GPA 4.0: " << index << endl;
} else {

cout << "Student with GPA 4.0 not found." << endl;

return 0;

Part d:

GRAPH

Linear Search:
| *

| *

| *

| *

| *

*----------------------------> Array Index


Binary Search (requires sorted data):
| *

| *

| *

| *

| *

| *

*----------------------------> Array Index

You might also like