0% found this document useful (0 votes)
17 views11 pages

OS Lab 2

The document contains code solutions for an OS lab assignment by Taaha Khan, including tasks for reading files, finding missing numbers in sequences, and writing results to output files. It features C++ implementations for handling command-line arguments, file input/output, and vector manipulations. The code is structured into in-lab and post-lab tasks with specific functionalities outlined for each question.

Uploaded by

Taaaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views11 pages

OS Lab 2

The document contains code solutions for an OS lab assignment by Taaha Khan, including tasks for reading files, finding missing numbers in sequences, and writing results to output files. It features C++ implementations for handling command-line arguments, file input/output, and vector manipulations. The code is structured into in-lab and post-lab tasks with specific functionalities outlined for each question.

Uploaded by

Taaaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

OS Lab 2 (Remaining Tasks)

Name: Taaha Khan


NU ID: 23K-0583

In-Lab Tasks
Question#01:
Question#02:
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc, char *argv[])
{
if (argc != 2)
{
cout << "Usage: " << argv[0] << " <filename>" << endl;
return 1;
}

ifstream file(argv[1]);

if (!file)
{
cout << "Error: File " << argv[1] << " not found." << endl;
return 1;
}
string line;
while (getline(file, line))
{
cout << line << endl;
}

file.close();
return 0;
}
Post-Lab Tasks
Question#02:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

int findMissingNumber(const vector<int>& numbers)


{
int n = numbers.size();

int d = (numbers[n - 1] - numbers[0]) / n;

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


{
if (numbers[i + 1] - numbers[i] != d)
{
return numbers[i] + d;
}
}

return -1;
}

int main(int argc, char* argv[])


{
if (argc < 3)
{
cout << "Usage: " << argv[0] << " <num1> <num2> ... <numN>" << endl;
return 1;
}

vector<int> numbers;
for (int i = 1; i < argc; i++)
{
numbers.push_back(stoi(argv[i]));
}

sort(numbers.begin(), numbers.end());

int missingNumber = findMissingNumber(numbers);

ofstream outFile("missing_number.txt");
if (!outFile)
{
cout << "Error: Unable to open output file." << endl;
return 1;
}

if (missingNumber != -1)
{
outFile << "Missing number: " << missingNumber << endl;
cout << "Missing number has been written to missing_number.txt" << endl;
}
else
{
cout << "No missing number detected." << endl;
}

outFile.close();
return 0;
}
Question#03:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

int findMissingNumber(const vector<int>& numbers)


{
int n = numbers.size();

int d = (numbers[n - 1] - numbers[0]) / n;

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


{
if (numbers[i + 1] - numbers[i] != d)
{
return numbers[i] + d;
}
}

return -1;
}

int main(int argc, char* argv[])


{
if (argc != 2)
{
cout << "Usage: " << argv[0] << " <filename>" << endl;
return 1;
}
ifstream inFile(argv[1]);

if (!inFile)
{
cout << "Error: File " << argv[1] << " not found." << endl;
return 1;
}

vector<int> numbers;
int num;
while (inFile >> num) {
numbers.push_back(num);
}

sort(numbers.begin(), numbers.end());

int missingNumber = findMissingNumber(numbers);

inFile.close();

ofstream outFile(argv[1], ios::app);


if (!outFile)
{
cout << "Error: Unable to open output file." << endl;
return 1;
}

if (missingNumber != -1)
{
outFile << "\nMissing number: " << missingNumber << endl;
cout << "Missing number has been written to " << argv[1] << endl;
}
else
{
cout << "No missing number detected." << endl;
}

outFile.close();
return 0;
}
Question#04:

You might also like