Arrays

Arrays

Array Programs Day -1

"Array" is a group of similar data type to store series of homogeneous pieces of data that all are same in type.

Here are some key characteristics of arrays:

  1. Fixed Size: The size of an array is typically fixed when it is declared, and it cannot be easily changed during runtime. This means that you need to know the size of the array in advance.

  2. Element Type: All elements in an array must be of the same data type. For example, an array of integers will only store integers, and an array of characters will only store characters.

  3. Contiguous Memory: The elements in an array are stored in adjacent memory locations. This characteristic allows for efficient memory access and is a key feature of arrays.

  4. Zero-Based Indexing: In many programming languages, array indices start from 0. This means that the first element of the array is accessed using index 0, the second with index 1, and so on.

  5. Random Access: Elements in an array can be accessed directly by their index, which allows for constant-time access. This is known as random access.

Write a program to store elements in an array and print them.

#include <iostream>
using namespace std;

int main()
{
    int n ;
    cout<<"Enter Size :: ";
    cin>>n;
    int arr[n];
    for(int i =0;i<n;i++)
    {
        cout<<"Enter element in "<<i+1<<"  ";
        cin>>arr[i];
    }
    for(int i =0;i<n;i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<"\nEnd of the program";
    return 0;
}

Write a program to find the sum of all elements of the array.

#include <iostream>
using namespace std;

int main()
{
    int n , sum = 0 ;
    cout<<"Enter Size :: ";
    cin>>n;
    int arr[n];
    for(int i =0;i<n;i++)
    {
        cout<<"Enter element in "<<i+1<<"  ";
        cin>>arr[i];
    }
    for(int i =0;i<n;i++)
    {
        cout<<arr[i]<<" ";
        sum += arr[i];
    }
    cout<<"\nSum of all elements  :: "<<sum<<endl;
    cout<<"End of the program";
    return 0;
}

Write a program to Print Even and Odd Numbers in an Array

#include <iostream>
using namespace std;

int main()
{
    int n ;
    cout<<"Enter Size :: ";
    cin>>n;
    int arr[n];
    for(int i =0;i<n;i++)
    {
        cout<<"Enter element in "<<i+1<<"  ";
        cin>>arr[i];
    }
    for(int i =0;i<n;i++)
    {
        if(arr[i] % 2 == 0) 
            cout<<arr[i]<<" Even \n";
        else
            cout<<arr[i]<<" Odd\n";
    }
    cout<<"\nEnd of the program";
    return 0;
}

Write a program to Print the Square of Array Elements

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n ;
    cout<<"Enter Size :: ";
    cin>>n;
    int arr[n];
    for(int i =0;i<n;i++)
    {
        cout<<"Enter element in "<<i+1<<"  ";
        cin>>arr[i];
    }
    cout<<"Power of the elements :: \n";
    for(int i =0;i<n;i++)
    {
        cout<<arr[i]<<" - "<<pow(arr[i],2)<<endl;
    }
    cout<<"\nEnd of the program";
    return 0;
}

Write a program to copy the elements of one array into another array.

#include <iostream>
using namespace std;

int main()
{
    int n ;
    cout<<"Enter Size :: ";
    cin>>n;
    int arr1[n], arr2[n];
    for(int i =0;i<n;i++)
    {
        cout<<"Enter element in "<<i+1<<"  ";
        cin>>arr1[i];
    }
    for(int i =0;i<n;i++)
        arr2[i] = arr1[i];
    for(int i =0;i<n;i++)
        cout<<arr1[i]<<" - ";
    cout<<endl;
    for(int i =0;i<n;i++)
        cout<<arr2[i]<<" - ";


    return 0;
}

Write a program to find the maximum and minimum elements in an array.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n ;
    cout<<"Enter Size :: ";
    cin>>n;
    int arr[n], min, max ;
    for(int i =0;i<n;i++)
    {
        cout<<"Enter element in "<<i+1<<"  ";
        cin>>arr[i];
    }
    min = INT_MAX;
    max = INT_MIN;
    for(int i =0;i<n;i++)
    {
        if(min > arr[i])
            min = arr[i];
        if(max < arr[i])
            max = arr[i];
    }
    cout<<"Max  = "<<max <<endl;
    cout<<"Min  = "<<min <<endl;
}

Write a program to separate odd and even integers into separate arrays.

#include <iostream>
using namespace std;

int main() 
{
    const int maxSize = 100; // Maximum size of the array
    int inputArray[maxSize], oddArray[maxSize], evenArray[maxSize];
    int n, oddCount = 0, evenCount = 0;

    // Input the size of the array
    cout << "Enter the size of the array: ";
    cin >> n;

    // Input the elements of the array
    cout << "Enter the elements of the array:\n";
    for (int i = 0; i < n; ++i) {
        cout << "Element " << i + 1 << ": ";
        cin >> inputArray[i];
    }

    // Separate odd and even integers
    for (int i = 0; i < n; ++i) {
        if (inputArray[i] % 2 == 0) {

            evenArray[evenCount] = inputArray[i];
            evenCount++;
        } else {

            oddArray[oddCount] = inputArray[i];
            oddCount++;
        }
    }

    // Display the separated arrays
    cout << "\nOdd numbers array: ";
    for (int i = 0; i < oddCount; ++i) {
        cout << oddArray[i] << " ";
    }

    cout << "\nEven numbers array: ";
    for (int i = 0; i < evenCount; ++i) {
        cout << evenArray[i] << " ";
    }

    cout << endl;

    return 0;
}

Write a program to find the second largest element in an array.

#include <bits/stdc++.h>
using namespace std;

int main() {
    const int maxSize = 100; // Maximum size of the array
    int inputArray[maxSize];
    int n;

    cout << "Enter the size of the array: ";
    cin >> n;
    cout << "Enter the elements of the array:\n";
    for (int i = 0; i < n; ++i) {
        cout << "Element " << i + 1 << ": ";
        cin >> inputArray[i];
    }
    int largest = inputArray[0];
    int secondLargest = INT_MIN; // Initialize to the smallest integer value

    for (int i = 1; i < n; ++i) {
        if (inputArray[i] > largest) {
            secondLargest = largest;
            largest = inputArray[i];
        } else if (inputArray[i] > secondLargest && inputArray[i] != largest) {
            secondLargest = inputArray[i];
        }
    }
    if (secondLargest != INT_MIN) {
        cout << "The second largest element in the array is: " << secondLargest << endl;
    } else {
        cout << "There is no second largest element in the array.\n";
    }
    return 0;
}

Write a program to find the second smallest element in an array.

#include <iostream>
#include <climits>
using namespace std;

int main() {
    const int maxSize = 100; // Maximum size of the array
    int inputArray[maxSize];
    int n;

    // Input the size of the array
    cout << "Enter the size of the array: ";
    cin >> n;

    // Input the elements of the array
    cout << "Enter the elements of the array:\n";
    for (int i = 0; i < n; ++i) {
        cout << "Element " << i + 1 << ": ";
        cin >> inputArray[i];
    }

    // Find the second smallest element
    int smallest = inputArray[0];
    int secondSmallest = INT_MAX; // Initialize to the largest integer value

    for (int i = 1; i < n; ++i) {
        if (inputArray[i] < smallest) {
            secondSmallest = smallest;
            smallest = inputArray[i];
        } else if (inputArray[i] < secondSmallest && inputArray[i] != smallest) {
            secondSmallest = inputArray[i];
        }
    }

    // Display the second smallest element
    if (secondSmallest != INT_MAX) {
        cout << "The second smallest element in the array is: " << secondSmallest << endl;
    } else {
        cout << "There is no second smallest element in the array.\n";
    }
    return 0;
}

Write a Program To Find Maximum Difference Between Two Elements in an Array

#include <iostream>
#include <climits>
using namespace std;

int main() {
    const int maxSize = 100; // Maximum size of the array
    int inputArray[maxSize];
    int n;

    // Input the size of the array
    cout << "Enter the size of the array: ";
    cin >> n;

    // Input the elements of the array
    cout << "Enter the elements of the array:\n";
    for (int i = 0; i < n; ++i) {
        cout << "Element " << i + 1 << ": ";
        cin >> inputArray[i];
    }

    // Find the maximum difference between two elements
    int maxDifference = INT_MIN;

    for (int i = 0; i < n - 1; ++i) {
        for (int j = i + 1; j < n; ++j) {
            int difference = inputArray[j] - inputArray[i];
            if (difference > maxDifference) {
                maxDifference = difference;
            }
        }
    }

    // Display the maximum difference
    if (maxDifference != INT_MIN) {
        cout << "The maximum difference between two elements in the array is: " << maxDifference << endl;
    } else {
        cout << "There is no maximum difference in the array (at least two elements required).\n";
    }

    return 0;
}