Lab 15

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

PROGRAMMING FUNDAMENTALS LAB

15
23f-0843

QUESTION 1
#include <iostream>
using namespace std;

const int ROWS = 5;


const int COLS = 5;

void subtractMatrices(float first[][COLS], float second[][COLS], float third[][COLS]) {


for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
third[i][j] = first[i][j] - second[i][j];
}
}
}

void print(float arr2D[][COLS]) {


for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
cout << arr2D[i][j] << " ";
}
cout << endl;
}
}

int main() {
float first[ROWS][COLS];
float second[ROWS][COLS];
float third[ROWS][COLS];

cout << "Enter values for the first matrix:" <<endl;


for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
cout << "Enter value for element (" << i << ", " << j << "): ";
cin >> first[i][j];
}
}

cout << "Enter values for the second matrix:" << endl;
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
cout << "Enter value for element (" << i << ", " << j << "): ";
cin >> second[i][j];
}
}

subtractMatrices(first, second, third);

cout << "Result of subtraction:" << endl;


print(third);
system("pause");
return 0; }

QUESTION 2
#include <iostream>

using namespace std;

int Minimum(int a, int b) {


return (a < b) ? a : b;
}

float Minimum(float a, float b) {


return (a < b) ? a : b;
}

char Minimum(char a, char b, char c) {


if (a < b && a < c)
return a;
else if (b < c)
return b;
else
return c;
}

int main() {
int choice;
cout << "Choose a function to find minimum:" << endl;
cout << "1. Minimum of two integers" << endl;
cout << "2. Minimum of two floats" << endl;
cout << "3. Minimum of three characters" << endl;
cout << "Enter your choice (1/2/3): ";
cin >> choice;

switch (choice) {
case 1: {
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
cout << "Minimum value: " << Minimum(num1, num2) << endl;
break;
}
case 2: {
float num1, num2;
cout << "Enter two floats: ";
cin >> num1 >> num2;
cout << "Minimum value: " << Minimum(num1, num2) << endl;
break;
}
case 3: {
char char1, char2, char3;
cout << "Enter three characters: ";
cin >> char1 >> char2 >> char3;
cout << "Minimum value: " << Minimum(char1, char2, char3) << endl;
break;
}
default:
cout << "Invalid choice!" << endl;
}

system("pause");
return 0;
}
QUESTION 3
#include <iostream>
#include <algorithm>
using namespace std;

const int MAX_SIZE = 100;

int reverse2D(int arr1[MAX_SIZE][MAX_SIZE], int arr2[MAX_SIZE][MAX_SIZE], int size) {

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


reverse(arr1[i], arr1[i] + size);
copy(arr1[i], arr1[i] + size, arr2[i]);
}

int max1 = INT_MIN, max2 = INT_MIN;


for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
if (arr2[i][j] > max1) {
max2 = max1;
max1 = arr2[i][j];
}
else if (arr2[i][j] > max2 && arr2[i][j] < max1) {
max2 = arr2[i][j];
}
}
}

cout << "Reversed second 2D array:" << endl;


for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
cout << arr2[i][j] << " ";
}
cout << endl;
}

return max2;
}

int main() {
int size;
cout << "Enter the size of the 2D arrays: ";
cin >> size;

int array1[MAX_SIZE][MAX_SIZE];
int array2[MAX_SIZE][MAX_SIZE];

cout << "Enter the elements of the first 2D array:" << endl;
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
cin >> array1[i][j];
}
}

int secondMax = reverse2D(array1, array2, size);

cout << "Second maximum value in the reversed 2D array: " << secondMax << endl;

system("pause");
return 0;
}

QUESTION 4
#include <iostream>
#include <iomanip>
using namespace std;

int volume(int l = 1, int w = 1, int h = 1);


void funcOne(int& x, double y = 12.34, char z = 'B');

int main() {
int a = 23;
double b = 48.78;
char ch = 'M';

cout << fixed << showpoint << setprecision(2);


cout << "Line 1: a = " << a << ", b = " << b << ", ch = " << ch << endl;
cout << "Line 2: Volume = " << volume() << endl;
cout << "Line 3: Volume = " << volume(5.4, 4) << endl;
cout << "Line 4: Volume = " << volume('A') << endl;
cout << "Line 5: Volume = " << volume(6, 4, 5) << endl;
funcOne(a);
funcOne(a, 42.68, '1');
funcOne(a, 34.65, 'Q');
cout << "Line 9: a = " << a << ", b = " << b << ", ch = " << ch << endl;

return 0;
}

int volume(int l, int w, int h) {


return l * w * h;
}

void funcOne(int& x, double y, char z) {


x = 2 * x;
cout << "Line 12: x = " << x << ", y = " << y << ", z = " << z << endl;
}

QUESTION 5
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream data("file.txt");

if (!data.is_open())
{
cout << "Error opening file." << endl;
return 1;
}
string line;
while (getline(data, line))
{
cout << line << endl;
}
data.close();
ofstream remove("file.txt");
remove << "";
remove.close();
return 0;
}

Question 6
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {

double num1;
double num2;
double sum;

ofstream file("add.txt");

if (!file) {
cerr << "Error!!" << endl;
return 1;
}

cout << "Enter your first number: ";


cin >> num1;
cout << "Enter your second number: ";
cin >> num2;

sum = num1 + num2;

file << "First Number: " << num1 << endl;


file << "Second Number: " << num2 << endl;
file << "Sum: " << sum << endl;

file.close();
ifstream readFile("add.txt");

if (!readFile) {
cerr << "Error!" << endl;
return 1;
}

cout << "Contents of the file are= " << endl;


string line;
while (getline(readFile, line)) {
cout << line << endl;
}

readFile.close();

system("pause");
return 0;
}

QUESTION 7
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {

ofstream data("TextFile.txt");

if (!data.is_open()) {
cout << "Error" << endl;
return 1;
}
int n1;
cout << "Enter your number: " << endl;
cin >> n1;

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


data << "3 x " << i << " = " << i * n1 << endl;
}

data.close();

ifstream read("TextFile.txt");

if (!read.is_open()) {
cout << "Error" << endl;
return 1;
}
t
string str;
while (getline(read, str)) {
cout << str << endl;
}

read.close();

return 0;
}

QUESTION 8
#include <iostream>
using namespace std;

void maxNumbers(int first[], int second[], int third[], int size)


{
for (int i = 0; i < size; ++i)
{
third[i] = first[i];
}

int j = size;
for (int i = 0; i < size; ++i)
{
if (second[i] > third[j - 1])
{
int k = j - 1;
while (k >= 0 && second[i] > third[k])
{
third[k + 1] = third[k];
--k;
}
third[k + 1] = second[i];
}
}
}

int main()
{
const int size = 10;
int first[size], second[size], third[size * 2];

cout << "Enter " << size << " integers for the first array:" << endl;
for (int i = 0; i < size; ++i)
{
cin >> first[i];
}

cout << "Enter " << size << " integers for the second array:" << endl;
for (int i = 0; i < size; ++i)
{
cin >> second[i];
}

maxNumbers(first, second, third, size);

cout << "The 10 maximum numbers stored in the third array are:" << endl;
for (int i = 0; i < 10; ++i)
{
cout << third[i] << " ";
}
cout << endl;
system("pause");
return 0;
}

You might also like