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

Advanced C/C++ Programming Exercises

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

Advanced C/C++ Programming Exercises

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

Advanced Programming Exercises in C/C++

Practice Set

Introduction
This document contains 10 advanced programming exercises in C/C++. Each exercise includes:

• A formal definition of the problem,

• Programming requirements,

• Test cases,

• Function prototypes or code skeletons.

1
1 Strong Numbers in Range [A, B]
Definition
A Strong Number is a positive integer for which the sum of the factorials of its digits is equal
to the number itself.
145 = 1! + 4! + 5! = 145

Requirements
1. Write a function long long factorial(int n).

2. Write a function bool isStrong(int n).

3. Input integers A and B (A ≤ B) and print all Strong Numbers in that range.

Code Skeleton
long long factorial(int n);

bool isStrong(int n);

int main() {
int A, B;
cin >> A >> B;
// print all strong numbers
}

Test Cases
Input Output
A = 1, B = 500 1, 2, 145
A = 1000, B = 2000 No Strong Numbers found.

2
2 Compute ex Using Taylor Series
Definition
The Taylor expansion for ex is:

x
X xn
e =
n!
n=0

Stop when the current term is smaller than ε.

Requirements
1. Implement ex using a loop.

2. Input x and ε.

3. Compare with exp(x).

Code Skeleton
double computeExp(double x, double eps);

int main() {
double x, eps;
cin >> x >> eps;
}

Test Cases
Input Output
x=1, eps=1e-6 approx 2.718281
x=0.5, eps=1e-6 approx 1.648721

3
3 Sieve of Eratosthenes
Requirements
1. Input integer N (N ≤ 105 ).

2. Implement sieve using boolean array.

3. Print all primes in [2, N ].

Code Skeleton
void sieve(int N, bool isPrime[]);

int main() {
int N;
cin >> N;
}

Test Cases
Input Output
N=30 2 3 5 7 11 13 17 19 23 29
N=10 2357

4
4 Find Majority Element in an Array
Definition
A majority element occurs more than ⌊N/2⌋ times.

Requirements
1. Implement Boyer-Moore algorithm.

2. Return -1 if no majority element exists.

Code Skeleton
int findMajority(int arr[], int n);

Test Cases
Array Majority
22111222 2
1552553 5
12345 -1

5
5 Array Rotation and Longest Non-Decreasing Subarray
Requirements
1. Write rotateLeft(arr, n, k).

2. Write longestNonDecreasing(arr, n).

3. Print rotated array and longest length.

Code Skeleton
void rotateLeft(int arr[], int n, int k);

int longestNonDecreasing(int arr[], int n);

Test Cases
Input Rotated LNDS Length
1 5 2 4 4 6 3 7, k=3 44637152 3
10 20 30 1 2 3, k=2 30 1 2 3 10 20 5

6
6 Fraction Operations Using Struct
Requirements
1. Define a struct Fraction.

2. Implement GCD, reduce, add, multiply, inverse.

Code Skeleton
struct Fraction { int num, den; };

int gcd(int a, int b);

Fraction reduce(Fraction f);

Fraction add(Fraction a, Fraction b);

Fraction multiply(Fraction a, Fraction b);

Fraction inverse(Fraction a);

Test Cases
• (1/2) + (3/4) = 5/4

• (2/3) ∗ (6/8) = 1/2

• inverse of 4/6 = 3/2

7
7 Student Management and Ranking
Requirements
1. Define struct Student.

2. Compute average score.

3. Rank: Excellent, Good, Fair, Weak.

4. Sort by descending average.

Code Skeleton
struct Student {
string id, name;
float math, lit, eng;
};

float average(Student s);

string rankStudent(float avg);

void sortStudents(Student a[], int n);

Expected Output Example


Sorted by descending average:

• 8.67 – Excellent

• 7.00 – Good

• 5.00 – Fair

8
8 Saddle Points in a Matrix
Definition
A Saddle Point is:

• the maximum in its row,

• and the minimum in its column.

Code Skeleton
void findSaddlePoints(int a[][100], int m, int n);

Test Cases
• Example matrix with saddle point at (2, 0) value 7.

• Example matrix with no saddle point.

9
9 Spiral Traversal of a Matrix
Requirements
1. Input square matrix.

2. Print in clockwise spiral order.

Code Skeleton
void printSpiral(int a[][100], int n);

Test Cases
• 4 × 4 matrix → 1 2 3 4 8 12 16 ...

• 3 × 3 matrix → 1 2 3 6 9 8 7 4 5

10
10 Validate a Sudoku Grid
Requirements
1. Check all rows, columns, and 3x3 subgrids.

2. Ensure no duplicates (1–9).

Code Skeleton
bool validSudoku(int a[9][9]);

Test Cases
• Valid Sudoku → print VALID.

• Duplicate in row/col/subgrid → print INVALID.

11

You might also like