Skip to content
1 change: 1 addition & 0 deletions week1/question3.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#include <iostream>
#include <cstdlib>

Expand Down
40 changes: 40 additions & 0 deletions week2/question1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
using namespace std;

int main() {
int n1, n2;

cout << "Enter size of first array: ";
cin >> n1;
int arr1[n1];

cout << "Enter elements of first array:\n";
for (int i = 0; i < n1; i++) {
cin >> arr1[i];
}

cout << "Enter size of second array: ";
cin >> n2;
int arr2[n2];

cout << "Enter elements of second array:\n";
for (int i = 0; i < n2; i++) {
cin >> arr2[i];
}

int merged[n1 + n2];
for (int i = 0; i < n1; i++) {
merged[i] = arr1[i];
}
for (int i = 0; i < n2; i++) {
merged[n1 + i] = arr2[i];
}

cout << "Merged array in reverse order:\n";
for (int i = n1 + n2 - 1; i >= 0; i--) {
cout << merged[i] << " ";
}
cout << endl;

return 0;
}
42 changes: 42 additions & 0 deletions week2/question2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
using namespace std;

void sortArray(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}

void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

int main() {
int n;

cout << "Enter the size of the array: ";
cin >> n;

int arr[n];

cout << "Enter " << n << " elements:\n";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

sortArray(arr, n);

cout << "Sorted array in ascending order:\n";
printArray(arr, n);

return 0;
}
28 changes: 28 additions & 0 deletions week2/question3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>
using namespace std;

int main() {
int n;

cout << "Enter the size of the array: ";
cin >> n;

int original[n], copy[n];

cout << "Enter " << n << " elements:\n";
for (int i = 0; i < n; i++) {
cin >> original[i];
}

for (int i = 0; i < n; i++) {
copy[i] = original[i];
}

cout << "Copied array elements:\n";
for (int i = 0; i < n; i++) {
cout << copy[i] << " ";
}
cout << endl;

return 0;
}
34 changes: 34 additions & 0 deletions week2/question4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
using namespace std;

int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;

int arr[n];
cout << "Enter " << n << " elements:\n";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int maxCount = 0;
int maxElement = arr[0];

for (int i = 0; i < n; i++) {
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
}
}
if (count > maxCount) {
maxCount = count;
maxElement = arr[i];
}
}

cout << "Maximum occurring integer is: " << maxElement << endl;
cout << "It occurs " << maxCount << " times." << endl;

return 0;
}
41 changes: 41 additions & 0 deletions week2/question5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>
#include <climits>
using namespace std;

int main() {
int n;

cout << "Enter the size of the array: ";
cin >> n;

if (n < 2) {
cout << "Array must contain at least two elements." << endl;
return 1;
}

int arr[n];
cout << "Enter " << n << " distinct elements:\n";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

int smallest = INT_MAX;
int secondSmallest = INT_MAX;

for (int i = 0; i < n; i++) {
if (arr[i] < smallest) {
secondSmallest = smallest;
smallest = arr[i];
} else if (arr[i] < secondSmallest && arr[i] != smallest) {
secondSmallest = arr[i];
}
}

if (secondSmallest == INT_MAX) {
cout << "There is no second smallest element (all elements may be equal)." << endl;
} else {
cout << "Second smallest element is: " << secondSmallest << endl;
}

return 0;
}
66 changes: 66 additions & 0 deletions week2/question6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <iostream>
using namespace std;

const int SIZE = 3;

void inputMatrix(int matrix[SIZE][SIZE], const string& name) {
cout << "Enter elements of " << name << " matrix (3x3):\n";
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
cin >> matrix[i][j];
}
}
}

void displayMatrix(int matrix[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
cout << matrix[i][j] << "\t";
}
cout << endl;
}
}

void addMatrices(int a[SIZE][SIZE], int b[SIZE][SIZE], int result[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
result[i][j] = a[i][j] + b[i][j];
}
}
}

void transposeMatrix(int matrix[SIZE][SIZE], int transpose[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
transpose[j][i] = matrix[i][j];
}
}
}

int main() {
int mat1[SIZE][SIZE], mat2[SIZE][SIZE];
int sumOriginal[SIZE][SIZE];
int trans1[SIZE][SIZE], trans2[SIZE][SIZE], sumTranspose[SIZE][SIZE];

inputMatrix(mat1, "first");
inputMatrix(mat2, "second");

addMatrices(mat1, mat2, sumOriginal);
cout << "\nSum of the original matrices:\n";
displayMatrix(sumOriginal);

transposeMatrix(mat1, trans1);
transposeMatrix(mat2, trans2);

cout << "\nTranspose of first matrix:\n";
displayMatrix(trans1);

cout << "\nTranspose of second matrix:\n";
displayMatrix(trans2);

addMatrices(trans1, trans2, sumTranspose);
cout << "\nSum of the transposed matrices:\n";
displayMatrix(sumTranspose);

return 0;
}
23 changes: 23 additions & 0 deletions week2/question7.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>
using namespace std;

int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
int n;
cout << "Enter the position (n) to find the nth Fibonacci number: ";
cin >> n;

if (n < 0) {
cout << "Please enter a non-negative integer." << endl;
} else {
int result = fibonacci(n);
cout << "Fibonacci number at position " << n << " is: " << result << endl;
}

return 0;
}
24 changes: 24 additions & 0 deletions week2/question8.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>
using namespace std;

// Recursive function to find sum of digits
int sumOfDigits(int n) {
if (n == 0)
return 0;
return (n % 10) + sumOfDigits(n / 10);
}

int main() {
int number;

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

if (number < 0) number = -number; // Handle negative input

int result = sumOfDigits(number);

cout << "Sum of digits: " << result << endl;

return 0;
}
30 changes: 30 additions & 0 deletions week2/question9.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
using namespace std;

void printArray(int arr[], int index, int size) {
if (index == size) {
return;
}

cout << arr[index] << " ";
printArray(arr, index + 1, size);
}

int main() {
int n;

cout << "Enter the size of the array: ";
cin >> n;

int arr[n];

cout << "Enter " << n << " elements:\n";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

cout << "Array elements are:\n";
printArray(arr, 0, n);

return 0;
}
33 changes: 33 additions & 0 deletions week3/question1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
using namespace std;

void moveNegatives(int arr[], int n) {
int j = 0;
for (int i = 0; i < n; i++) {
if (arr[i] < 0) {
swap(arr[i], arr[j]);
j++;
}
}
}

void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

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

moveNegatives(arr, n);
printArray(arr, n);

return 0;
}
Loading