Skip to main content

C++ program for the list

C++ program for the list

In this post i am going to write a program. The question for that program is:

WAP to perform following actions on an array entered by the user: i) Print the even-valued elements 
ii) Print the odd-valued elements 
iii) Calculate and print the sum and average of the elements of array 
iv) Print the maximum and minimum element of array 
v) Remove the duplicate from the array 
vi) Print the array in reverse order.
 The program should present a menu to the userand ask for one of the options. The menu should also include options to re-enter array and to quit the program.

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

class List{
int list[30];
int i;  // for taking input
public:
void getlist();
void display();
void display_even();
void display_odd();
void display_sum();
void display_average();
void display_max();
void dispaly_min();
void remove_dublicates();
void reverse();
};
//  taking list form keyborad
void List::getlist(){
i=-1;
cout<<"\nEnter any character to end the list entry:";
do{
cout<<"\n Enter value for "<<i+2<<"th cell:";
i++;
}while(cin>>list[i]);
}
// displaying the list
void List::display(){
cout<<"\n The elements of the list is:\n";
for(int j=0;j<i;j++){
cout<<list[j]<<" ";
}
}

// displaying even numbers
void List::display_even(){
cout<<"\n Even numbers in the list are:";
int count=0;
for(int j=0;j<i;j++){

if(list[j]%2==0)
{
cout<<list[j]<<" ";
count++;
}
}
if(!count)
cout<<"\n No even numbers are present in the list.";
}
//displaying odd numbers
void List::display_odd(){
cout<<"\n Odd numbers in the list are:";
int count=0;
for(int j=0;j<i;j++){
if(list[j]%2)
{
cout<<list[j]<<" ";
count++;
}
}
if(!count)
cout<<"\n No odd numbers are present in the list.";
}

// displaying sumation of the list

void List::display_sum(){
cout<<"\n Sum=";
long sum=0;
for(int j=0;j<i;j++){
sum+=list[j];
}
cout<<sum;
}
// displaying average of the list
void List::display_average(){
cout<<"\n Average of the list is:";
long sum=0;
for(int j=0;j<i;j++){
sum+=list[j];
}
sum=sum/i;
cout<<sum;
}
// displaying maximum number present in the list
void List::display_max(){
cout<<"\n Maximun number present in the list:";
int max=list[0];
for(int j=1;j<i;j++){
if(max<list[j])
max=list[j];
}
cout<<max;
}
// displaying minimum number present in the list
void List::dispaly_min(){
cout<<"\n Minimum number present in the list :";
int min=list[0];
for(int j=1;j<i;j++){
if(min>list[j])
min=list[j];
}
cout<<min;
}

// removing dublicate from the list
void List::remove_dublicates(){
int dblct;
int k=i;
for(int j=0;j<k;j++){
dblct=list[j];
for(int l=0;l<k;l++){
if(j==l)
continue;
else if(dblct==list[l])
{
for(int m=l;m<k-1;m++)// deleting the list
{
list[m]=list[m+1];
}
k--;
i--;
}
}
}
}

// reversing the list

void List::reverse(){
int k;
if(i%2)
    k=(i-1)/2;
    else
    k=i/2;
for(int j=0,l=i-1;j<k;j++,l--)
{
int x;
x=list[j];
list[j]=list[l];
list[l]=x;
}
}

// let's strat the main function construction


int main(){
List a;
a.getlist();
a.display();
while(true){
        cout<<"        ----------------------------Menu-----------------------------------";
        cout<<"\n1. Display even numbers present in the list:";
        cout<<"\n2. Display odd numbers present in the list:";
        cout<<"\n3. Display sumation of the list's elements:";
        cout<<"\n4. Display average of the list's elements:";
        cout<<"\n5. Display maximum number present in the list:";
        cout<<"\n6. Display minimum number present in the list:";
        cout<<"\n7. Remove the dublicates from the list:";
        cout<<"\n8. Reverse the list:";
        cout<<"\n9. Re-enter the list:";
        cout<<"\n10. Quit";
        short i;
        cout<<"\n Enter your choice:";
        cin.clear();
        fflush(stdin);
        cin>>i;
        switch(i){
            case 1:
                a.display_even();
                cout<<"\n";
                break;
            case 2:
                a.display_odd();
                cout<<"\n";
                break;
            case 3:
                a.display_sum();
                cout<<"\n";
break;
            case 4:
                a.display_average();
                cout<<"\n";
                break;
            case 5:
                a.display_max();
                cout<<"\n";
                break;
            case 6:
                a.dispaly_min();
                cout<<"\n";
                break;
            case 7:
                a.remove_dublicates();
                a.display();
                cout<<"\n";
                break;
            case 8:
                a.reverse();
                a.display();
                cout<<"\n";
                break;
            case 9:
                a.getlist();
                cout<<"\n";
                a.display();
                cout<<"\n";
                break;
            case 10:
                return 0;
        }
}
return 0;
}

Output:-


C++ program for the list

C++ program for the list
C++ program for the list

C++ program for the list

Comments

Popular posts from this blog

Advantage and disadvantages of Structure and object oriented programming

In my last post, i discussed about structured oriented programming, object oriented programming and language processors. Today i am going to discuss about :- 1. Advantages and Disadvantages of structured oriented programming. 2. Advantages and Disadvantages of object oriented programming. 3.Difference between them. And in the next post i will discuss about:- 1. 1st C++ program and explanation. 2. Structure of a C++ program. 3. Data types, Identifiers, Variable and Keywords. Let's start: Advantages of Structure oriented programming:- 1. It is easy to understand. 2. It is to test a program that is divided into modules(sub-problem) and sub-module(sub-sub-problem). 3. Modification is easier. 4. It results in speedy execution of the program. 5. Development of modules is easier. Disadvantages of Structured oriented programming:- 1. Modules occupy more space in the memory. 2.  Testing of every module is time consuming process. 3. Combination of modules becomes di

size and range of basic data types and modifing the basic types.

Size and range of basic data types:-   Modifying the basic types:-  Except for type void, the basic data types may have various modifiers preceding them.  You use a modifier to alter the meaning of the base type to fit various situations more  precisely. The list of modifiers is shown here: signed unsigned long short You can apply the modifiers signed, short, long, and unsigned to integer base  types. You can apply unsigned and signed to characters. You may also apply long to  double. The above  table shows all valid data type combinations, along with their minimal ranges and approximate bit widths. (These values also apply to a typical C++  implementation.) Remember, the table shows the minimum range that these types will  have as specified by Standard C/C++, not their typical range. For example, on  computers that use two's complement arithmetic (which is nearly all), an integer will have a range of at least 32,767 to –32,768. The use of signed on integers is al

C++ tokens, rule for naming variable and rule for constructing integer constant, real constant and character constant

 C++ character set :- The following image shows the valid character that are used in C++.  C++ data types:- 1st of all we have to know that What is a data? Data is a raw material for constructing a information. For example, the following list are the data:- Ram,17, XII C. And from this data we can construct the information as Ram Aged 17 years is in class XII C. Data may be any number,words, amount,quantity. Now, Let's come on the topic: Latter i will discus about 'structure', 'Union', 'Class', Array', 'function','pointer','reference' for now there are five basic data types that are: 1. int(integer)  2. char(Character) 3. float 4. double  5. void  Now look at variable:  Variable is a entity whose value may change and in programming language it is used to store data. A integer type variable can hold a integer number(number must be a whole number either in positive or in negative but not in fraction),  a ch