OOP Solutions
OOP Solutions
Complete the following code wherever mentioned. (Assume that libraries are already included).
/ Complete the Function findMax (4 lines approx.)
return max;
return min;
//main function
int main() {
// Declare 2 pointers referring to Min. and Max. (initialized with start element of array) (2 lines
approx.) (0.5 *2 = 1 Mark)
// 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)
}//end main
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>
struct Flight {
string flightNumber;
string destination;
int seatsAvailable;
};
f->seatsAvailable -= seatsToBook;
cout << "Successfully booked " << seatsToBook << " seats.\n";
} else {
return *f;
}
// Function to print the details of a flight 2.5
printFlightDetails(flight1);
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
Sample output:
Solution:
#include <iostream>
if (N == 1) { (3 Marks)
} else {
}
}
int main() {
int N;
cin >> N;
if (N <= 0) {
} else {
cout << "Sum of the series for N = " << N << " is: " << result << std::endl;
return 0;
Write outputs of the following codes. Write output in front of every “cout<<”
statement.
int main() {
int var = 5;
int* pointVar;
pointVar = &var;
<< endl;
var = 7;
<< endl;
*pointVar = 16;
Address of n : 100
Address of n2 : 104
Address of n3 : 108
#include <bits/stdc++.h>
int square1(int n)
n *= n;
return n;
void square2(int* n)
*n *= *n;
void square3(int& n)
n *= n;
void checks()
int n1 = 8;
C- (3 Marks) (0.5 marks for each cout<< statmement)
int main() {
cout << *iPtr + *(iPtr + 1) << endl; // Output: Sum of the first and
second elements (33)
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>
int count(const char *str, const char c); // No need to pass the array size
int main() {
int count = 0;
if (*str == c) ++count;
++str;
return count;
OUTPUT