C++ PROGRAMS (STAGE-III)


C++ PROGRAMS


// PROGRAM TO REVERSE A VECTOR

#include<iostream.h>
#include<conio.h>
void main()
{
char choice;
do
{
clrscr();
int vec[10], count1, count2;
cout<<"ENTER TEN ELEMENTS FOR VECTOR :\n";
for(count1=0;count1<10;count1++)
cin>>vec[count1];
cout<<"\n\n";
for(count1=0, count2=9;count1<5;count1++,count2--)
{
vec[count1]=vec[count1]+vec[count2];
vec[count2]=vec[count1]-vec[count2];
vec[count1]=vec[count1]-vec[count2];
}
cout<<"\n\nTHE ORIGINAL VECTOR IS AS FOLLOWS : \n\n";
for(count1=0;count1<10;count1++)
cout<<" " <<vec[count1];
cout<<"\n\nTHE REVERSED VECTOR IS AS FOLLOWS : \n\n";
for(count1=0; count1<10;count1++)
cout<<" "<<vec[count1];
cout<<"\n\nDO YOU WISH TO RE-EXECUTE THE PROGRAM?(Y/N) : ";
cin>>choice;
}while(choice=='y'||choice=='Y');
}



/* OUTPUT
ENTER TEN ELEMENTS FOR VECTOR :
12
23
34
45
56
67
78
89
90
98
THE ORIGINAL VECTOR IS AS FOLLOWS :
12 23 34 45 56 67 78 89 90 98
THE REVERSED VECTOR IS AS FOLLOWS :
98 90 89 78 67 56 45 34 23 12
DO YOU WISH TO RE-EXECUTE THE PROGRAM?(Y/N) : N
*/


_____________http://sundcs.blogspot.com/____________




//PROGRAM TO FIND THE LARGEST AND SMALLEST ELEMENTS IN A VECTOR

#include<iostream.h>
#include<conio.h>
void main()
{
char choice;
do
{
clrscr();
int count, num, vec[50], large, small;
cout<<"ENTER HOW MANY ELEMENTS ARE THERE IN THE VECTOR (MAX 50) : ";
cin>>num;
cout<<"\nENTER THE VALUES IN THE VECOR \n";
for(count=0; count<num; count++)
cin>>vec[count];
large=small=vec[1];
for(count=0; count<num;count++)
{
if(vec[count]>large)
large=vec[count];
else if(vec[count]<small)
small=vec[count];
else
continue;
}
cout<<"\nTHE LARGEST ELEMENT IS : "<<large<<endl;
cout<<"\nTHE SMALLEST ELEMENT IS : "<<small<<endl;
cout<<"\nDO YOU WISH TO RE-EXECUTE THE PROGRAM?(Y/N) : ";
cin>>choice;
}while(choice=='y'||choice=='Y');
}



/*OUTPUT
ENTER HOW MANY ELEMENTS ARE THERE IN THE VECTOR (MAX 50) : 15
ENTER THE VALUES IN THE VECOR
12
23
34
45
56
67
78
89
90
98
87
76
65
54
34
THE LARGEST ELEMENT IS : 98
THE SMALLEST ELEMENT IS : 12
DO YOU WISH TO RE-EXECUTE THE PROGRAM? (Y/N) : n
*/


_____________http://sundcs.blogspot.com/____________




// PROGRAM TO DELETE THE DUPLICATE ELEMENTS FROM A VECTOR

#include<iostream.h>
#include<conio.h>
void main()
{
char choice;
int count1, count2, count3, num, ans=0;
float vec[20];
do
{
clrscr();
cout<<"\nENTER THE SIZE OF THE VECTOR(MAX. 20) : ";
cin>>num;
cout<<"\nENTER THE ELEMENTS FOR THE VECTOR: \n";
for(count1=0; count1<num; count1++)
cin>>vec[count1];
cout<<"\nTHE ORIGINAL VECTOR:\n";
for(count1=0; count1<num; count1++)
cout<<"\n"<<vec[count1];
for(count1=0; count1<num; count1++)
{
for(count2=count1+1; count2<num; count2++)
{
if(vec[count1]==vec[count2])
{
num=num-1;
for(count3=count2; count3<num; count3++)
vec[count3] =vec [count3+1];
ans=1;
count2--;
}
}
}
if(ans==0)
cout<<"\nVECTOR IS WITHOUT DUPLICATES:\n";
else
{
cout<<"\n\nVECTOR AFTER DELETING THE DUPLICATES: \n";
for(count2=0; count2<num; count2++)
cout<<"\n"<<vec[count2];
}
cout<<"\n\nDO YOU WISH TO RE-EXECUTE THE PROGRAM? (Y/N) : ";
cin>>choice;
}while(choice=='y'||choice=='Y');
}



/* OUTPUT
ENTER THE SIZE OF THE VECTOR(MAX. 20) : 9
ENTER THE ELEMENTS FOR THE VECTOR:
231
345
32
45
76
34
32
231
345
THE ORIGINAL VECTOR:
231
345
32
45
76
34
32
231
345
VECTOR AFTER DELETING THE DUPLICATES:
231
345
32
45
76
34
DO YOU WISH TO RE-EXECUTE THE PROGRAM? (Y/N) : n
*/


_____________http://sundcs.blogspot.com/____________




// PLAYER DETAILS USING ARRAYS

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char name[30];
int count, matches;
float runs[10],balls[10];
long double sr[10], avgsr, temp=0;
cout <<"\nENTER PLAYER NAME : ";
cin.getline(name,30);
cout<<"\nENTER NUMBER OF MATCHES PLAYED : ";
cin>>matches;
for(count=1;count<=matches;count++)
{
cout<<"\nENTER RUNS SCORED IN MATCH "<<count<<" : ";
cin>>runs[count];
cout<<"\nENTER BALLS PLAYED IN MATCH "<<count<<" : ";
cin >> balls[count];
sr[count]=(runs[count]*100)/balls[count];
temp+=sr[count];
}
avgsr=temp/matches;
clrscr();
cout<<"\nPLAYER NAME : "<< name <<endl;
for(count=1;count<=matches;count++)
{
cout<<"\nRUNS SCORED IN MATCH "<<count<<" : "<< runs[count] <<endl;
cout<<"\nBALLS PLAYED IN MATCH "<<count<<" : "<< balls[count]<<endl;
cout<<"\nSTRIKE RATE IN MATCH "<<count<< sr[count]<<endl<<endl;
}
cout<<"\nAVERAGE STRIKE RATE : " <<avgsr<<endl;
getch();
}



/*OUTPUT
ENTER PLAYER NAME : ROHAN GAVASKAR
ENTER NUMBER OF MATCHES PLAYED : 5
ENTER RUNS SCORED IN MATCH 1 : 79
ENTER BALLS PLAYED IN MATCH 1 : 76
ENTER RUNS SCORED IN MATCH 2 : 89
ENTER BALLS PLAYED IN MATCH 2 : 95
ENTER RUNS SCORED IN MATCH 3 : 84
ENTER BALLS PLAYED IN MATCH 3 : 97
ENTER RUNS SCORED IN MATCH 4 : 108
ENTER BALLS PLAYED IN MATCH 4 : 105
ENTER RUNS SCORED IN MATCH 5 : 120
ENTER BALLS PLAYED IN MATCH 5 : 150
PLAYER NAME : ROHAN GAVASKAR
RUNS SCORED IN MATCH 1 : 79
BALLS PLAYED IN MATCH 1 : 76
STRIKE RATE IN MATCH 1103.947368
RUNS SCORED IN MATCH 2 : 89
BALLS PLAYED IN MATCH 2 : 95
STRIKE RATE IN MATCH 293.684211
RUNS SCORED IN MATCH 3 : 84
BALLS PLAYED IN MATCH 3 : 97
STRIKE RATE IN MATCH 386.597938
RUNS SCORED IN MATCH 4 : 108
BALLS PLAYED IN MATCH 4 : 105
STRIKE RATE IN MATCH 4102.857143
RUNS SCORED IN MATCH 5 : 120
BALLS PLAYED IN MATCH 5 : 150
STRIKE RATE IN MATCH 580
AVERAGE STRIKE RATE : 93.417332
*/


_____________http://sundcs.blogspot.com/____________




//WHITE SPACE CONFIRMATION

#include<iostream.h>
#include<conio.h>
void main()
{
char choice, ch;
do
{
clrscr();
cout<<"ENTER THE CHARACTER : ";
cin.get(ch);
if(ch=='\t')
cout<<"\n\nWHITE SPACE \'TAB\' CHARACTER \n";
else if (ch=='\n')
cout<<"\n\nWHITE SPACE \'NEW LINE \' CHARACTER \n";
else if (ch==' ')
cout<<"\n\nWHITE SPACE \'SPACE\' CHARACTER \n";
else
cout<<"\n\n IT IS A DIFFERENT CHARACTER AND NOT A WHITE SPACE.";
cout<<"\n\nDO YOU WISH TO REPEAT THE PROGRAM? (Y/N) : ";
cin>>choice;
}while(choice=='y'||choice=='Y');
}



/*OUTPUT
ENTER THE CHARACTER :
WHITE SPACE 'SPACE' CHARACTER
DO YOU WISH TO REPEAT THE PROGRAM? (Y/N) : Y
ENTER THE CHARACTER :
WHITE SPACE 'NEW LINE ' CHARACTER
DO YOU WISH TO REPEAT THE PROGRAM? (Y/N) : Y
ENTER THE CHARACTER :
WHITE SPACE 'TAB' CHARACTER
DO YOU WISH TO REPEAT THE PROGRAM? (Y/N) : N
*/


_____________http://sundcs.blogspot.com/____________




/* CONFIRM PRESENCE OF A CHARACTER IN A STRING*/

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char choice, ch, str[70], flag;
int len, count;
do
{
clrscr();
cout<<"\nENTER THE STRING:\n\n";
cin.getline(str, 70);
len=strlen(str);
cout<<"\nENTER A CHARACTER: ";
cin.get(ch);
flag='n';
for(count=0;str[count]!=len;count++)
{
if(ch==str[count])
{
flag='Y';
break;
}
}
if(flag=='Y')
{
cout<<"\n";
cout.put(ch);
cout<<" is contained in the string : \n\n";
cout.write(str,len);
}
else
{
cout<<"\n";
cout.put(ch);
cout<<" is not contained in the string : \n\n";
cout.write(str,len);
}
cout<<"\n\nDO YOU WISH TO REPEAT THE PROGRAM?
(Y/N) : ";
cin>>choice;
}while(choice=='y'||choice=='Y');
}



/* OUTPUT
ENTER THE STRING:
lion and tiger kileed the zebra
ENTER A CHARACTER: z
z is contained in the string :
lion and tiger kileed the zebra
DO YOU WISH TO REPEAT THE PROGRAM? (Y/N) : n
*/


_____________http://sundcs.blogspot.com/____________




//COUNT THE NUMBER OF VOWELS IN A STRING.

#include<iostream.h>
#include<conio.h>
void main()
{
char str[25];
int count, consonant=0, vowel=0, digit=0;
clrscr();
cout<<"ENTER A WORD : ";
cin.getline(str,25);
for(count=0;str[count]!='\0';count++)
{
if(str[count]=='A'||str[count]=='a'||str[count]=='E'
||str[count]=='e'||str[count]=='I'||str[count]=='i'||
str[count]=='O'||str[count]=='o'||str[count]=='U'||
str[count]=='u')
{
vowel++;
}
else
{
consonant++;
}
}
cout<<"consonant: "<<consonant;
cout<<"\nvowel: "<<vowel;
getch();
}


_____________http://sundcs.blogspot.com/____________




//ENCRYPTION OF A STRING

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
char choice, str1[21], str2[21], ch;
int count=0;
do
{
clrscr();
cout<<"ENTER A STRING : ";
gets(str1);
for (count=0; str1[count]!='\0'; count++)
str2[count]=255-str1[count];
str2[count]='\0';
puts("\nTHE ENCRYPTED STRING IS : ");
cout<<str2;
cout<<"\n\nDO YOU WISH TO REPEAT THE PROGRAM? (Y/N) : ";
cin>>choice;
}while(choice=='y'||choice=='Y');
}



/* OUTPUT
ENTER A STRING : godfather
THE ENCRYPTED STRING IS :
˜›™ž‹—š
DO YOU WISH TO REPEAT THE PROGRAM? (Y/N) : y
ENTER A STRING : rachit
THE ENCRYPTED STRING IS :
žoe—–‹
DO YOU WISH TO REPEAT THE PROGRAM? (Y/N) : n
ENTER A STRING : GOOD MORNING TEACHER
THE ENCRYPTED STRING IS :
¸°°»ß²°±¶±¸ß«º¾¼.º
DO YOU WISH TO REPEAT THE PROGRAM? (Y/N) : N
*/


_____________http://sundcs.blogspot.com/____________




// CONVERT A STRING INTO UPPERCASE

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
void main()
{
clrscr();
char str[50];
int flag=1;
cout<<"\nENTER A STRING : \n\n";
cin.getline(str,50);
for(int i=0; str[i]!='\0';i++)
{
if(islower(str[i]))
{
flag=1;
str[i]=toupper(str[i]);
}
}
if((flag==1)||(str[i]=='\0'))
{
cout<<"\nUPPERCASE STRING IS :\n\n";
cout<<str;
}
getch();
}



/*OUTPUT
ENTER A STRING :
rachit is coding programs for his project
UPPERCASE STRING IS :
RACHIT IS CODING PROGRAMS FOR HIS PROJECT
*/


_____________http://sundcs.blogspot.com/____________




// PROGRAM TO FIND THE SUBSTRING OF A GIVEN STRING.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void main()
{
clrscr();
char mainstr[50], substr[50];
int pos, count, count1, count2, len, num, temp;
cout<<"ENTER THE MAIN STRING ( MAX 49 CHARACTERS) : \n";
cin.getline(mainstr, 50);
len=strlen(mainstr);
cout<<"\nENTER THE STARTING POSITION OF SUBSTRING : ";
cin>>pos;
if(pos>len)
{
cout<<"\n\nSTARTING POSITION EXCEEDS THE TOTAL LENGTH OF STRING";
exit(0);
}
cout<<"\n\nENTER THE NUMBER OF CHARACTERS IN SUBSTRING : ";
cin>>count;
if(pos<=0)
{
cout<<"\n\nEXTRACTED STRING IS EMPTY.";
exit(0);
}
else if (((pos+count)-1)>len)
{
cout<<"\n\nSTRING TO BE EXTRACTED EXCEEDS LENGTH \n";
num=(len-pos);
}
else
num=count;
count2=0;
for(count1=--pos; count2<num;count2++, count1++)
substr[count2]=mainstr[count1];
cout<<"\n\nTHE SUBSTRING IS : ";
cout<<substr;
for(count=0;count<50;count++)
substr[count]='\0';
getch();
}



/*OUTPUT
ENTER THE MAIN STRING ( MAX 49 CHARACTERS) :
GOOD MORNING TEACHER
ENTER THE STARTING POSITION OF SUBSTRING : 3
ENTER THE NUMBER OF CHARACTERS IN SUBSTRING : 6
THE SUBSTRING IS : OD MOR
*/


_____________http://sundcs.blogspot.com/____________




// PROGRAM TO SEARCH FOR A PATTERN STRING IN THE THE MAIN STRING

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char mainstr[50], patstr[50];
int count1, count2, count3=0, len1, len2, flag, pos, found=0;
cout<<"ENTER THE MAIN STRING : ";
cin.getline(mainstr, 50);
cout<<"ENTER THE PATTERN STRING : ";
cin.getline(patstr, 50);
len1= strlen(mainstr);
len2= strlen(patstr);
flag=0;
for(count1=0; count1<len1; count1++)
{
for(count2=count1; count2<len1; )
{
if(mainstr[count2]!=patstr[0])
{
flag=1;
count2++;
}
else if(mainstr[count2]==patstr[0])
{
pos=count2;
count3=0;
while(mainstr[count2]==patstr[count3])
{
flag=0;
count2++;
count3++;
}
if(count3>=len2)
{
cout<<"\nPATTERN FOUND AT POSITION " << pos+1 << endl;
found=1;
break;
}
else
count3=0;
}
}
if ( found)
break;
}
if((flag!=0)||((flag==0)&&(count3<len2)))
cout<<"\nPATTERN NOT FOUND."<<endl;
getch();
}



/*OUTPUT
ENTER THE MAIN STRING : Rachit
ENTER THE PATTERN STRING : ach
PATTERN FOUND AT POSITION 2
*/


_____________http://sundcs.blogspot.com/____________




//PROGRAM TO CONCATENATE TWO STRINGS

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char choice;
do
{
clrscr();
char str1[25], str2[25], str3[25];
int count1, count2;
cout<<"ENTER THE FIRST STRING : ";
cin.getline(str1, 25);
cout<<"ENTER THE SECOND STRING : ";
cin.getline(str2, 25);
for(count1=0; str1[count1]!='\0'; count1++)
{
str3[count1]=str1[count1];
}
str3[count1]=' ';
count1++;
for(count2=0; str2[count2]!='\0'; count2++)
{
str3[count1+count2]=str2[count2];
}
str3[count1+count2]='\0';
cout<<"\nTHE NEW CONCATENATED STRING IS : \n\n";
cout<<str3;
cout<<"\n\nDO YOU WISH TO RE-EXECUTE THE PROGRAM?(Y/N): ";
cin>>choice;
}while(choice=='Y'||choice=='y');
}



/* OUTPUT
ENTER THE FIRST STRING : GOOD EVENING
ENTER THE SECOND STRING : MR. SATYANARAYAN
THE NEW CONCATENATED STRING IS :
GOOD EVENING MR. SATYANARAYAN
DO YOU WISH TO RE-EXECUTE THE PROGRAM?(Y/N): N
*/


_____________http://sundcs.blogspot.com/____________

No comments:

Post a Comment