[go: up one dir, main page]

0% found this document useful (0 votes)
14 views4 pages

Data Structures Assessment - 1

The document contains four C++ programming tasks: multiplying matrices, sorting a vector without using the sort function, calculating the Fibonacci series using both loop and recursive approaches, and calculating prefix and suffix sums for an array. Each task includes a complete code solution demonstrating the required functionality. The solutions utilize standard C++ libraries and basic programming constructs.

Uploaded by

gourav39rohilla
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)
14 views4 pages

Data Structures Assessment - 1

The document contains four C++ programming tasks: multiplying matrices, sorting a vector without using the sort function, calculating the Fibonacci series using both loop and recursive approaches, and calculating prefix and suffix sums for an array. Each task includes a complete code solution demonstrating the required functionality. The solutions utilize standard C++ libraries and basic programming constructs.

Uploaded by

gourav39rohilla
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/ 4

Que-1 Write a C++ program to apply multiplication of matrices of two matrices consider the

conditions required for multiplication of two matrices in terms of matrix size.

Solution:
#include <iostream>
using namespace std;

void multiplyMatrices(int A[][10], int B[][10], int C[][10], int r1, int c1, int
r2, int c2) {
if (c1 != r2) {
cout << "Matrix multiplication is not possible";
return;
}
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
C[i][j] = 0;
for (int k = 0; k < c1; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}

int main() {
int A[10][10], B[10][10], C[10][10], r1, c1, r2, c2;
cout << "Enter rows and columns of first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns of second matrix: ";
cin >> r2 >> c2;

cout << "Enter first matrix elements:";


for (int i = 0; i < r1; i++)
for (int j = 0; j < c1; j++)
cin >> A[i][j];

cout << "Enter second matrix elements:";


for (int i = 0; i < r2; i++)
for (int j = 0; j < c2; j++)
cin >> B[i][j];

multiplyMatrices(A, B, C, r1, c1, r2, c2);

cout << "Result matrix:";


for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++)
cout << C[i][j] << " ";
cout << endl;
}
return 0;
}
Que-2 Write a C++ program to sort a vector without using sort function.

Solution:
#include <iostream>
#include <vector>
using namespace std;

void bubbleSort(vector<int>& arr) {


int n = arr.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}

int main() {
vector<int> arr = {5, 3, 8, 6, 2, 7};
bubbleSort(arr);
for (int num : arr) {
cout << num << " ";
}
return 0;
}
Que-3 Write a C++ program to calculate the Fibonacci series using a) loop approach and b) recursive
approach.

Solution:
#include <iostream>
using namespace std;

void fibonacciLoop(int n) {
int a = 0, b = 1, c;
cout << a << " " << b << " ";
for (int i = 2; i < n; i++) {
c = a + b;
cout << c << " ";
a = b;
b = c;
}
}

int fibonacciRecursive(int n) {
if (n <= 1) return n;
return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);
}

int main() {
int n;
cout << "Enter the number of terms: ";
cin >> n;
fibonacciLoop(n);
cout << endl << "Recursive: ";
for (int i = 0; i < n; i++) {
cout << fibonacciRecursive(i) << " ";
}
return 0;
}
Que-4 Write a C++ program to calculate the suffix array sum and prefix array sum for a given input
array.

Solution:
#include <iostream>
#include <vector>
using namespace std;

void prefixSuffixSum(const vector<int>& arr) {


int n = arr.size();
vector<int> prefix(n), suffix(n);

prefix[0] = arr[0];
for (int i = 1; i < n; i++) {
prefix[i] = prefix[i - 1] + arr[i];
}

suffix[n - 1] = arr[n - 1];


for (int i = n - 2; i >= 0; i--) {
suffix[i] = suffix[i + 1] + arr[i];
}

cout << "Prefix Sum: ";


for (int sum : prefix) cout << sum << " ";
cout << endl;

cout << "Suffix Sum: ";


for (int sum : suffix) cout << sum << " ";
cout << endl;
}

int main() {
vector<int> arr = {1, 2, 3, 4, 5};
prefixSuffixSum(arr);
return 0;
}

You might also like