Answer to Question #196586 in C++ for kaushal

Question #196586

. Write a C++ program for implementing the following


a)    Create a class called number which will have an array of integers and its size as its members



b)    Write member functions for storing data and display data



c)    Add all types of constructors



d)    Write a member function which takes a character argument, If the argument is E it

should return the sum of all even elements, if it is O it should return the sum

of all odd elements for any other character it should return 0.


1
Expert's answer
2021-05-21T16:49:45-0400
#include<iostream>
using namespace std;
class Numbers
{
    int arr[100],n;
public:
    Numbers()
    {
        n = 0;
        for(int i=0;i<n;i++)
        {
            arr[i]=0;
        }
    }
    void getdata()
    {
        cout<<"Enter the size of array : ";
        cin>>n;
        cout<<"Enter the elements of array : ";
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
        }
    }
    void showData()
    {
        cout<<"Size of array : "<<n;
        cout<<"\nElements of array are : ";
        for(int i=0;i<n;i++)
        {
            cout<<"\n"<<arr[i];
        }
    }
    void SumData()
    {
        char ch;
        int even_sum=0,odd_sum=0;
        cout<<"\nEnter your choice (E/O) : ";
        cin>>ch;
        for(int i=0;i<n;i++)
        {
            if(arr[i]%2==0)
            {
                even_sum = even_sum + arr[i];
            }
            else
            {
                odd_sum = odd_sum + arr[i];
            }
        }
        switch(ch)
        {
            case 'E' : cout<<"Sum of even elements of array is = "<<even_sum;
                       break;
            case 'O' : cout<<"Sum of odd elements of array is = "<<odd_sum;
                       break;
            default : cout<<"Sum = 0 ";
        }
    }
};
int main()
{
    Numbers n1;
    n1.getdata();
    n1.showData();
    n1.SumData();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog