0% found this document useful (0 votes)
10 views7 pages

Assignment-2

The document contains multiple C++ programming experiments including matrix addition, subtraction, and multiplication, as well as array element counting and rectangle area and perimeter calculations. Each experiment includes code snippets that prompt user input for dimensions and elements, perform calculations, and display results. The code demonstrates fundamental programming concepts such as loops, conditionals, and class usage.

Uploaded by

Abhikansh Mital
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)
10 views7 pages

Assignment-2

The document contains multiple C++ programming experiments including matrix addition, subtraction, and multiplication, as well as array element counting and rectangle area and perimeter calculations. Each experiment includes code snippets that prompt user input for dimensions and elements, perform calculations, and display results. The code demonstrates fundamental programming concepts such as loops, conditionals, and class usage.

Uploaded by

Abhikansh Mital
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/ 7

Assignment -2

Experiment - 4

a. #include <iostream>
using namespace std;
int main() {
// your code goes here
cout << "enter the nos: " << endl;
int n, m;
cin >> n >> m;
int arr[n][m];
int brr[n][m];
cout << "Enter first array: " << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> arr[i][j];
}
}
cout << "Enter secound array: " << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> brr[i][j];
}
}
cout << "Enter a for add and s for sub" << endl;
char ch;
cin >> ch;
if (ch == 'a')
{
cout << "Final array: " << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << arr[i][j] + brr[i][j] << " ";
}
cout << endl;
}
}
else
{
cout << "Final array: " << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << arr[i][j] - brr[i][j] << " ";
}
cout << endl;
}
}

b. #include <iostream>
using namespace std;
int main() {
// your code goes here
cout << "Enter the dimensions of the matrix: " << endl;
int a, b;
cin >> a >> b;
int arr[a][b];
cout << "Enter first array: " << endl;
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
cin >> arr[i][j];
}
}
cout << "Enter the dimensions of Secound matrix: " << endl;
int n, m;
cin >> n >> m;
int brr[n][m];

cout << "Enter second array: " << endl;


for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> brr[i][j];
}
}
if (b != n)
{
cout << "Multiplication not possible" << endl;
return 0;
}
int crr[a][m];
for (int i = 0; i < a; i++)
{
for (int j = 0; j < m; j++)
{
int ans = 0;
for (int k = 0; k < b; k++)
{
ans = ans + arr[i][k] * brr[k][j];
crr[i][j]=ans;
}
}
}
cout << "Resultant matrix after multiplication: " << endl;
for (int i = 0; i < a; i++) {
for (int j = 0; j < m; j++) {
cout << crr[i][j] << " ";
}
cout << endl;
}
return 0;
}
Experiment - 5

#include <iostream>

#include <vector>

using namespace std;

int find(vector<int>& arr, int target)

int count = 0;

for (int i = 0; i < arr.size(); i++)

if (target == arr[i])

count++;

return count;

int main()

int n;

cout << "Enter the size of the array: ";

cin >> n;

vector<int> arr(n);

cout << "Enter the elements of the array: ";

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

{
cin >> arr[i];

int target;

cout << "Enter the target: ";

cin >> target;

int ans = find(arr, target);

if (ans >= 1)

cout << "Yes" << endl;

} else

cout << "No" << endl;

return 0;

Experiment -5

#include <iostream>

using namespace std;

class Rectangle {

private:

double length;

double breadth;

public:

Rectangle(double l, double b)

{
length = l;

breadth = b;

Rectangle()

length = 0;

breadth = 0;

void setLength(double l)

length = l;

void setBreadth(double b)

breadth = b;

double getLength() const

return length;

double getBreadth() const

return breadth;

double area() const

{
return length * breadth;

double perimeter() const

return 2 * (length + breadth);

void display() const

cout << "Length: " << length << ", Breadth: " << breadth << endl;

};

int main()

int a,b;

cin >>a>>b;

Rectangle rect(a, b);

rect.display();

cout << "Area: " << rect.area() << endl;

cout << "Perimeter: " << rect.perimeter() << endl;

return 0;

You might also like