Programming II – Group Assignment
Page Number: 1
Reference: (Add your reference sources here)
Contents
1. Introduction
2. Simple Calculator Program in C++ Using Functions
3. Five Built‑in C++ Functions (Descriptions & Examples)
4. Description of Terms with C++ Examples
5. Array Sum
6. Array Reverse
7. Array Search
8. Find Minimum and Maximum in an Array
9. Second Largest in an Array
10. String Reverse
11. String Copy
12. Structure Sorting
1. Introduction
This assignment focuses on fundamental C++ programming concepts such as functions, arrays, strings,
and structures. You will learn how to build a simple calculator using user‑defined functions and gain
hands‑on practice with common operations performed on arrays and strings. Understanding these
basics strengthens core programming skills required for advanced topics in software development.
2. C++ Program: Simple Calculator Using Functions
#include <iostream>
using namespace std;
// Function declarations
float add(float a, float b) { return a + b; }
float subtract(float a, float b) { return a - b; }
float multiply(float a, float b) { return a * b; }
float divide(float a, float b) { return b != 0 ? a / b : 0; }
int module(int a, int b) { return b != 0 ? a % b : 0; }
int main() {
float num1, num2;
int choice;
cout << "Simple Calculator" << endl;
1
cout << "1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5.
Modulus" << endl;
cout << "Enter your choice: ";
cin >> choice;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
switch(choice) {
case 1: cout << "Result: " << add(num1, num2); break;
case 2: cout << "Result: " << subtract(num1, num2); break;
case 3: cout << "Result: " << multiply(num1, num2); break;
case 4: cout << "Result: " << divide(num1, num2); break;
case 5: cout << "Result: " << module((int)num1, (int)num2); break;
default: cout << "Invalid choice";
}
return 0;
}
3. Five Built‑in C++ Functions (With Examples)
1. sqrt() – Square Root Function
Returns the square root of a number.
cout << sqrt(25); // Output: 5
2. pow() – Power Function
Raises a number to a given power.
cout << pow(2, 3); // Output: 8
3. toupper() – Convert to Uppercase
cout << char(toupper('a')); // Output: A
4. strlen() – String Length
cout << strlen("Hello"); // Output: 5
2
5. abs() – Absolute Value
cout << abs(-10); // Output: 10
4. Terms Explained With C++ Examples
Array Sum
int arr[] = {1, 2, 3, 4, 5};
int sum = 0;
for(int i = 0; i < 5; i++) sum += arr[i];
Array Reverse
for(int i = 0; i < n/2; i++)
swap(arr[i], arr[n - i - 1]);
Array Search
for(int i = 0; i < n; i++)
if(arr[i] == key) cout << "Found at index " << i;
Find Minimum and Maximum
int min = arr[0], max = arr[0];
for(int i = 1; i < n; i++){
if(arr[i] < min) min = arr[i];
if(arr[i] > max) max = arr[i];
}
Second Largest in an Array
int largest = -9999, second = -9999;
for(int i = 0; i < n; i++){
if(arr[i] > largest){
second = largest;
largest = arr[i];
}
else if(arr[i] > second && arr[i] != largest)
second = arr[i];
}
3
String Reverse
string s = "hello";
reverse([Link](), [Link]());
String Copy
char a[20] = "Hello";
char b[20];
strcpy(b, a);
Structure Sorting
struct Student {
string name;
int marks;
};
Student s[3] = {{"Ali", 80}, {"John", 60}, {"Mary", 90}};
sort(s, s+3, [](Student a, Student b){ return [Link] < [Link]; });
End of Assignment