Found 7347 Articles for C++

C++ program to demonstrate function of macros

Arnab Chakraborty
Updated on 12-Oct-2021 13:48:40

342 Views

Suppose we are given a integer array that contains several integer values. We have to find out the difference between the smallest value and the largest value in the array. To solve this problem, we have to use macros. The inputs are taken from stdin, and the result is printed back to stdout.So, if the input is like array = {120, 589, 324, 221, 234}, then the output will be The answer is : 469The difference between the largest value 589 and the smallest value 120 is 469.To solve this, we will follow these steps −mini := infinitymaxi := negative ... Read More

C++ program to demonstrate multi-level inheritance

Arnab Chakraborty
Updated on 12-Oct-2021 06:21:59

11K+ Views

Suppose we have three classes Vehicle, FourWheeler, and Car. The class Vehicle is the base class, the class FourWheeler is derived from it and the class Car is derived from the class FourWheeler. Class Vehicle has a method 'vehicle' that prints 'I am a vehicle', class FourWheeler has a method 'fourWheeler' that prints 'I have four wheels', and class Car has a method 'car' that prints 'I am a car'. So, as this is a multi-level inheritance; we can have access to all the other classes methods from the object of the class Car. We invoke all the methods from ... Read More

C++ program to print values in a specified format

Arnab Chakraborty
Updated on 11-Oct-2021 13:52:27

789 Views

Suppose we are given three double values. We have to format them and print them in the following format.We have to print the integer part of the first value in hexadecimal format with lowercase letters.We have to print the second value up to two decimal places, putting a sign before it to show whether it is positive or negative. The second value to be printed has to be rightjustified and 15 characters long, padded with underscores in the left unused positions.We have to print the third value up to nine decimal places in a scientific notation.So, if the input is ... Read More

C++ program to search specific values in an array

Arnab Chakraborty
Updated on 11-Oct-2021 14:36:48

3K+ Views

Suppose we are given an array 'arr' that contains n number of sorted integer values. We are also given an array 'query' of size q, and we have to tell either the values in ‘query’ are present in the given array 'arr' or not. If a value in query is present in arr, we print "Present" along with the position where the value is situated in. Otherwise, we print "Not present" and print the position in arr, where the minimum value greater than the value in query is located. We have to remember that the arrays are 1-indexed.So, if the ... Read More

C++ program to print values of different data types provided as input

Arnab Chakraborty
Updated on 11-Oct-2021 12:07:11

2K+ Views

Suppose we are given an integer value, a long value, a character value, a float value, and a double value as inputs. We have to print the values that are given to us as input maintaining their precision.So, if the input is like integer value = 15, long value = 59523256297252, character value = 'y', float value = 367.124, double value = 6464292.312621, then the output will be15 59523256297252 y 367.124 6464292.31262To solve this, we will follow these steps −Print the values given as input in separate lines.ExampleLet us see the following implementation to get better understanding −#include #include ... Read More

C++ program to overload addition operator to add two matrices

Arnab Chakraborty
Updated on 07-Oct-2021 08:48:02

3K+ Views

Suppose we have two matrices mat1 and mat2. We shall have to add these two matrices and form the third matrix. We shall have to do this by overloading the addition operator.So, if the input is like589679834763then the output will be131113131312To solve this, we will follow these steps −Overload the addition operator, this will take another matrix mat as second argumentdefine one blank 2d array vvDefine one 2D array vv and load current matrix elements into itfor initialize i := 0, when i < size of vv, update (increase i by 1), do:for initialize j := 0, when j < ... Read More

C++ program to overload extraction operator

Arnab Chakraborty
Updated on 07-Oct-2021 08:45:04

334 Views

Suppose we have a Person class with two attributes first_name and the last_name. It also has two methods called get_first_name() and get_last_name() to retrieve or set first name and last name respectively. We shall have to overload the extraction operator (

C++ program to find maximum of each k sized contiguous subarray

Arnab Chakraborty
Updated on 07-Oct-2021 08:42:37

313 Views

Suppose we have an array with n elements and a value k. We shall have to find maximum value for each of the contiguous subarray of size k.So, if the input is like arr = [3, 4, 6, 2, 8], k = 3, then the output will be The contiguous subarrays of size 3 are [3, 4, 6], [4, 6, 2], [6, 2, 8], so the maximum elements are 6, 6 and 8.To solve this, we will follow these steps −Define one deque Qi of size kfor initialize i := 0, when i < k, update (increase i by 1), ... Read More

C++ program to define exception for small username, and validate username

Arnab Chakraborty
Updated on 07-Oct-2021 08:40:06

674 Views

Suppose we have a string of usernames and we shall have to check whether username is valid or not based on few conditions. So we shall have to define an exception that is thrown when the length of username is less than 5 characters long. We shall have to return "Valid" for valid username, "Invalid" for invalid username and throw exception for smaller usernames. The valid username conditions are −Username must be five-character longThere should not two consecutive 'w' in the usernameSo, if the input is like unames = ["amit", "to", "paul_tim", "greg_harry", "towwer"], then the output will be [Too ... Read More

C++ program to overload addition operator to add two complex numbers

Arnab Chakraborty
Updated on 31-Oct-2023 02:59:44

28K+ Views

Suppose we have a complex number class with real and imaginary part. We shall have to overload the addition (+) operator to add two complex number. We also have to define a function to return complex number in proper representation.So, if the input is like c1 = 8 - 5i, c2 = 2 + 3i, then the output will be 10 - 2i.To solve this, we will follow these steps −Overload the + operator and take another complex number c2 as argumentdefine a complex number called ret whose real and imag are 0real of ret := own real + real ... Read More

Advertisements