0% found this document useful (0 votes)
17 views7 pages

C++ Array Practice With Solutions

The document contains a series of C++ programming exercises focused on array manipulation, including input/output, summation, finding maximum/minimum values, counting even/odd numbers, reversing arrays, searching, copying, sorting, and merging arrays. Each exercise is accompanied by a code snippet that demonstrates the solution. The exercises are designed to help learners practice and understand array operations in C++.

Uploaded by

mughal.4th
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)
17 views7 pages

C++ Array Practice With Solutions

The document contains a series of C++ programming exercises focused on array manipulation, including input/output, summation, finding maximum/minimum values, counting even/odd numbers, reversing arrays, searching, copying, sorting, and merging arrays. Each exercise is accompanied by a code snippet that demonstrates the solution. The exercises are designed to help learners practice and understand array operations in C++.

Uploaded by

mughal.4th
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

C++ Array Practice Questions with Solutions

1. Input and Display Array Elements


#include <iostream>
using namespace std;
int main() {
int arr[5];
cout << "Enter 5 numbers: ";
for (int i = 0; i < 5; i++) cin >> arr[i];
cout << "Array elements: ";
for (int i = 0; i < 5; i++) cout << arr[i] << " ";
return 0;
}

2. Find the Sum of Array Elements


#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n], sum = 0;
cout << "Enter elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
cout << "Sum = " << sum;
return 0;
}

3. Find the Maximum and Minimum Element


#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter elements: ";
for (int i = 0; i < n; i++) cin >> arr[i];
int max = arr[0], min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
cout << "Max = " << max << ", Min = " << min;
return 0;
}

4. Count Even and Odd Numbers in Array


#include <iostream>
using namespace std;
int main() {
int n, even = 0, odd = 0;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (arr[i] % 2 == 0) even++;
else odd++;
}
cout << "Even = " << even << ", Odd = " << odd;
return 0;
}

5. Reverse an Array
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter elements: ";
for (int i = 0; i < n; i++) cin >> arr[i];
cout << "Reversed array: ";
for (int i = n - 1; i >= 0; i--) cout << arr[i] << " ";
return 0;
}

6. Search an Element in Array


#include <iostream>
using namespace std;
int main() {
int n, key, found = 0;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter elements: ";
for (int i = 0; i < n; i++) cin >> arr[i];
cout << "Enter element to search: ";
cin >> key;
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
found = 1;
break;
}
}
if (found)
cout << "Element found.";
else
cout << "Element not found.";
return 0;
}

7. Copy Elements of One Array into Another


#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr1[n], arr2[n];
cout << "Enter elements: ";
for (int i = 0; i < n; i++) cin >> arr1[i];
for (int i = 0; i < n; i++) arr2[i] = arr1[i];
cout << "Copied array: ";
for (int i = 0; i < n; i++) cout << arr2[i] << " ";
return 0;
}

8. Sort Array in Ascending Order


#include <iostream>
using namespace std;
int main() {
int n, temp;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter elements: ";
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
cout << "Sorted array: ";
for (int i = 0; i < n; i++) cout << arr[i] << " ";
return 0;
}

9. Find the Second Largest Element


#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter elements: ";
for (int i = 0; i < n; i++) cin >> arr[i];
int first = arr[0], second = -1e9;
for (int i = 1; i < n; i++) {
if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second && arr[i] < first) {
second = arr[i];
}
}
cout << "Second largest element = " << second;
return 0;
}

10. Calculate Average of Array Elements


#include <iostream>
using namespace std;
int main() {
int n;
float sum = 0;
cout << "Enter number of elements: ";
cin >> n;
float arr[n];
cout << "Enter elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
cout << "Average = " << sum / n;
return 0;
}

11. Merge Two Arrays


#include <iostream>
using namespace std;
int main() {
int n1, n2;
cout << "Enter size of first array: ";
cin >> n1;
int arr1[n1];
cout << "Enter elements of first array: ";
for (int i = 0; i < n1; i++) cin >> arr1[i];

cout << "Enter size of second array: ";


cin >> n2;
int arr2[n2], merged[n1 + n2];
cout << "Enter elements of second array: ";
for (int i = 0; i < n2; i++) cin >> arr2[i];

for (int i = 0; i < n1; i++) merged[i] = arr1[i];


for (int i = 0; i < n2; i++) merged[n1 + i] = arr2[i];

cout << "Merged array: ";


for (int i = 0; i < n1 + n2; i++) cout << merged[i] << " ";
return 0;
}

12. Count Frequency of Each Element


#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n], freq[n];
cout << "Enter elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
freq[i] = -1;
}
for (int i = 0; i < n; i++) {
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
freq[j] = 0;
}
}
if (freq[i] != 0)
freq[i] = count;
}
cout << "Element - Frequency\n";
for (int i = 0; i < n; i++) {
if (freq[i] != 0)
cout << arr[i] << " - " << freq[i] << endl;
}
return 0;
}

13. Find Duplicate Elements


#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter elements: ";
for (int i = 0; i < n; i++) cin >> arr[i];
cout << "Duplicate elements: ";
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
cout << arr[i] << " ";
break;
}
}
}
return 0;
}

14. Print Array in Reverse Order Without Changing It


#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
int arr[n];
cout << "Enter elements: ";
for (int i = 0; i < n; i++) cin >> arr[i];
cout << "Array in reverse order: ";
for (int i = n - 1; i >= 0; i--) cout << arr[i] << " ";
return 0;
}

15. Insert an Element at a Given Position


#include <iostream>
using namespace std;
int main() {
int n, pos, value;
cout << "Enter number of elements: ";
cin >> n;
int arr[100];
cout << "Enter elements: ";
for (int i = 0; i < n; i++) cin >> arr[i];
cout << "Enter position and value to insert: ";
cin >> pos >> value;
for (int i = n; i > pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos] = value;
n++;
cout << "Array after insertion: ";
for (int i = 0; i < n; i++) cout << arr[i] << " ";
return 0;
}

You might also like