Use one dimensional array to solve the following problem. Read 20 integers from data file, each of which is between 10 and 100, inclusive. As each number is read, print it only if it is not a duplicate of a number already read. Provide for the Worst case in which all 20 integers are different. Use the smallest possible array To solve this problem



#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[20];
for (int i=0;i<20;i++)
                {
                cout<<"enter the element no "<<i+1<<endl;
                cin>>arr[i];
                }

for (i=0;i<20;i++)
                {
                for (int j=0;j<20;j++)
                                {
                                if (i!=j)
                                {
                                                if (arr[i]==arr[j])
                                                arr[j]=0;
                                                else
                                                continue;
                                }
                                else
                                continue;
                                }
                }

for (i=0;i<20;i++)
                {
                if (arr[i]<=100&&arr[i]>=10)
                cout<<arr[i]<<endl;
                else
                continue;
                }

getch();
}



OUTPUT:

C++ Programme for a function to encode a string that is passed to it. The string should get converted into an unrecognizable form.



#include<iostream.h>
#include<conio.h>
#include<string.h>


#include<stdio.h>


void encode(char str1[],int l);


int main()
{
               char str[30];
               cout<<"Enter an string:";
               gets(str);
               int len=strlen(str);
               encode(str,len);
               return 0;
}

void encode(char str1[],int l)
{
               int i;
               for(i=0;i<l;i++){
                              str1[i]=str1[i]+10;
               }
               cout<<"\nThe encoded string is:"<<endl;
               for(i=0;i<l;i++){
                              cout<<str1[i];
               }
               cout<<"\nThe proper string is:"<<endl;
               for(i=0;i<l;i++)
                              str1[i]=str1[i]-10;
               for(i=0;i<l;i++)
                              cout<<str1[i];
}





OUTPUT:

C++ Programme for A book shop maintains the inventory of books that are being sold at the shop. The list includes details such as author, title, price, publisher and stock position. Whenever a customer wants a book, the sales person inputs the title and author and the system searches the list and displays whether it is available or not.



#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class stock
{
               char author[50];
               char title[50];
               char pub[50];
               double price;
               int numcopies;
   public:
               stock();
               int access_title(char a[]);
               void input();
               void getdata(int);

};
stock::stock()
{
                  char author[50]={"abc"};
                  char title[50]={"efg"};
                  char pub[50]={"hij"};
                  price=500;
                  numcopies=50;
}
int stock::access_title(char a[])
{
               if(strcmp(title,a))
                              return 0;
               else return 1;
}
void stock::getdata(int num)
{
               if(numcopies>=num)
                              cout<<"\nCost of "<<num<<" books is Rs. "<<(price*num);
               else
                  cout<<"\nSorry! These many copies are unavailable!";
}
void stock::input()
{
               cout<<"\nTitle: ";
               gets(title);
               cout<<"\nAuthor:";
               gets(author);
               cout<<"\nPublisher:";
               gets(pub);
               cout<<"\nPrices:";
               cin>>price;
               cout<<"\ncopies available:";
               cin>>numcopies;
}

void main()
{
   clrscr();

   stock obj[2];
   int n;
   char ttle[50];

   cout<<"Enter details of 3 books";
   for(int i=0;i<2;++i)
               obj[i].input();

   cout<<endl;
   cout<<"\n Enter title of required book\n";
   gets(ttle);

   for(i=0;i<2;i++)
   {
               if(obj[i].access_title(ttle))
               {
                                             cout<<"\nHow many copies? ";
                                             cin>>n;
                                             obj[i].getdata(n);
               }
               else
                                             cout<<"\nBook unavailable";
   }
   getch();
}




OUTPUT:

C++ Programme using class serial to store and display serial's title,duration, number of episodes, serial code



#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class serial
{
               int code;
               char title[20];
               float duration;
               int noe;
               public:
               serial()
               {
                              duration=30;
                              noe=103;
               }
               void newserial();
               void otherentries(int dur,int no);
               void displaydata();
};


void serial::newserial()
{
               cout<<"\n\nEnter the serial code:";
               cin>>code;
               cout<<"Enter the title:";
               gets(title);
}


void serial::otherentries(int dur,int no)
{
               duration=dur;
               noe=no;
}


void serial::displaydata()
{
               cout<<"\tSerial code is:"<<code<<endl;
               cout<<"\tTitle is:"<<title<<endl;
               cout<<"\tDurations is:"<<duration<<endl;
               cout<<"\tNo. of episodes are:"<<noe<<endl<<endl;
}


int main()
{
               clrscr();
               char ch='y';
               int i=0,dur,no;
               serial s1[10];
               while(ch=='y')
               {
                              s1[i].newserial();
                              cout<<"Enter the duration and the no. of episodes:";
                              cin>>dur>>no;
                              s1[i].otherentries(dur,no);
                              i++;
                              cout<<"\n\nDo you want to continue:";
                              cin>>ch;
               }
               cout<<"\n\nThe details you have entered are:"<<endl<<endl;
               for(int j=0;j<i;j++){
                              cout<<"Data of serial "<<j+1<<" is:"<<endl;
                              s1[j].displaydata();
               }
               return 0;
}

OUTPUT:

Imagine a ticket selling booth at a fair. People passing by are requested to purchase a ticket. A ticket is priced at Rs. 2.50. The booth keeps track of the number of people that have visited the fair and of the total amount of money collected. Model this ticket selling booth with a class tick. Include a program to rest this class



#include<iostream.h>
#include<conio.h>
#include<process.h>

class tick
{
               int nop;
               float total;
               public:
               tick()
               {
                              nop=0;
                              total=0;
               }
               void inc();
               void display();
               void displaynop();
};



void tick::inc()
{
               nop++;
               total+=2.50;
               cout<<"\nThe no. of people and the total have beem incremented";
}

void tick::display()
{
               cout<<"\nThe number of people who have entered the fair are:"<<nop<<endl;
               cout<<"The total amount collected till now is:"<<total<<endl;
}


void tick::displaynop()
{
               cout<<"The no. of people who have visited so far are:"<<nop<<endl;
}



int main()
{
               char ch='y';
               int choice;
               tick t1;

               l1:cout<<"\n\n\n\n1.Increment person and total"<<endl;
               cout<<"2.Display no. of people and the amount collected till now"<<endl;
               cout<<"3.Display no. of people who entered"<<endl;
               cout<<"4.Exit"<<endl;
               cout<<"Enter your choice:";
               cin>>choice;
               switch(choice)
               {
                              case 1:t1.inc();
                                             goto l1;
                                             break;
                              case 2:t1.display();
                                             goto l1;
                                             break;
                              case 3:t1.displaynop();
                                             goto l1;
                                             break;
                              case 4:exit(0);
                                     break;

               }
               return 0;
}

OUTPUT:

C++ programme using a class student to calculate the percentage of marks obtained by him



#include<iostream.h>
#include<conio.h>

class student
{
               int roll_no;
               char name[20];
               char class_st[8];
               int marks[5];
               float percentage;
               float calculate();
               public:
               void readmarks();
               void displaymarks();
};


float student::calculate()
{
               percentage=0;
               for(int i=0;i<5;i++)
                              percentage+=marks[i];
               percentage=(percentage/5);
               return percentage;
}


void student::readmarks()
{
               cout<<"Enter the roll no.:";
               cin>>roll_no;
               cout<<"Enter the name:";
               cin>>name;
               cout<<"Enter the class studing in:";
               cin>>class_st;
               cout<<"Enter the marks:"<<endl;
               for(int j=0;j<5;j++){
                              cout<<"\tEnter mark "<<j+1<<":";
                              cin>>marks[j];
               }
}


void student::displaymarks()
{
               cout<<"Roll no:"<<roll_no<<endl;
               cout<<"Name:"<<name<<endl;
               cout<<"Class:"<<class_st<<endl;
               cout<<"Percentage:"<<calculate()<<endl;
}


int main()
{
               student s1;
               s1.readmarks();
               s1.displaymarks();
               return 0;
}

OUTPUT:

C++ Programme to sum the sequence x -(x^2/2!) + (x^4/4!) -(x^6/6!) + .......



#include<iostream.h>
#include<conio.h>
#include<math.h>

int main()
{
                int x,p,i,j;
                double fact=1.0,ans=0;
                cout<<"Enter the value of x:";
                cin>>x;
                cout<<"Enter till what power you want:";
                cin>>p;
                ans=x;
                for(i=2,j=1;i<=p;i++,j++){
                                fact=fact*i;
                                if(i%2==0)
                                                ans+=(pow(-1,j))*((pow(x,i))/(fact));
                }
                cout<<"The sum of the series is:"<<ans;
                return 0;
}


OUTPUT:

C++ Program to reverse the order of given names



#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
               clrscr();
               char *name[]={"anand","naureen","banjot","wahid","sheena"};
               int i,j;
               cout<<"\nOriginal string\n";
               for(i=0;i<5;i++)
                              cout<<name[i]<<endl;
               char *t;
               for(i=0,j=4;i<5/2;i++,j--)
               {
                              t=name[i];
                              name[i]=name[j];
                              name[j]=t;
               }
               cout<<"\nReversed string:\n";
               for(i=0;i<5;i++)
               cout<<name[i]<<endl;
               getch();
}

OUTPUT:

C++ programme to record a score of a cricket match. One array stores information of batting team such as batman's name, runs score ,etc. The other array stores information about bowling team. The program reads in above information and depending on user's choice, it displays either batting team's information or bowling team's information




#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct bat
{
char name[20],modeout[70], indica;
int runs, score, totruns, totove, xtras;
};

struct bowl
{
char name[20];
int ttvrs,rnsgvn,wktstkn;
};
void main()
{
clrscr();
int plno;
int plytyp;
bat pl1[3];
bowl pl2[3];



cout<<"Enter the batsmen details:"<<endl;
for (int i=0;i<3;i++)
{
cout<<"Enter name of player "<<i+1<<endl;
gets (pl1[i].name);
cout<<"enter the runs scored by player "<<i+1<<endl;
cin>>pl1[i].runs;
cout<<"Enter the overs played by the player"<<i+1<<endl;
cin>>pl1[i].totove;
cout<<"Enter the status of the player if out (N)or not(Y)"<<endl;
cin>>pl1[i].indica;
}



cout<<"Enter the bowlers details "<<endl;
for (i=0;i<3;i++)
{
cout<<"Enter the name of the bowler "<<i+1<<endl;
gets(pl2[i].name);
cout<<"Enter the runs given by the bowler "<<i+1<<endl;
cin>>pl2[i].rnsgvn;
cout<<"Enter the wickets taken by the bowler "<<i+1<<endl;
cin>>pl2[i].wktstkn;
cout<<"Enter the total overs played by the  bowler "<<i+1<<endl;
cin>>pl2[i].ttvrs;
}


cout<<"Thank you all details recd"<<endl;
xyz:
cout<<"Select between batsmen(1) or bowlers(2) to see their details"<<endl;
abc:
cin>>plytyp;


switch (plytyp)
{


case 1:
cout<<"Enter the batsman number to see his details "<<endl<<endl<<endl;
cin>>plno;
plno--;
cout<<"Batsman number :"<<plno+1<<endl;
cout<<"Batsman name :";
puts(pl1[plno].name);
cout<<"Runs scored by the batsman :"<<pl1[plno].runs<<endl;
cout<<"Total overs played by the batsman :"<<pl1[plno].totove<<endl;
cout<<"Player status out "<<pl1[plno].indica<<endl;
break;



case 2:
cout<<"Enter the bowlers number to see his details "<<endl<<endl<<endl;
cin>>plno;
plno--;
cout<<"Bowlers name :";
puts(pl2[plno].name);
cout<<"Runs given by the player is :"<<pl2[plno].rnsgvn<<endl;
cout<<"Total overs played by the player :"<<pl2[plno].ttvrs<<endl;
cout<<"Total wickets taken by the user :"<<pl2[plno].wktstkn<<endl;
break;


default:
cout<<"Idiot enter a decent value"<<endl;
goto abc;
}

cout<<endl<<endl<<endl<<"Do you wish to continue? Y-1 N-2"<<endl;
cin>>plno;
if (plno==1)
goto xyz;
else
cout<<"Thank you Press any key to exit";
getch();
}

output:

C++ programme to illustrate a calculator. Perform calculations on two operands using a class calculator. Calculator should add, subtract, multiply and divide operands



#include<iostream.h>
#include<conio.h>
#include<process.h>

class calculator
{
               float result;
               int o1,o2;
               public:
               void enter();
               void showresult();
               void add();
               void sub();
               void mul();
               void div();
               void clear();
};


void calculator::enter()
{
               cout<<"Enter a operant:";
               cin>>o1;
               cout<<"Enter the other operant:";
               cin>>o2;
}


void calculator::showresult()
{
               cout<<"The result of the operation is:"<<result<<endl;
}


void calculator::add()
{
               result=o2+o1;
}

void calculator::sub()
{
               result=o1-o1;
}


void calculator::mul()
{
               result=o1*o2;
}


void calculator::div()
{
               result=o1/o2;
}


void calculator::clear()
{
               result=0;
}



int main()
{
               char ch='y';
               calculator c1;
               while(ch=='y')
               {
                              c1.enter();
                              cout<<"Which operation do you want to perform:";
                              cin>>ch;
                              switch(ch)
                              {
                                             case '+':c1.add();
                                                            break;
                                             case '-':c1.sub();
                                                            break;
                                             case '*':c1.mul();
                                                            break;
                                             case '/':c1.div();
                                                            break;
                                             default :cout<<"Wrong choice:";
                                                            exit(0);
                              }
                              c1.showresult();
                              c1.clear();
                              cout<<"Do you want to continue:";
                              cin>>ch;
               }
               return 0;
}

OUTPUT:

C++ programme to find the sum of the sequence



#include<iostream.h>
#include<conio.h>
#include<math.h>
void sum(int x,int n)
{
               double sum=0,s=0;
               int k=1;
               long fac=1;
               for(int i=1;i<=n-1;i++)
               {
                              for(int j=(2*k);j>=1;j--)
                              {
                                             fac*=j;
                              }
                              sum+=(pow(x,i)/fac);
               }
               s=1+sum;
               cout<<"\nThe sum is:\n";
               cout<<s;
}
void main()
{
               clrscr();
               int x,n;
               cout<<"\nEnter the limiting value\n";
               cin>>n;
               cout<<"\nEnter the value \n";
               cin>>x;
               sum(x,n);
getch();
}

OUTPUT:

C++ programme to find the sum of sequence 1 + 1/1! + 1/2!+.........



#include<iostream.h>
#include<conio.h>

void main()
{
int n,i;
float sum=1.00;
float fact(int a);
clrscr();
cout<<"Enter n:";
cin>>n;
for(i=1;i<n;i++)
{
sum+=(1/fact(i));
}
cout<<"Sum of series ="<<sum;
getch();
}

float fact (int a)
{
int f=1,i;
for (i=1;i<=a;i++)
f*=i;
return f;
}

OUTPUT:

C++ PROGRAM TO COUNT AND DISPLAY THE NUMBER OF LINES NOT STARTING WITH ALPHABET 'A' PRESENT IN THE TEXT FILE "STORY .TXT

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<fstream.h>
int nooflines()
{
               ifstream fin("STORY.TXT");
               int number=-1;
               char line[50];
               while(!fin.eof())
               {
                              fin.getline(line,50,'.');
                              for(int i=0;i<50;i++)
                              {
                              if((line[i]!='a'&&line[i]!='A')&&(line[i-1]=='\r'))
                              number++;
                              }
               }
               cout<<"\nNumber of lines="<<" "<<number;
}
void main()
{
               clrscr();
               ofstream fout;
               char str[500];
               fout.open("STORY.TXT");
               cout<<"\nEnter a string\n";
               gets(str);
               fout<<str<<"\n";
               fout.close();
               nooflines();
               getch();
}

output: