[go: up one dir, main page]

0% found this document useful (0 votes)
5 views66 pages

cs-308 Assignment - With - ToC

The document is a compilation of programming exercises and solutions for a CS-308 assignment, covering various topics such as strings, pointers, structures, and mathematical calculations. Each exercise includes code snippets in C++ along with expected outputs, demonstrating concepts like gross salary calculation, palindrome checks, and distance conversions. It serves as a comprehensive guide for students to practice and understand fundamental programming concepts.
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)
5 views66 pages

cs-308 Assignment - With - ToC

The document is a compilation of programming exercises and solutions for a CS-308 assignment, covering various topics such as strings, pointers, structures, and mathematical calculations. Each exercise includes code snippets in C++ along with expected outputs, demonstrating concepts like gross salary calculation, palindrome checks, and distance conversions. It serves as a comprehensive guide for students to practice and understand fundamental programming concepts.
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/ 66

Cs-308 assignment

37. String Exercises (Functions)

36. Pointer Exercises (Functions)

35. Structure Exercises (Functions)

34. Book and Order Structures

33. Income and Tax of 5 Persons

32. Employee with More Salary

31. Distance Covered by Players (Structure)

30. Prime Check Using Function Pointer

29. Swap Two Values Using Pointers

28. Average Marks Using Dynamic Array

27. Structure with Account ID & Amount (Dynamic Array)

26. Find Maximum Using Pointers

25. Swap Integers Using Pointers

24. Sort Array of Integers (Pointers)

23. Employee Tax Calculation

22. Student Grades

21. Quadratic Equation Roots

20. Prime Numbers in Array

19. Array of Structures (Income and Tax)

18. Leap Year Check

17. Series Sum

16. Character Case Conversion

15. String Triangle Pattern

14. Prime Number Check

13. Player Class with Static Member

1
Cs-308 assignment

12. Distance Converter

11. Temperature Message

10. Bank Account Class

9. Palindrome Check

8. Total and Percentage

7. ASCII Code

6. Compound Interest

5. Add Two Times

4. Gross Salary Calculation

3. Sum of Digits

2. Next Two Letters

1. Log Base 2 of a Number

Table of Contents

Table of Contents

Log base 2 of a number

Output:

Log base 2 of 16 is: 4.00

#include <iostream>

#include <cmath>

using namespace std;

int main() {

int number = 16;

2
Cs-308 assignment

double result = log2(number);

cout << "Log base 2 of " << number << " is: " << result << endl;

return 0;

Next two letters

Output:

Input letter: A, Next letters: B, C

#include <iostream>

using namespace std;

int main() {

char letter = 'A';

cout << "Input letter: " << letter << ", Next letters: "

<< char(letter + 1) << ", " << char(letter + 2) << endl;

return 0;

Sum of digits of a five-digit number

Output:

Sum of digits of 12345 is: 15

3
Cs-308 assignment

#include <iostream>

using namespace std;

int main() {

int number = 12345, sum = 0, digit;

int temp = number;

while (temp > 0) {

digit = temp % 10;

sum += digit;

temp /= 10;

cout << "Sum of digits of " << number << " is: " << sum << endl;

return 0;

Gross Salary Calculation

Output:

Basic Salary: 20000, Gross Salary: 32000

#include <iostream>

using namespace std;

int main() {

double basic = 20000;

double gross = basic + 0.35 * basic + 0.25 * basic;

4
Cs-308 assignment

cout << "Basic Salary: " << basic << ", Gross Salary: " << gross << endl;

return 0;

Add two times in hh:mm:ss

Output:

Total time is 4:6:20

#include <iostream>

using namespace std;

int main() {

int h1 = 2, m1 = 45, s1 = 50;

int h2 = 1, m2 = 20, s2 = 30;

int totalSec = s1 + s2;

int totalMin = m1 + m2 + totalSec / 60;

int totalHour = h1 + h2 + totalMin / 60;

totalSec %= 60;

totalMin %= 60;

cout << "Total time is " << totalHour << ":" << totalMin << ":" << totalSec << endl;

return 0;

5
Cs-308 assignment

Compound Interest

Output:

Compound Interest = 102.50

#include <iostream>

#include <cmath>

using namespace std;

int main() {

double P = 1000, R = 5, T = 2;

double CI = P * pow((1 + R / 100), T) - P;

cout << "Compound Interest = " << CI << endl;

return 0;

ASCII Code

Output:

ASCII code of 'A' is: 65

6
Cs-308 assignment

#include <iostream>

using namespace std;

int main() {

char ch = 'A';

cout << "ASCII code of '" << ch << "' is: " << int(ch) << endl;

return 0;

Total and Percentage

Output:

Total Marks: 400, Percentage: 80.00%

#include <iostream>

using namespace std;

int main() {

int marks[5] = {80, 75, 90, 85, 70}, total = 0;

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

total += marks[i];

float percentage = (total / 500.0) * 100;

7
Cs-308 assignment

cout << "Total Marks: " << total << ", Percentage: " << percentage << "%" << endl;

return 0;

Palindrome Check

#include <iostream>

#include <string>

#include <algorithm>

using namespace std;

bool isPalindrome(string str) {

string reversed = str;

reverse(reversed.begin(), reversed.end());

return str == reversed;

int main() {

string input;

cout << "Enter a string: ";

getline(cin, input);

if (isPalindrome(input)) {

cout << "The string is a palindrome." << endl;

} else {

cout << "The string is not a palindrome." << endl;

8
Cs-308 assignment

return 0;

Output:

Enter a string: madam

The string is a palindrome.

Enter a string: hello

The string is not a palindrome.

Bank Account Class

#include <iostream>

#include <string>

using namespace std;

class BankAccount {

private:

string depositorName;

string accountNumber;

string accountType;

double balance;

public:

BankAccount(string name, string accNum, string type, double initialBalance) {

9
Cs-308 assignment

depositorName = name;

accountNumber = accNum;

accountType = type;

balance = initialBalance;

void deposit(double amount) {

balance += amount;

cout << "Deposited: " << amount << endl;

void withdraw(double amount) {

if (amount > balance) {

cout << "Insufficient balance!" << endl;

} else {

balance -= amount;

cout << "Withdrawn: " << amount << endl;

void display() {

cout << "Account Holder: " << depositorName << endl;

cout << "Balance: " << balance << endl;

};

10
Cs-308 assignment

int main() {

BankAccount account("John Doe", "123456789", "Savings", 1000.0);

account.display();

account.deposit(500);

account.withdraw(200);

account.display();

return 0;

Output:

Account Holder: John Doe

Balance: 1000

Deposited: 500

Withdrawn: 200

Account Holder: John Doe

Balance: 1300

Temperature Message

#include <iostream>

using namespace std;

int main() {

float temp;

cout << "Enter temperature: ";

11
Cs-308 assignment

cin >> temp;

if (temp > 35) {

cout << "Hot day" << endl;

} else if (temp >= 25 && temp <= 35) {

cout << "Pleasant day" << endl;

} else {

cout << "Cool day" << endl;

return 0;

Output:

Enter temperature: 28

Pleasant day

Enter temperature: 12

Cool day

Distance Converter

#include <iostream>

using namespace std;

int main() {

float value;

12
Cs-308 assignment

int choice;

cout << "Conversion Options:" << endl;

cout << "1. Inches to Centimeters" << endl;

cout << "2. Gallons to Liters" << endl;

cout << "3. Miles to Kilometers" << endl;

cout << "4. Pounds to Kilograms" << endl;

cout << "Enter your choice (1-4): ";

cin >> choice;

if (choice < 1 || choice > 4) {

cout << "Invalid choice!" << endl;

return 1;

cout << "Enter value to convert: ";

cin >> value;

switch(choice) {

case 1:

cout << value << " inches = " << value * 2.54 << " cm" << endl;

break;

case 2:

cout << value << " gallons = " << value * 3.785 << " liters" << endl;

break;

13
Cs-308 assignment

case 3:

cout << value << " miles = " << value * 1.609 << " km" << endl;

break;

case 4:

cout << value << " pounds = " << value * 0.4536 << " kg" << endl;

break;

return 0;

Output:

Conversion Options:

1. Inches to Centimeters

2. Gallons to Liters

3. Miles to Kilometers

4. Pounds to Kilograms

Enter your choice (1-4): 3

Enter value to convert: 10

10 miles = 16.09 km

Player Class with Static Member

#include <iostream>

#include <string>

using namespace std;

14
Cs-308 assignment

class Run {

private:

string runnerName;

float distance;

static float maxDistance;

static string bestRunner;

public:

void getData() {

cout << "Enter runner name: ";

cin >> runnerName;

cout << "Enter distance covered: ";

cin >> distance;

if (distance > maxDistance) {

maxDistance = distance;

bestRunner = runnerName;

void showData() {

cout << "Runner: " << runnerName << endl;

cout << "Distance: " << distance << " km" << endl;

15
Cs-308 assignment

static void showBestRunner() {

cout << "Best Runner: " << bestRunner << " (" << maxDistance << " km)" << endl;

};

float Run::maxDistance = 0;

string Run::bestRunner = "";

int main() {

Run runners[3];

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

cout << "\nEnter details for runner " << i+1 << endl;

runners[i].getData();

cout << "\nAll Runners:" << endl;

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

runners[i].showData();

cout << "\n";

Run::showBestRunner();

return 0;

16
Cs-308 assignment

Output:

Enter details for runner 1

Enter runner name: Alice

Enter distance covered: 12.5

Enter details for runner 2

Enter runner name: Bob

Enter distance covered: 15.2

Enter details for runner 3

Enter runner name: Carol

Enter distance covered: 14.8

All Runners:

Runner: Alice

Distance: 12.5 km

Runner: Bob

Distance: 15.2 km

Runner: Carol

Distance: 14.8 km

Best Runner: Bob (15.2 km)

Prime Number Check

17
Cs-308 assignment

#include <iostream>

using namespace std;

bool isPrime(int *num) {

if (*num <= 1) return false;

for (int i = 2; i * i <= *num; i++) {

if (*num % i == 0) return false;

return true;

int main() {

int number;

cout << "Enter an integer: ";

cin >> number;

if (isPrime(&number)) {

cout << number << " is a prime number." << endl;

} else {

cout << number << " is not a prime number." << endl;

return 0;

Output:

18
Cs-308 assignment

Enter an integer: 17

17 is a prime number.

Enter an integer: 22

22 is not a prime number.

String Triangle Pattern

#include <iostream>

#include <string>

using namespace std;

int main() {

string input;

cout << "Enter a string: ";

getline(cin, input);

for (int i = 0; i < input.length(); i++) {

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

cout << input[j] << " ";

cout << endl;

return 0;

19
Cs-308 assignment

Output:

Enter a string: World

Wo

Wor

Worl

World

Character Case Conversion

#include <iostream>

#include <cctype>

using namespace std;

void convertChar(string &str, char ch) {

for (char &c : str) {

if (tolower(c) == tolower(ch)) {

if (islower(c)) c = toupper(c);

else c = tolower(c);

int main() {

string input;

char character;

20
Cs-308 assignment

cout << "Enter a string: ";

getline(cin, input);

cout << "Enter a character: ";

cin >> character;

convertChar(input, character);

cout << "Modified string: " << input << endl;

return 0;

Output:

Enter a string: Hello World

Enter a character: l

Modified string: HeLLo WorLd

Series Sum

#include <iostream>

using namespace std;

int main() {

double sum = 0.0;

for (int i = 1; i <= 99; i++) {

sum += static_cast<double>(i) / (i + 1);

21
Cs-308 assignment

cout << "Sum of series: " << sum << endl;

return 0;

Output:

Sum of series: 94.4126

Leap Year Check

#include <iostream>

using namespace std;

bool isLeapYear(int year) {

if (year % 4 != 0) return false;

else if (year % 100 != 0) return true;

else return (year % 400 == 0);

int main() {

int year;

cout << "Enter a year: ";

cin >> year;

if (isLeapYear(year)) {

cout << year << " is a leap year." << endl;

} else {

22
Cs-308 assignment

cout << year << " is not a leap year." << endl;

return 0;

Output:

Enter a year: 2020

2020 is a leap year.

Enter a year: 1900

1900 is not a leap year.

Array of Structures

#include <iostream>

using namespace std;

struct Person {

float income;

float taxRate;

float tax;

};

int main() {

Person people[5];

23
Cs-308 assignment

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

cout << "Enter income for person " << i+1 << ": ";

cin >> people[i].income;

cout << "Enter tax rate (as decimal): ";

cin >> people[i].taxRate;

people[i].tax = people[i].income * people[i].taxRate;

cout << "\nTax Details:\n";

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

cout << "Person " << i+1 << ": Income=" << people[i].income

<< ", Tax=" << people[i].tax << endl;

return 0;

Output:

Enter income for person 1: 50000

Enter tax rate (as decimal): 0.2

Enter income for person 2: 30000

Enter tax rate (as decimal): 0.15

Enter income for person 3: 75000

Enter tax rate (as decimal): 0.25

Enter income for person 4: 40000

Enter tax rate (as decimal): 0.18

24
Cs-308 assignment

Enter income for person 5: 60000

Enter tax rate (as decimal): 0.22

Tax Details:

Person 1: Income=50000, Tax=10000

Person 2: Income=30000, Tax=4500

Person 3: Income=75000, Tax=18750

Person 4: Income=40000, Tax=7200

Person 5: Income=60000, Tax=13200

Prime Numbers in Array

#include <iostream>

using namespace std;

bool isPrime(int num) {

if (num <= 1) return false;

for (int i = 2; i * i <= num; i++) {

if (num % i == 0) return false;

return true;

int main() {

int numbers[10], count = 0;

25
Cs-308 assignment

cout << "Enter 10 integers:\n";

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

cin >> numbers[i];

if (isPrime(numbers[i])) count++;

cout << "Number of prime numbers: " << count << endl;

return 0;

Output:

Enter 10 integers:

7 12 23 34 41 56 67 78 89 97

Number of prime numbers: 6

Quadratic Equation Roots

#include <iostream>

#include <cmath>

using namespace std;

void findRoots(double a, double b, double c) {

double discriminant = b*b - 4*a*c;

if (discriminant < 0) {

cout << "Sorry, the roots are not real." << endl;

26
Cs-308 assignment

} else {

double root1 = (-b + sqrt(discriminant)) / (2*a);

double root2 = (-b - sqrt(discriminant)) / (2*a);

cout << "Roots of the equation are " << root1 << " and " << root2 << endl;

int main() {

double a, b, c;

cout << "Enter coefficients a, b, c: ";

cin >> a >> b >> c;

if (a == 0) {

cout << "This is not a quadratic equation!" << endl;

} else {

findRoots(a, b, c);

return 0;

/* Output:

Enter coefficients a, b, c: 1 1 -6

Roots of the equation are 2 and -3

Enter coefficients a, b, c: 1 0 9

27
Cs-308 assignment

Sorry, the roots are not real.

*/

Student Grades

#include <iostream>

using namespace std;

int main() {

int marks[10], gradeA = 0, gradeB = 0, gradeC = 0, gradeF = 0;

cout << "Enter marks of 10 students: ";

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

cin >> marks[i];

if (marks[i] >= 80) gradeA++;

else if (marks[i] >= 60) gradeB++;

else if (marks[i] >= 40) gradeC++;

else gradeF++;

cout << "\nGrade Distribution:\n";

cout << "A: " << gradeA << " students\n";

cout << "B: " << gradeB << " students\n";

cout << "C: " << gradeC << " students\n";

cout << "F: " << gradeF << " students\n";

28
Cs-308 assignment

return 0;

/* Output:

Enter marks of 10 students: 85 72 65 92 38 45 78 83 55 67

Grade Distribution:

A: 3 students

B: 3 students

C: 2 students

F: 2 students

*/

```

Employee Tax Calculation

#include <iostream>

#include <string>

using namespace std;

int main() {

const int NUM_EMPLOYEES = 10;

string names[NUM_EMPLOYEES];

float salaries[NUM_EMPLOYEES];

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

cout << "Enter name of employee " << i+1 << ": ";

29
Cs-308 assignment

cin >> names[i];

cout << "Enter monthly salary: ";

cin >> salaries[i];

cout << "\nEmployee Details:\n";

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

float annualSalary = salaries[i] * 12;

cout << names[i] << ": " << annualSalary;

if (annualSalary >= 250000) {

cout << " - Tax to be paid" << endl;

} else {

cout << " - No tax" << endl;

return 0;

/* Output:

Enter name of employee 1: Alice

Enter monthly salary: 25000

Enter name of employee 2: Bob

Enter monthly salary: 18000

Enter name of employee 3: Carol

30
Cs-308 assignment

Enter monthly salary: 30000

Employee Details:

Alice: 300000 - Tax to be paid

Bob: 216000 - No tax

Carol: 360000 - Tax to be paid

*/

```Programming Exercises – Image 2 (Pointers & Arrays)

Sort Array of Integers Using Pointers

#include <iostream>

using namespace std;

void sort(int* arr, int size) {

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

for (int j = i + 1; j < size; j++) {

if (*(arr + i) > *(arr + j)) {

int temp = *(arr + i);

*(arr + i) = *(arr + j);

*(arr + j) = temp;

31
Cs-308 assignment

int main() {

int arr[5] = {5, 2, 9, 1, 3};

sort(arr, 5);

cout << "Sorted array: ";

for (int i = 0; i < 5; i++)

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

return 0;

Sample Output:

Sorted array: 1 2 3 5 9

Swap Two Integers Using Pointers

#include <iostream>

using namespace std;

void swap(int* a, int* b) {

int temp = *a;

*a = *b;

*b = temp;

32
Cs-308 assignment

int main() {

int x = 10, y = 20;

cout << "Before swap: x = " << x << ", y = " << y << endl;

swap(&x, &y);

cout << "After swap: x = " << x << ", y = " << y << endl;

return 0;

Sample Output:

Before swap: x = 10, y = 20

After swap: x = 20, y = 10

Declare Five Integer Variables and Find Maximum Using Pointers

#include <iostream>

using namespace std;

int main() {

int a = 10, b = 30, c = 25, d = 50, e = 5;

int* ptr[5] = {&a, &b, &c, &d, &e};

int max = *ptr[0];

for (int i = 1; i < 5; i++) {

33
Cs-308 assignment

if (*ptr[i] > max)

max = *ptr[i];

cout << "Maximum value is: " << max << endl;

return 0;

Sample Output:

Maximum value is: 50

Structure with Account ID and Amount Using Dynamic Array

#include <iostream>

using namespace std;

struct Account {

int id;

float amount;

};

void input(Account* acc, int n) {

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

cout << "Enter ID and Amount for Account " << i + 1 << ": ";

cin >> acc[i].id >> acc[i].amount;

34
Cs-308 assignment

void display(Account* acc, int n) {

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

cout << "Account ID: " << acc[i].id << ", Amount: " << acc[i].amount << endl;

int main() {

int n;

cout << "Enter number of account holders: ";

cin >> n;

Account* acc = new Account[n];

input(acc, n);

display(acc, n);

delete[] acc;

return 0;

Sample Output:

Enter number of account holders: 2

35
Cs-308 assignment

Enter ID and Amount for Account 1: 101 2000

Enter ID and Amount for Account 2: 102 3000

Account ID: 101, Amount: 2000

Account ID: 102, Amount: 3000

Average Marks of Students Using Dynamic Array

#include <iostream>

using namespace std;

int main() {

int n;

cout << "Enter number of students: ";

cin >> n;

float* marks = new float[n];

float sum = 0;

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

cout << "Enter marks for student " << i + 1 << ": ";

cin >> marks[i];

sum += marks[i];

cout << "Average marks: " << sum / n << endl;

36
Cs-308 assignment

delete[] marks;

return 0;

Sample Output:

Enter number of students: 3

Enter marks for student 1: 80

Enter marks for student 2: 90

Enter marks for student 3: 100

Average marks: 90

Swap Two Values by Passing Pointers

#include <iostream>

using namespace std;

void swap(int* x, int* y) {

int temp = *x;

*x = *y;

*y = temp;

int main() {

int a = 15, b = 25;

cout << "Before: a = " << a << ", b = " << b << endl;

37
Cs-308 assignment

swap(&a, &b);

cout << "After: a = " << a << ", b = " << b << endl;

return 0;

Prime Check Using Pointer to Function

#include <iostream>

using namespace std;

bool isPrime(int n) {

if (n < 2) return false;

for (int i = 2; i <= n / 2; i++)

if (n % i == 0)

return false;

return true;

int main() {

int num;

bool (*ptr)(int) = isPrime;

cout << "Enter a number: ";

cin >> num;

if (ptr(num))

38
Cs-308 assignment

cout << num << " is a prime number.\n";

else

cout << num << " is not a prime number.\n";

return 0;

}[5/28, 7:51 PM] Home: ✅ Programming Exercises – Image 1

Distance Covered by Players

#include <iostream>

using namespace std;

struct Player {

string name;

int minutes;

int seconds;

};

int main() {

Player p1, p2;

cout << "Enter name, minutes and seconds for Player 1: ";

cin >> p1.name >> p1.minutes >> p1.seconds;

cout << "Enter name, minutes and seconds for Player 2: ";

cin >> p2.name >> p2.minutes >> p2.seconds;

39
Cs-308 assignment

int time1 = p1.minutes * 60 + p1.seconds;

int time2 = p2.minutes * 60 + p2.seconds;

cout << "\nWinner is: ";

if (time1 < time2)

cout << p1.name << endl;

else

cout << p2.name << endl;

return 0;

Sample Output:

Enter name, minutes and seconds for Player 1: Ali 2 45

Enter name, minutes and seconds for Player 2: Sara 3 10

Winner is: Ali

Employee with More Salary

#include <iostream>

using namespace std;

struct Employee {

40
Cs-308 assignment

int code;

float salary;

string grade;

};

int main() {

Employee e1, e2;

cout << "Enter code, salary, and grade for Employee 1: ";

cin >> e1.code >> e1.salary >> e1.grade;

cout << "Enter code, salary, and grade for Employee 2: ";

cin >> e2.code >> e2.salary >> e2.grade;

cout << "\nEmployee with higher salary:\n";

if (e1.salary > e2.salary)

cout << "Code: " << e1.code << ", Salary: " << e1.salary << ", Grade: " << e1.grade << endl;

else

cout << "Code: " << e2.code << ", Salary: " << e2.salary << ", Grade: " << e2.grade << endl;

return 0;

Sample Output:

41
Cs-308 assignment

Enter code, salary, and grade for Employee 1: 101 55000 A

Enter code, salary, and grade for Employee 2: 102 60000 B

Employee with higher salary:

Code: 102, Salary: 60000, Grade: B

Income and Tax of 5 Persons

#include <iostream>

using namespace std;

struct Person {

float income;

float taxRate;

};

int main() {

Person persons[5];

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

cout << "Enter income and tax rate for person " << i + 1 << ": ";

cin >> persons[i].income >> persons[i].taxRate;

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

float tax = persons[i].income * persons[i].taxRate / 100;

cout << "Tax for person " << i + 1 << ": " << tax << endl;

42
Cs-308 assignment

return 0;

Sample Output:

Enter income and tax rate for person 1: 50000 10

Tax for person 1: 5000

Book and Order Structures

#include <iostream>

using namespace std;

struct Book {

int bookID;

string name;

float price;

};

struct Order {

int orderID;

Book books[5];

};

43
Cs-308 assignment

int main() {

Order order;

cout << "Enter Order ID: ";

cin >> order.orderID;

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

cout << "Enter BookID, Name, Price for Book " << i + 1 << ": ";

cin >> order.books[i].bookID >> order.books[i].name >> order.books[i].price;

cout << "\nOrder Details:\n";

cout << "Order ID: " << order.orderID << endl;

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

cout << "Book " << i + 1 << ": " << order.books[i].bookID << ", "

<< order.books[i].name << ", $" << order.books[i].price << endl;

return 0;

Sample Output:

Enter Order ID: 123

Enter BookID, Name, Price for Book 1: 1 Cpp 250

44
Cs-308 assignment

Order Details:

Order ID: 123

Book 1: 1, Cpp, $250

...

[5/28, 7:52 PM] Home: // ===============================

// C++ Programming Exercises

// Covers: Structures, Pointers, Strings

// ===============================

#include <iostream>

#include <cstring>

#include <cctype>

using namespace std;

// ---------- STRUCTURE EXERCISES ----------

struct Player {

int minutes;

int seconds;

float distance;

};

int totalSeconds(Player p) {

return p.minutes * 60 + p.seconds;

45
Cs-308 assignment

void structure_ex1() {

Player p1, p2;

cout << "\n[Structure 1] Player Winner Based on Speed\n";

cout << "Enter distance, minutes and seconds for Player 1: ";

cin >> p1.distance >> p1.minutes >> p1.seconds;

cout << "Enter distance, minutes and seconds for Player 2: ";

cin >> p2.distance >> p2.minutes >> p2.seconds;

float speed1 = p1.distance / totalSeconds(p1);

float speed2 = p2.distance / totalSeconds(p2);

cout << ((speed1 > speed2) ? "Player 1 wins\n" : "Player 2 wins\n");

struct Employee {

int code;

float salary;

string grade;

};

void structure_ex2() {

Employee e1, e2;

cout << "\n[Structure 2] Higher Salary Employee\n";

cout << "Enter code, salary, and grade for Employee 1: ";

cin >> e1.code >> e1.salary >> e1.grade;

46
Cs-308 assignment

cout << "Enter code, salary, and grade for Employee 2: ";

cin >> e2.code >> e2.salary >> e2.grade;

cout << ((e1.salary > e2.salary) ? "Employee 1 has higher salary\n" : "Employee 2 has higher salary\n");

struct Person {

float income;

float taxRate;

};

void structure_ex3() {

Person p[5];

cout << "\n[Structure 3] Tax Calculation\n";

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

cout << "Enter income and tax rate for person " << i + 1 << ": ";

cin >> p[i].income >> p[i].taxRate;

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

cout << "Tax for person " << i + 1 << ": " << (p[i].income * p[i].taxRate / 100) << endl;

struct Book {

int bookID;

string name;

47
Cs-308 assignment

float price;

};

struct Order {

int orderID;

Book books[5];

};

void structure_ex4() {

Order o;

cout << "\n[Structure 4] Book Order\nEnter order ID: ";

cin >> o.orderID;

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

cout << "Enter book ID, name, price for Book " << i + 1 << ": ";

cin >> o.books[i].bookID >> o.books[i].name >> o.books[i].price;

cout << "Order ID: " << o.orderID << endl;

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

cout << "Book " << i + 1 << ": " << o.books[i].bookID << ", " << o.books[i].name << ", $" <<
o.books[i].price << endl;

// ---------- POINTER EXERCISES ----------

void pointer_ex1() {

48
Cs-308 assignment

int arr[5] = {5, 2, 9, 1, 3};

int* p = arr;

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

for (int j = i + 1; j < 5; j++) {

if (*(p + i) > *(p + j)) {

int temp = *(p + i);

*(p + i) = *(p + j);

*(p + j) = temp;

cout << "\n[Pointer 1] Sorted Array: ";

for (int i = 0; i < 5; i++) cout << *(p + i) << " ";

cout << endl;

void pointer_ex2() {

int a = 10, b = 20;

int *x = &a, *y = &b;

int temp = *x;

*x = *y;

*y = temp;

cout << "\n[Pointer 2] Swapped: a = " << a << ", b = " << b << endl;

49
Cs-308 assignment

void pointer_ex3() {

int a = 10, b = 30, c = 25, d = 50, e = 5;

int* arr[] = {&a, &b, &c, &d, &e};

int max = *arr[0];

for (int i = 1; i < 5; i++)

if (*arr[i] > max) max = *arr[i];

cout << "\n[Pointer 3] Maximum Value: " << max << endl;

struct Account {

int id;

float amount;

};

void pointer_ex4() {

int n;

cout << "\n[Pointer 4] Account Details\nEnter number of accounts: ";

cin >> n;

Account* acc = new Account[n];

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

cout << "Enter ID and Amount for Account " << i + 1 << ": ";

cin >> acc[i].id >> acc[i].amount;

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

cout << "Account ID: " << acc[i].id << ", Amount: " << acc[i].amount << endl;

50
Cs-308 assignment

delete[] acc;

void pointer_ex5() {

int n;

cout << "\n[Pointer 5] Average Marks\nEnter number of students: ";

cin >> n;

float* marks = new float[n];

float sum = 0;

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

cout << "Enter marks for student " << i + 1 << ": ";

cin >> marks[i];

sum += marks[i];

cout << "Average marks: " << sum / n << endl;

delete[] marks;

bool isPrime(int n) {

if (n < 2) return false;

for (int i = 2; i <= n / 2; i++)

if (n % i == 0) return false;

return true;

51
Cs-308 assignment

void pointer_ex6() {

int num;

bool (*ptr)(int) = isPrime;

cout << "\n[Pointer 6] Prime Checker\nEnter a number: ";

cin >> num;

cout << (ptr(num) ? "Prime\n" : "Not Prime\n");

// ---------- STRING EXERCISES ----------

void string_ex1() {

char str[100];

cout << "\n[String 1] Palindrome Check\nEnter a string: ";

cin >> str;

int len = strlen(str);

bool isPal = true;

for (int i = 0; i < len / 2; i++)

if (str[i] != str[len - 1 - i]) {

isPal = false;

break;

cout << (isPal ? "Palindrome\n" : "Not Palindrome\n");

52
Cs-308 assignment

void string_ex2() {

char str[200];

cout << "\n[String 2] Count Words & Characters\nEnter a string: ";

cin.ignore();

cin.getline(str, 200);

int words = 0, chars = 0;

bool inWord = false;

for (int i = 0; str[i]; i++) {

if (str[i] != ' ') {

chars++;

if (!inWord) {

words++;

inWord = true;

} else inWord = false;

cout << "Words: " << words << ", Characters (no spaces): " << chars << endl;

void string_ex3() {

char str[100];

cout << "\n[String 3] Reverse String\nEnter a string: ";

cin >> str;

int len = strlen(str);

cout << "Reversed: ";

53
Cs-308 assignment

for (int i = len - 1; i >= 0; i--)

cout << str[i];

cout << endl;

void string_ex4() {

char str[100];

cout << "\n[String 4] Triangle Pattern\nEnter a string: ";

cin >> str;

for (int i = 0; i < strlen(str); i++) {

for (int j = 0; j <= i; j++)

cout << str[j];

cout << endl;

void string_ex5() {

char str[100], ch;

cout << "\n[String 5] Toggle Case\nEnter a string: ";

cin >> str;

cout << "Enter character to toggle: ";

cin >> ch;

for (int i = 0; str[i]; i++) {

if (str[i] == ch) {

if (islower(str[i])) str[i] = toupper(str[i]);

54
Cs-308 assignment

else str[i] = tolower(str[i]);

cout << "Modified: " << str << endl;

int main() {

structure_ex1();

structure_ex2();

structure_ex3();

structure_ex4();

pointer_ex1();

pointer_ex2();

pointer_ex3();

pointer_ex4();

pointer_ex5();

pointer_ex6();

string_ex1();

string_ex2();

string_ex3();

string_ex4();

string_ex5();

55
Cs-308 assignment

return 0;

}// ===============================

// C++ Programming Exercises

// Covers: Structures, Pointers, Strings

// ===============================

#include <iostream>

#include <cstring>

#include <cctype>

using namespace std;

// ---------- STRUCTURE EXERCISES ----------

struct Player {

int minutes;

int seconds;

float distance;

};

int totalSeconds(Player p) {

return p.minutes * 60 + p.seconds;

void structure_ex1() {

Player p1, p2;

cout << "\n[Structure 1] Player Winner Based on Speed\n";

56
Cs-308 assignment

cout << "Enter distance, minutes and seconds for Player 1: ";

cin >> p1.distance >> p1.minutes >> p1.seconds;

cout << "Enter distance, minutes and seconds for Player 2: ";

cin >> p2.distance >> p2.minutes >> p2.seconds;

float speed1 = p1.distance / totalSeconds(p1);

float speed2 = p2.distance / totalSeconds(p2);

cout << ((speed1 > speed2) ? "Player 1 wins\n" : "Player 2 wins\n");

struct Employee {

int code;

float salary;

string grade;

};

void structure_ex2() {

Employee e1, e2;

cout << "\n[Structure 2] Higher Salary Employee\n";

cout << "Enter code, salary, and grade for Employee 1: ";

cin >> e1.code >> e1.salary >> e1.grade;

cout << "Enter code, salary, and grade for Employee 2: ";

cin >> e2.code >> e2.salary >> e2.grade;

cout << ((e1.salary > e2.salary) ? "Employee 1 has higher salary\n" : "Employee 2 has higher salary\n");

57
Cs-308 assignment

struct Person {

float income;

float taxRate;

};

void structure_ex3() {

Person p[5];

cout << "\n[Structure 3] Tax Calculation\n";

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

cout << "Enter income and tax rate for person " << i + 1 << ": ";

cin >> p[i].income >> p[i].taxRate;

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

cout << "Tax for person " << i + 1 << ": " << (p[i].income * p[i].taxRate / 100) << endl;

struct Book {

int bookID;

string name;

float price;

};

58
Cs-308 assignment

struct Order {

int orderID;

Book books[5];

};

void structure_ex4() {

Order o;

cout << "\n[Structure 4] Book Order\nEnter order ID: ";

cin >> o.orderID;

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

cout << "Enter book ID, name, price for Book " << i + 1 << ": ";

cin >> o.books[i].bookID >> o.books[i].name >> o.books[i].price;

cout << "Order ID: " << o.orderID << endl;

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

cout << "Book " << i + 1 << ": " << o.books[i].bookID << ", " << o.books[i].name << ", $" <<
o.books[i].price << endl;

// ---------- POINTER EXERCISES ----------

void pointer_ex1() {

int arr[5] = {5, 2, 9, 1, 3};

int* p = arr;

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

for (int j = i + 1; j < 5; j++) {

59
Cs-308 assignment

if (*(p + i) > *(p + j)) {

int temp = *(p + i);

*(p + i) = *(p + j);

*(p + j) = temp;

cout << "\n[Pointer 1] Sorted Array: ";

for (int i = 0; i < 5; i++) cout << *(p + i) << " ";

cout << endl;

void pointer_ex2() {

int a = 10, b = 20;

int *x = &a, *y = &b;

int temp = *x;

*x = *y;

*y = temp;

cout << "\n[Pointer 2] Swapped: a = " << a << ", b = " << b << endl;

void pointer_ex3() {

int a = 10, b = 30, c = 25, d = 50, e = 5;

int* arr[] = {&a, &b, &c, &d, &e};

int max = *arr[0];

60
Cs-308 assignment

for (int i = 1; i < 5; i++)

if (*arr[i] > max) max = *arr[i];

cout << "\n[Pointer 3] Maximum Value: " << max << endl;

struct Account {

int id;

float amount;

};

void pointer_ex4() {

int n;

cout << "\n[Pointer 4] Account Details\nEnter number of accounts: ";

cin >> n;

Account* acc = new Account[n];

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

cout << "Enter ID and Amount for Account " << i + 1 << ": ";

cin >> acc[i].id >> acc[i].amount;

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

cout << "Account ID: " << acc[i].id << ", Amount: " << acc[i].amount << endl;

delete[] acc;

void pointer_ex5() {

61
Cs-308 assignment

int n;

cout << "\n[Pointer 5] Average Marks\nEnter number of students: ";

cin >> n;

float* marks = new float[n];

float sum = 0;

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

cout << "Enter marks for student " << i + 1 << ": ";

cin >> marks[i];

sum += marks[i];

cout << "Average marks: " << sum / n << endl;

delete[] marks;

bool isPrime(int n) {

if (n < 2) return false;

for (int i = 2; i <= n / 2; i++)

if (n % i == 0) return false;

return true;

void pointer_ex6() {

int num;

bool (*ptr)(int) = isPrime;

cout << "\n[Pointer 6] Prime Checker\nEnter a number: ";

62
Cs-308 assignment

cin >> num;

cout << (ptr(num) ? "Prime\n" : "Not Prime\n");

// ---------- STRING EXERCISES ----------

void string_ex1() {

char str[100];

cout << "\n[String 1] Palindrome Check\nEnter a string: ";

cin >> str;

int len = strlen(str);

bool isPal = true;

for (int i = 0; i < len / 2; i++)

if (str[i] != str[len - 1 - i]) {

isPal = false;

break;

cout << (isPal ? "Palindrome\n" : "Not Palindrome\n");

void string_ex2() {

char str[200];

cout << "\n[String 2] Count Words & Characters\nEnter a string: ";

cin.ignore();

cin.getline(str, 200);

63
Cs-308 assignment

int words = 0, chars = 0;

bool inWord = false;

for (int i = 0; str[i]; i++) {

if (str[i] != ' ') {

chars++;

if (!inWord) {

words++;

inWord = true;

} else inWord = false;

cout << "Words: " << words << ", Characters (no spaces): " << chars << endl;

void string_ex3() {

char str[100];

cout << "\n[String 3] Reverse String\nEnter a string: ";

cin >> str;

int len = strlen(str);

cout << "Reversed: ";

for (int i = len - 1; i >= 0; i--)

cout << str[i];

cout << endl;

64
Cs-308 assignment

void string_ex4() {

char str[100];

cout << "\n[String 4] Triangle Pattern\nEnter a string: ";

cin >> str;

for (int i = 0; i < strlen(str); i++) {

for (int j = 0; j <= i; j++)

cout << str[j];

cout << endl;

void string_ex5() {

char str[100], ch;

cout << "\n[String 5] Toggle Case\nEnter a string: ";

cin >> str;

cout << "Enter character to toggle: ";

cin >> ch;

for (int i = 0; str[i]; i++) {

if (str[i] == ch) {

if (islower(str[i])) str[i] = toupper(str[i]);

else str[i] = tolower(str[i]);

cout << "Modified: " << str << endl;

65
Cs-308 assignment

int main() {

structure_ex1();

structure_ex2();

structure_ex3();

structure_ex4();

pointer_ex1();

pointer_ex2();

pointer_ex3();

pointer_ex4();

pointer_ex5();

pointer_ex6();

string_ex1();

string_ex2();

string_ex3();

string_ex4();

string_ex5();

return 0;

66

You might also like