Lab 15
Lab 15
Lab 15
15
23f-0843
QUESTION 1
#include <iostream>
using namespace std;
int main() {
float first[ROWS][COLS];
float second[ROWS][COLS];
float third[ROWS][COLS];
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];
}
}
QUESTION 2
#include <iostream>
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;
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];
}
}
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 main() {
int a = 23;
double b = 48.78;
char ch = 'M';
return 0;
}
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>
int main() {
double num1;
double num2;
double sum;
ofstream file("add.txt");
if (!file) {
cerr << "Error!!" << endl;
return 1;
}
file.close();
ifstream readFile("add.txt");
if (!readFile) {
cerr << "Error!" << endl;
return 1;
}
readFile.close();
system("pause");
return 0;
}
QUESTION 7
#include <iostream>
#include <fstream>
#include <string>
int main() {
ofstream data("TextFile.txt");
if (!data.is_open()) {
cout << "Error" << endl;
return 1;
}
int n1;
cout << "Enter your number: " << endl;
cin >> n1;
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;
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];
}
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;
}