0% found this document useful (0 votes)
59 views26 pages

Input

The document provides code snippets for various C++ programs involving arrays, strings, pointers, files, and data structures like stacks and queues. Some key programs include: 1) A program that replaces all capital letters in a string with lowercase and vice versa. 2) A program that finds the sum of the main diagonal elements of a 2D array. 3) Menu driven programs to perform push, pop, and display operations on a stack and insert, delete, and display operations on a queue using array implementations.

Uploaded by

GUNEET SURA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views26 pages

Input

The document provides code snippets for various C++ programs involving arrays, strings, pointers, files, and data structures like stacks and queues. Some key programs include: 1) A program that replaces all capital letters in a string with lowercase and vice versa. 2) A program that finds the sum of the main diagonal elements of a 2D array. 3) Menu driven programs to perform push, pop, and display operations on a stack and insert, delete, and display operations on a queue using array implementations.

Uploaded by

GUNEET SURA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

1) Write a program to read a string and print it after replacing each of its

capital alphabets by the corresponding small alphabet and each small


alphabet by its corresponding capital alphabet.

Input:-
#include<constream.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
void main()
{ clrscr();
char str[10];
cout<<"\nenter a string: ";
gets(str);
for(int i=0;i<strlen(str);i++)
{ if(str[i]>='A'&&str[i]<='Z')
str[i]=tolower(str[i]);
else
str[i]=toupper(str[i]);
}
cout<<"\nupdated string: "<<str;
getch();
}

Output:-
3) Write a program to input elements in a 2D array and then display the sum
of main diagonal elements of this array.

Input:-
#include<constream.h>
void main()
{ clrscr();
int m[15][15];
int n,i,j,sum=0;
cout<<"enter no of rows and columns(max.15)";
cin>>n;
cout<<"\nenter a "<<n<<"x"<<n<<" 2D-array:\n";
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
{ cin>>m[i][j];
}
}

for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
{ if(i==j)
{ sum+=m[i][j];
}
}
}
cout<<"\nsum of main diagonal is: "<<sum;
getch();
}
Output:-

Q4. Write a program to read a string and print out the following :
i) No. of capital alphabets
ii) No. of small alphabets
iii) No. of non-alphabets

Input:-
#include<constream.h>

#include<stdio.h>

void main()

clrscr();

char str[50];

int count=0,cap=0,low=0,space=0;

cout<<"Enter a string: "<<endl;

gets(str);
for(int i=0;str[i]!='\0';i++)

if(str[i]>='A' && str[i]<='Z')

cap++;

else if(str[i]>='a' && str[i]<='z')

low++;

else if(str[i]>='0' && str[i]<='9')

count++;

else

space++;

cout<<"\nCapital letters: "<<cap;

cout<<"\nSmall letters: "<<low;

cout<<"\nNon Alphabets: "<<count;

cout<<"\nSpaces: "<<space;

getch();

Output for Q4
Q6) Write to create a text file to input roll no. and marks of ten student
and display them on screen after reading from text file using data file
handling.

Input:-
#include<fstream.h>
#include<conio.h>

void main()
{
clrscr();

int a[10]; float b[10];

for(int i=0; i<10; i++)


{
cout<<"\nEnter the roll no. of student "<<i+1<<": ";
cin>>a[i];
cout<<"\nEnter the marks of student "<<i+1<<": ";
cin>>b[i];
}

ofstream student;
student.open("stud.txt");
cout<<"\nStudent's details: ";
for(i=0;i<10;i++)
{
cout<<"\nMarks of roll no. "<<a[i]<<" = "<<b[i];
}

student.close();
getch();
}
Output for Q6

Q7) Write a function in C++ which accepts an integer array and its size
as arguments/parameter and exchange the value of first half side
element with the second half side element of the array.
Example:
If an array of eight element has initial contents as
2, 4, 1, 6, 7,9,23,10
The function should rearrange the array as
7, 9, 23, 10, 2, 4, 1, 6

Input:-
#include<constream.h>

void main()

clrscr();

int arr[8];

int i,j,temp;
int size=8;

cout<<"Enter the value for array(max 8): "<<endl;

for(i=0;i<8;i++)

cin>>arr[i];

cout<<"\nThe rearranged array is: "<<endl;

for(i=0,j=size/2;j<size;i++,j++)

temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

for(i=0;i<8;i++)

cout<<arr[i]<<" ";

getch();

}
9) Write a C++ program using pointer to find the smallest and largest
element in a dynamically created array.
Input:-
#include<constream.h>
void main()
{
clrscr();
int *a;
int no;
cout<<"Enter number of elements:"<<endl;
cin>>no;
a=new int[no];
cout<<"Enter elements:"<<endl;
for( int i=0;i<no;i++)
{
cin>>*(a+i);
}
int high=0;
int low=0;
high=*a;
low=*a;
for(i=0;i<no;i++)
{
if(low>*(a+i))
{
low=*(a+i);
}
}
for(i=0;i<no;i++)
{
if(high<*(a+i))
{
high=*(a+i);
a++;
}
}
cout<<"The highest number is:"<<high<<endl;
cout<<"The lowest number is:"<<low<<endl;
getch();
}

10) Write a Program using pointer to swap two integers.


Input:-
#include<constream.h>
void swap(int*x,int*y);
void main()
{ clrscr();
int a,b;
cout<<"\nenter two integers...";
cout<<"\nfirst integer: ";
cin>>a;
cout<<"\nsecond integer: ";
cin>>b;
cout<<"\noriginal values...";
cout<<"\na="<<a<<",b="<<b;
swap(&a,&b);
cout<<"\n\nswapped values...";
cout<<"\na="<<a<<",b="<<b;
getch();
}
void swap(int*x,int*y)
{ int temp;
temp=*x;
*x=*y;
*y=temp;
}

Output:-

13) Write a function in C++ which accepts an integer array and its size as
arguments/parameter and exchange the value of first half side element with
the second half side element of the array.
Example:
If an array of eight element has initial contents as
2, 4, 1, 6, 7,9,23,10
The function should rearrange the array as
7, 9, 23, 10, 2, 4, 1, 6

Input:-
#include<constream.h>
void rearrange( int a[], int size)
{
int i,j,temp;
for ( i=0,j=size/2;j<size;i++,j++)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
cout<<"The rearranged array is:"<<endl;
for( i=0;i<size;i++)
{
cout<<a[i]<<"\t";
}
}
void main()
{
clrscr();
int a[10], size;
cout<<"Enter size of array:"<<endl;
cin>>size;
cout<<"Enter array:"<<endl;
for( int i=0;i<size;i++)
{
cin>>a[i];
}
rearrange(a,size);
getch();
}

15) Write a function in C++ which accepts an integer array and its size as
arguments/parameters and assigns the elements into a two-dimensional
array of integers in the following format:

If the array is 1, 2, 3, 4, 5, 6 If the array is 1, 2, 3


The resultant 2-D array is: The resultant 2-D array is:
1 2 3 4 5 6 1 2 3
1 2 3 4 5 0 1 2 0
1 2 3 4 0 0 1 0 0
1 2 3 0 0 0
1 2 0 0 0 0
1 0 0 0 0 0

Input:-
#include <iostream>
using namespace std;
void func(int arr[6], int n)
{
int i,j,a[6][6];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=0;
}
}
for(i=n-1;i>=0;i--)
{
for(j=0;j<=i;j++)
{
a[i-j][j]=arr[j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j];
}
cout<<endl;
}
}
int main() {
int arr[6],n;
cout<<”Enter the size of array:”<<endl;
cin>>n;
cout<<”Enter the array:”<<endl;
for( int i=0;i<n;i++)
cin>>arr[i];
func(arr,n);
getch();
}
17) Write a menu driven program which allows the user to perform the
following operations on a stack (Array implementation):
1. Push
2. Pop
3. Display

Input:-

#include<constream.h>
#include<process.h>

const size=10;
int push(int s[],int&top);
int pop(int s[],int&top);
void display(int s[],int&top);
void main()
{ clrscr();
cout<<"\nARRAY REPRESENTATION OF STACK\n";
int stack[size];
int top=-1;
int choice;
char ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\n1.push \n2.pop \n3.display \n4.exit";
cout<<"\nenter your choice...";
cin>>choice;
switch(choice)
{ case 1:int x=push(stack,top);
if(x==1)
{ cout<<"\nsuccessful push operation";
display(stack,top);
}
else
cout<<"\nunsuccessful push operation";
break;
case 2:x=pop(stack,top);
if(x==1)
{ cout<<"\nsuccessful pop operation";
display(stack,top);
}
else
cout<<"\nunsuccessful pop operation";
break;
case 3:cout<<"\nresultant stack is:\n";
display(stack,top);
break;
case 4:cout<<"\nabortimg program...";
exit(0);
break;
default:cout<<"\nincorrect choice entered...";
}
cout<<"\ndo you wish to comtinue more operation on stack (Y/N): ";
cin>>ch;
}
getch();
}

int push(int s[],int&top)


{ if(top<size-1)
{ int item;
cout<<"\nenter item to be pushed: ";
cin>>item;
top++;
s[top]=item;
return 1;
}
else
{ cout<<"\nstack overflow..";
cout<<"\ndata cannot be pushed..";
return 0;
}
}
int pop(int s[],int&top)
{ if(top>=0)
{ cout<<"\nvalue popped is: "<<s[top];
top--;
return 1;
}
else
{ cout<<"\nempty stack, value cannot be popped..";
return 0;
}
}
void display(int s[],int&top)
{ cout<<s[top];
for(int i=top-1;i>=0;i--)
cout<<s[i]<<"<--";
}

Output:-
18) Write a menu driven program which allows the user to perform the
following operations on a queue (Array implementation):
1. Insert
2. Delete
3. Display

Input:-

#include<constream.h>
#include<process.h>

const size=10;
int q[size],front=-1,rear=-1;
int insert(int q[]);
int Delete(int q[]);
void display(int q[],int front,int rear);
void main()
{ clrscr();
cout<<"\nCIRCULAR ARRAY REPRESENTATION OF QUEUE\n";
int choice;
char ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\n***M*E*N*U***";
cout<<"\n1.Insert \n2.Delete \n3.Display \n4.Exit";
cout<<"\nenter your choice...";
cin>>choice;
switch(choice)
{ case 1:int x=insert(q);
if(x==0)
cout<<"\nsuccessful insertion.";
else
cout<<"\nunsuccessful insertion.";
break;
case 2:x=Delete(q);
if(x==0)
cout<<"\nsuccessful deletion";
else
cout<<"\nunsuccessful deletion";
break;
case 3:cout<<"\nresultant queue is:\n";
display(q,front,rear);
break;
case 4:cout<<"\nabortimg program...";
exit(0);
break;
default:cout<<"\nincorrect choice entered...";
}
cout<<"\n\ndo you wish to comtinue more operation on stack (Y/N): ";
cin>>ch;
}
getch();
}

int insert(int q[])


{ if(rear==size-1)
return(-1);
else if(rear==-1)
{ int item;
cout<<"\nenter item to be inserted: ";
cin>>item;
front=rear=0;
q[rear]=item;
return 0;
}
else
{ int item;
cout<<"\nenter item to be inserted: ";
cin>>item;
rear++;
q[rear]=item;
return 0;
}
}

int Delete(int q[])


{ if(front==-1)
return(-1);
else
{ if(front==rear)
front=rear=-1;
else
front++;
}
return 0;
}

void display(int q[],int front,int rear)


{ if(front==-1)
cout<<"\noverflow..";
else
{ for(int i=front;i<=rear;i++)
{ cout<<q[i]<<"\t";
}
cout<<"\nrear->"<<rear;
cout<<"\nfront->"<<front;
}
}

Output:-

19) Write a menu driven program which allows the user to perform the
following operations on a stack (Linked implementation):
1. Push
2. Pop
3. Display

Input:-

#include<constream.h>
#include<iomanip.h>
#include<string.h>
#include<stdio.h>
struct node
{ char name[20];
int age;
node *ptr;
};

class stack
{ node *x,*y,*temp;
public:
stack()
{ x=NULL;
}
void push()
{ if(x==NULL)
{ x=new node;
cout<<"\nenter name: ";
gets(x->name);
cout<<"enter age: ";
cin>>x->age;
x->ptr=NULL;
}
else
{ temp=new node;
cout<<"\nenter name: ";
gets(temp->name);
cout<<"enter age: ";
cin>>temp->age;
temp->ptr=x;
x=temp;
}
}
void pop()
{ if(x==NULL)
cout<<"stack is empty...";
else
{ x=temp;
x=x->ptr;
delete(temp);
}
}
void display()
{ if(x==NULL)
cout<<"empty list";
else
{ y=x;
while(y!=NULL)
{ cout<<"\n";
cout<<y->name<<"\t";
cout<<y->age<<"\n";
y=y->ptr;
}
}
}
};

int main()
{ clrscr();
stack s;
int choice;
do
{ cout<<"\n\nS T A C K M E N U ";
cout<<"\n1.Push \n2.Pop \n3.Display \n4.Exit";
cout<<"\nenter your choice: ";
cin>>choice;
switch(choice)
{ case 1:s.push();
break;
case 2:s.pop();
break;
case 3:s.display();
break;
case 4:cout<<"aborting program...";
break;
default:cout<<"wrong choice...";
}
}
while(choice!=4);
return 0;
}

Output:-

20) Write a menu driven program which allows the user to perform the
following functions on a queue (Linked implementation):
1. Insert
2. Delete
3. Display

Input:-

#include<constream.h>
#include<process.h>

struct node
{ int data;
node *next;
} *front=NULL,*rear=NULL;

void insert(int value);


void deletE();
void display();

void main()
{ clrscr();
int choice,value;
cout<<"\nQUEUE IMPLEMENTATION USING LINKED LIST";
while(1)
{ cout<<"\n\n**M*E*N*U**";
cout<<"\n1.Insert \n2.Delete \n3.Display \n4.Exit";
cout<<"\nenter your choice...";
cin>>choice;
switch(choice)
{ case 1:cout<<"\nenter value to be inserted: ";
cin>>value;
insert(value);
break;
case 2:deletE();
break;
case 3:display();
break;
case 4:cout<<"\nabortimg program...";
exit(0);
break;
default:cout<<"\nincorrect choice entered...";
}
}
}

void insert(int value)


{ node *ptr;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(front==NULL)
front=rear=new node;
else
{ rear->next=ptr;
rear=ptr;
}
cout<<"\ninsertion successful..";
}

void deletE()
{ if(front==rear)
cout<<"\nqueue is empty...";
else
{ node *temp=front;
front=front->next;
cout<<"\ndeleted element: "<<temp->data;
delete(temp);
}
}

void display()
{ if(front==NULL)
cout<<"\nqueue is empty...";
else
{ node *temp=front;
while(temp->next!=NULL)
{ cout<<temp->data<<"--->";
temp=temp->next;
}
cout<<temp->data;
}
}

Output:-

You might also like