0% found this document useful (0 votes)
3 views6 pages

Array Programs

The document contains ten C++ programs that demonstrate various array operations. These include storing integers, calculating sum and average, finding largest and smallest elements, reversing an array, counting even and odd numbers, performing linear search, copying arrays, sorting using bubble sort, calculating frequency of elements, and merging two arrays. Each program includes user input and output to illustrate the functionality.

Uploaded by

younas ali
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)
3 views6 pages

Array Programs

The document contains ten C++ programs that demonstrate various array operations. These include storing integers, calculating sum and average, finding largest and smallest elements, reversing an array, counting even and odd numbers, performing linear search, copying arrays, sorting using bubble sort, calculating frequency of elements, and merging two arrays. Each program includes user input and output to illustrate the functionality.

Uploaded by

younas ali
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

1.

Store 5 integers in an array and print them


#include <iostream>
using namespace std;
int main()

{
int arr[5]; // Array of size 5
cout << "Enter 5 integers: ";
for(int i = 0; i < 5; i++)

{
cin >> arr[i]; // Taking input in array
}
cout << "You entered: ";
for(int i = 0; i < 5; i++)

{
cout << arr[i] << " "; // Printing elements
}
}

2. Sum and Average of Array Elements


#include <iostream>
using namespace std;
int main()

{
int arr[5], sum = 0;
cout << "Enter 5 numbers: ";

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

{
cin >> arr[i]; // Input
sum = sum + arr[i]; // Adding each value
}

float avg = sum / 5.0; // Average (use 5.0 for float result)
cout << "Sum = " << sum << endl;
cout << "Average = " << avg;

3. Largest and Smallest Element in Array


#include <iostream>
using namespace std;
int main()

{
int arr[5];
cout << "Enter 5 numbers: ";
for(int i = 0; i < 5; i++)
cin >> arr[i];

int largest = arr[0]; // Assume first element is largest


int smallest = arr[0]; // Assume first element is smallest

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

{
if(arr[i] > largest)
largest = arr[i];
if(arr[i] < smallest)
smallest = arr[i];
}
cout << "Largest = " << largest ;
cout << "Smallest = " << smallest;
}

4. Reverse the Array


#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 << "Reversed array: ";
for(int i = 4; i >= 0; i--)

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

5. Count Even and Odd Numbers


#include <iostream>
using namespace std;
int main()

{
int arr[5];
int even = 0, odd = 0;
cout << "Enter 5 numbers: ";
for(int i = 0; i < 5; i++)

{
cin >> arr[i];
if(arr[i] % 2 == 0)
even++; // Even number
else
odd++; // Odd number
}
cout << "Even count = " << even << endl;
cout << "Odd count = " << odd;
}

6. Linear Search
#include <iostream>
using namespace std;
int main() {
int arr[5], key;
cout << "Enter 5 numbers: ";
for(int i = 0; i < 5; i++)
cin >> arr[i];
cout << "Enter number to search: ";
cin >> key;

bool found = false; // assume not found

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

{
if(arr[i] == key)

{
found = true;
cout << "Element found at index " << i;
break;
}
}

if(!found)
cout << "Element not found!";
}

7. Copy One Array to Another


#include <iostream>
using namespace std;

int main() {

int arr1[5], arr2[5];

cout << "Enter 5 numbers: ";


for(int i = 0; i < 5; i++)
cin >> arr1[i];
// Copying arr1 → arr2
for(int i = 0; i < 5; i++)
arr2[i] = arr1[i];

cout << "Copied array: ";


for(int i = 0; i < 5; i++)
cout << arr2[i] << " ";
}

8. Sort Array (Bubble Sort)


#include <iostream>
using namespace std;
int main()

{
int arr[5];
cout << "Enter 5 numbers: ";
for(int i = 0; i < 5; i++)
cin >> arr[i];
// Bubble Sort
for(int i = 0; i < 5; i++)

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

{
if(arr[j] > arr[j+1])

// swap numbers for arrange numbers


int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

cout << "Sorted array: ";


for(int i = 0; i < 5; i++)
cout << arr[i] << " ";
}

9. Frequency of Elements
#include <iostream>
using namespace std;

int main()
{

int arr[5], freq[5];

cout << "Enter 5 numbers: ";


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

{
cin >> arr[i];
freq[i] = -1; // initialize
}

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

{
int count = 1;
if(freq[i] != 0) { // not counted before
for(int j = i+1; j < 5; j++)

{
if(arr[i] == arr[j])

{
count++;
freq[j] = 0; // mark counted
}
}
freq[i] = count;
}
}
cout << "Element Frequency:";
for(int i = 0; i < 5; i++)

{
if(freq[i] != 0)
cout << arr[i] << " occurs " << freq[i] << " times";
}
}

10. Merge Two Arrays


#include <iostream>
using namespace std;
int main() {
int arr1[5], arr2[5], merged[10];
cout << "Enter 5 elements of first array: ";
for(int i = 0; i < 5; i++)
cin >> arr1[i];
cout << "Enter 5 elements of second array: ";
for(int i = 0; i < 5; i++)
cin >> arr2[i];
// Copy arr1 into merged
for(int i = 0; i < 5; i++)
merged[i] = arr1[i];
// Copy arr2 into merged after arr1
for(int i = 0; i < 5; i++)
merged[5 + i] = arr2[i];
cout << "Merged array: ";
for(int i = 0; i < 10; i++)
cout << merged[i] << " ";
}

You might also like