Input
Input
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;
gets(str);
for(int i=0;str[i]!='\0';i++)
cap++;
low++;
count++;
else
space++;
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();
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;
for(i=0;i<8;i++)
cin>>arr[i];
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();
}
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:
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();
}
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();
}
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 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 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:-