0% found this document useful (0 votes)
35 views14 pages

OOP Solutions

The document contains 4 coding questions with the following details: 1. Functions to find maximum and minimum elements in an array. 2. Flight booking system simulation with struct, booking and printing functions. 3. Recursive function to calculate sum of a series. 4. Code snippets with output requested for each print statement.

Uploaded by

anwarshahphd2021
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)
35 views14 pages

OOP Solutions

The document contains 4 coding questions with the following details: 1. Functions to find maximum and minimum elements in an array. 2. Flight booking system simulation with struct, booking and printing functions. 3. Recursive function to calculate sum of a series. 4. Code snippets with output requested for each print statement.

Uploaded by

anwarshahphd2021
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/ 14

Question 1 (15 Marks)

Complete the following code wherever mentioned. (Assume that libraries are already included).
/ Complete the Function findMax (4 lines approx.)

int findMax(int* arr, int size) { (5 MARKS)

return max;

//Complete the Function findMin (4 lines approx.)

int findMin(int* arr, int size) { (5 MARKS)

return min;

//main function

int main() {

const int size = 5;

int arr[size] = {34, 12, 7, 45, 23};


cout << "Original Array: ";

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

cout << arr[i] << " ";

// Declare 2 pointers referring to Min. and Max. (initialized with start element of array) (2 lines
approx.) (0.5 *2 = 1 Mark)

int* maxPtr = &arr[0]; // Pointer to the maximum element


int* minPtr = &arr[0]; // Pointer to the minimum element

// Iterate over the array to assign Max. & Min reference to pointers using findMax and findMin
functions (5 lines approx.) (5 MARKS)

// Deference the Max. and Min. values on console (2 lines approx.) (0.5 *2 = 1 Mark)

std::cout << "Maximum Element: " << *maxPtr << std::endl;

std::cout << "Minimum Element: " << *minPtr << std::endl;

}//end main

Question 2 (10 Marks)

Write a C++ program to simulate a basic flight booking system where:

1. A Flight structure holds details about the flight number, destination, and the number of seats
available.
2. Implement a function bookFlight() that takes a pointer to the Flight structure and an integer
representing the number of seats to book. If enough seats are available, the function should
update the number of available seats and return the updated Flight structure; otherwise, it
should print an error message and return the unmodified Flight structure.
3. Implement a function printFlightDetails() that takes a Flight structure and prints the details
of the flight.

In the main() function, create a Flight instance, book some seats using the bookFlight() function, and
print the flight details before and after booking using the printFlightDetails() function.

#include <iostream>

using namespace std;

// Define the Flight structure 2.5

struct Flight {

string flightNumber;

string destination;

int seatsAvailable;

};

// Function to book seats in a flight 2.5

Flight bookFlight(Flight *f, int seatsToBook) {

if (f->seatsAvailable >= seatsToBook) {

f->seatsAvailable -= seatsToBook;

cout << "Successfully booked " << seatsToBook << " seats.\n";

} else {

cout << "Error: Not enough seats available!\n";

return *f;

}
// Function to print the details of a flight 2.5

void printFlightDetails(const Flight& f) {

cout << "Flight Number: " << f.flightNumber << "\n";

cout << "Destination: " << f.destination << "\n";

cout << "Seats Available: " << f.seatsAvailable << "\n";

int main() { 2.5

// Create an instance of Flight

Flight flight1 = {"FN123", "Paris", 100};

// Print the initial details of the flight

cout << "Initial Flight Details:\n";

printFlightDetails(flight1);

// Try to book some seats

flight1 = bookFlight(&flight1, 20);

// Print the updated details of the flight

cout << "Updated Flight Details:\n";

printFlightDetails(flight1);

return 0;
Question 3 (10 Marks)

Write a recursive function in c++ that takes an input integer N and calculates the sum of
following series

2
1/1 +1/22+1/32 + 1/42 + ………. + 1/N2

// the function will calculate sum of series described above until N.

The function signature is:

float CalculateSum(int N);

Sample output:

cout<<CalculateSum(4) ;// this should print 1.4225 (1+0.25+0.11+0.0625)

cout<<CalculateSum(5) ;// this should print 1.4625 (1+0.25+0.11+0.0625+0.04)

Solution:

#include <iostream>

using namespace std;

float CalculateSum (int N) {

if (N == 1) { (3 Marks)

return 1.0; // Base case: 1/1^2 (2 Mark)

} else {

float term = 1.0 / (N * N); (3 Marks)

return term + CalculateSum(N - 1); (3 Marks)

}
}

// the main part was optional

int main() {

int N;

cout << "Enter the value of N: ";

cin >> N;

if (N <= 0) {

cout << "N should be a positive integer." << std::endl;

} else {

float result = CalculateSum (N);

cout << "Sum of the series for N = " << N << " is: " << result << std::endl;

return 0;

Question 4 (3+5+3+4 = 15 Marks)

Write outputs of the following codes. Write output in front of every “cout<<”

statement.

A- (3 Marks) (0.5 marks for each cout<< statmement)


#include <iostream>

using namespace std;

int main() {

int var = 5;

int* pointVar;

pointVar = &var;

cout << "var = " << var << endl;

cout << "*pointVar = " << *pointVar << endl

<< endl;

var = 7;

cout << "var = " << var << endl;

cout << "*pointVar = " << *pointVar << endl

<< endl;

*pointVar = 16;

cout << "var = " << var << endl;

cout << "*pointVar = " << *pointVar << endl;


B- Assume following things in driver function only (checks()) (5 marks)

(0.5 marks for each cout<< statmement)

Address of n : 100

Address of n2 : 104

Address of n3 : 108
#include <bits/stdc++.h>

using namespace std;

int square1(int n)

cout << "address of n1 in square1(): " << &n << "\n";

n *= n;

return n;

void square2(int* n)

cout << "n2 in square2(): " << n << "\n";

*n *= *n;

void square3(int& n)

cout << "n3 in square3(): " << &n << "\n";

n *= n;

void checks()

int n1 = 8;
C- (3 Marks) (0.5 marks for each cout<< statmement)

Address of *iptr = 100

int main() {

int numbers[] = {11, 22, 33};

int* iPtr = numbers;

cout << iPtr << endl; // Output: Address of the first


element (e.g., 0x7fff5a29d6c0)

cout << iPtr + 1 << endl; // Output: Address of the second


element (e.g., 0x7fff5a29d6c4)

cout << *iPtr << endl; // Output: Value of the first


element ( 11)

cout << *iPtr + *(iPtr + 1) << endl; // Output: Sum of the first and
second elements (33)

cout << *(iPtr + 1) << endl; // Output: Value of the second


element (22)

cout << *iPtr + 1 << endl; // Output: Value of the first


element plus 1 (12)

return 0;

D- (4 Marks) (1 mark for each cout << statement = 3 marks + 1 mark for correct structured
output with new line (\n))
#include <iostream>

#include <cstring>

using namespace std;

int count(const char *str, const char c); // No need to pass the array size

int main() {

char msg1[] = "Hello, world";

char *msg2 = "Hello, world";

cout << count(msg1, 'l') << endl;

cout << count(msg2, 'l') << endl;

cout << count("Hello, world", 'l') << endl;

// Count the occurrence of c in str

// No need to pass the size of char[] as C-string is terminated with '\0'

int count(const char *str, const char c) {

int count = 0;

while (*str) { // same as (*str != '\0')

if (*str == c) ++count;

++str;

return count;

OUTPUT

You might also like