Thursday, April 28, 2022

Write a program in C++ which read an array of integers and then perform following operations: Print highest and second highest in an array. Search a given number in an array. Count the even and odd numbers in an array.

 #include <iostream>

using namespace std;
class arr
{
public:
    int size, max, i, c1 = 0, c2 = 0;
    string c;
    void arrays()
    {
        system("cls");
        cout << "enter array size" << endl;
        cin >> size;
        int arr[size];
        system("cls");
        cout << "enter array elements upto index " << size << endl;
        for (i = 0; i < size; i++)
        {
            cin >> arr[i];
        }

        for (i = 0; i < size; i++)
        {
            for (int j = i + 1; j < size; j++) // checks greater between previous and next index and swaps the greater with smaller
            {
                if (arr[j] > arr[i])
                {
                    max = arr[i];
                    arr[i] = arr[j];
                    arr[j] = max;
                }
            }
        }
        system("cls");
        cout << "your array in ascending order" << endl;
        for (i = 0; i < size; i++)
        {
            cout << arr[i] << endl;
        }
        cout << "enter anything to proceed\n"
             << endl;
        cin >> c;
        system("cls");
        cout << "highest and second highest in the array is \n";
        cout << "highest  second highest\n"
             << arr[0] << " \t " << arr[1] << endl;
        for (int i = 0; i < size; i++)
        {
            if (arr[i] % 2 == 0)
            {
                c1++; // counts even
            }
            else
                c2++; // counts odd
        }
        cout << "number of odd and  even numbers are\n";
        cout << "odd \t even\n"
             << c2 << " \t " << c1;
    }
};
int main()
{
    arr obj1;
    obj1.arrays();

    return 0;
}

No comments:

Software scope

 In software engineering, the software scope refers to the boundaries and limitations of a software project. It defines what the software wi...

Popular Posts