Saturday, 16 May 2015

U4P7

Write a program which include class to represent a vector(a serious of float values). Include member functions to perform the following tasks:
a> To create the vector
b> To modify the value of given elements
c> To Display the given vector in the form (10,20,30)

Code : 
 #include<iostream.h>  
 #include<conio.h>  
 #include<process.h>  
 class vector  
 {  
      public:  
      int size;  
      float v[10];  
      void create()  
      {  
           agn:  
           cout<<"\nEnter size of vector (MAX 10) : ";  
           cin>>size;  
           if(size>10){goto agn;}  
           cout<<"Enter Values for Vector : ";  
           for(int i=0;i<size;i++)  
           {  
                cin>>v[i];  
           }  
           cout<<"Vector Created.\n\n";  
      }  
      void modify()  
      {  
           int old1,new1,flag=0;  
           cout<<"\nOld Element Value : ";  
           cin>>old1;  
           cout<<"New Element Value : ";  
           cin>>new1;  
           for(int i=0;i<size;i++)  
           {  
                if(v[i]==old1)  
                {  
                     v[i] = new1;  
                     flag = 1;  
                }  
           }  
           if(flag==0)  
           {  
                cout<<"\nElement Not Found..";  
           }  
           cout<<"Vector Modified.\n\n";  
      }  
      void display()  
      {  
           cout<<"\nVector V() = (";  
           for(int i=0;i<size;i++)  
           {  
                cout<<v[i];  
                if(i<size-1)  
                {  
                cout<<",";  
                }  
           }  
           cout<<")\n\n";  
      }  
 };  
 void main()  
 {  
      clrscr();  
      vector vec;  
      int task;  
      again:  
      cout<<"Select Vector Task: (1)Create (2)Modify (3)Display (0)Exit || choice: ";  
      cin>>task;  
      switch(task)  
      {  
      case 0:  
           exit(0);  
      case 1:  
           vec.create();  
           goto again;  
           //break;  
      case 2:  
           vec.modify();  
           goto again;  
           //break;  
      case 3:  
           vec.display();  
           goto again;  
           //break;  
      default:  
           cout<<"\n==BAD CHOICE==";  
           goto again;  
           //break;  
      }  
      /*  
      char ch;  
      cout<<"\nCONTINUE? (y/n) : ";  
      cin>>ch;  
      if(ch == 'y' || ch == 'Y')  
      {  
           goto again;  
      }  
      else  
      {  
           exit(0);  
      } */  
      getch();  
 }  


Output :

No comments:

Post a Comment