0% found this document useful (0 votes)
2 views

Simple_CPP_Programs_Explained

The document contains multiple C++ programs that demonstrate various functionalities such as sorting an array, finding duplicates, calculating the middle value of time, accepting student details, searching for a value in an array, finding the largest value, and printing ASCII characters. Each program includes code snippets, outputs, and explanations of their operations. These examples serve as practical exercises for understanding basic programming concepts in C++.

Uploaded by

shivamsv167
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Simple_CPP_Programs_Explained

The document contains multiple C++ programs that demonstrate various functionalities such as sorting an array, finding duplicates, calculating the middle value of time, accepting student details, searching for a value in an array, finding the largest value, and printing ASCII characters. Each program includes code snippets, outputs, and explanations of their operations. These examples serve as practical exercises for understanding basic programming concepts in C++.

Uploaded by

shivamsv167
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Create an Array and Sort It (Ascending & Descending)

Solution:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr[10] = {5, 2, 9, 1, 5, 6, 3, 7, 8, 4};
sort(arr, arr + 10); // Ascending
cout << "Ascending: ";
for (int i : arr) cout << i << " ";
sort(arr, arr + 10, greater<int>()); // Descending
cout << "\nDescending: ";
for (int i : arr) cout << i << " ";
return 0;
}

Output:
Ascending: 1 2 3 4 5 5 6 7 8 9
Descending: 9 8 7 6 5 5 4 3 2 1

Explanation:
This program sorts an array in ascending order and then in descending order using
nested loops.

2. Find Duplicate Numbers in an Array

Solution:
#include <iostream>
using namespace std;
int main() {
int arr[10] = {2, 4, 5, 6, 4, 7, 8, 5, 9, 2};
cout << "Duplicates: ";
for (int i = 0; i < 10; i++)
for (int j = i + 1; j < 10; j++)
if (arr[i] == arr[j]) cout << arr[i] << " ";
return 0;
}

Output:
Duplicates: 2 4 5

Explanation:
This program finds duplicate numbers by comparing each element with all the
elements after it.

3. Find and Display the Middle Value of Given Time

Solution:
#include <iostream>
using namespace std;
int main() {
int h, m, s;
cout << "Enter time (HH MM SS): ";
cin >> h >> m >> s;
cout << "Middle value: " << (h + m + s) / 3;
return 0;
}

Output:
Middle value: (HH+MM+SS)/3

Explanation:
This program takes input for time in hours, minutes, and seconds.
It calculates the middle value by finding the average using the formula:
Middle Value = (Hours + Minutes + Seconds) / 3
Finally, it prints the middle value.
Example:
Input: 10 30 50
Calculation: (10 + 30 + 50) / 3 = 30
Output: Middle value: 30

4. Accept and Display Student Details

Solution:
#include <iostream>
using namespace std;
int main() {
int roll[10], classNum[10];
string name[10], section[10];
for (int i = 0; i < 10; i++)
cin >> roll[i] >> name[i] >> classNum[i] >> section[i];
for (int i = 0; i < 10; i++)
cout << roll[i] << " " << name[i] << " " << classNum[i] << " " << section[i] << "\n";
return 0;
}

Output:
Roll No Name Class Section
1 John 10 A
2 Alice 12 B
...

Explanation:
This program takes student details and then displays them.

5. Find a Value in an Array

Solution:
#include <iostream>
using namespace std;
int main() {
int arr[10] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 0}, key;
cout << "Enter number: ";
cin >> key;
for (int i = 0; i < 10; i++)
if (arr[i] == key) { cout << "Found at " << i + 1; return 0; }
cout << "Not found";
return 0;
}

Output:
Found at position 5 or Not found

Explanation:
This program searches for a number in the array and prints its position.

6. Find the Largest Value in an Array

Solution:
#include <iostream>
using namespace std;
int main() {
int arr[10] = {1, 45, 23, 89, 56, 90, 12, 77, 34, 99}, max = arr[0];
for (int i = 1; i < 10; i++)
if (arr[i] > max) max = arr[i];
cout << "Largest: " << max;
return 0;
}

Output:
Largest: 99

Explanation:
This program finds the largest number in an array using a simple loop.

7. Print ASCII Characters from 0 to 255

Solution:
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i <= 255; i++) cout << i << " = " << char(i) << "\n";
return 0;
}

Output:
0 = NULL
1 = SOH
...
255 = ÿ

Explanation:
This program loops from 0 to 255 and prints the ASCII characters.
Each number represents an ASCII character.

Key ASCII Ranges:


- 0 to 31: Non-printable characters (e.g., NULL, ENTER)
- 48 to 57: Digits (0-9)
- 65 to 90: Uppercase letters (A-Z)
- 97 to 122: Lowercase letters (a-z)
- 255: Extended ASCII character 'ÿ'

Example Output (Partial):


65 = A
66 = B
67 = C
97 = a
98 = b
99 = c

You might also like