Cs File
Cs File
Cs File
#include <iostream.h>
#include <string.h>
void main()
{
char s;
cout<<"ENTER A STRING:-"<<endl;
cin>>s;
cout<<”ENTERED STRING IS:-”<<endl;
for (int i=0;i<s.length();i++)
{ if ('a'<=s[i] && s[i]<='z'){
s[i]=char(((int)s[i])-32);
}
if ('A'<=s[i] && s[i]<='Z')
{ s[i]=char(((int)s[i])+32);
}
}
cout<<"CONVERTED STRING:- "<<s<<endl;
return 0;}
OUTPUT:
2) A class STUDENT has 3 data members:
Name, Roll Number, Marks of 5 subjects, Stream
And member functions to input and display data. It also has a
functions to input and display data. It also has a function member to
assign stream on the basis of the table given below:
Average Marks Stream
96% or more Computer Science
91% -95% Electronics
86% -90% Mechanical
81% -85% Electrical
75% -80% Chemical
71% -75% Civil
Declare a structure STUDENT and define the member functions:
Write a function to a define a structure STUDENT and input the
marks of n (<=20) students and for each student allot the stream.
(Don’t use any array)
#include <iostream.h>
#include <string.h>
class student
{ char name[50];
int rno;
float marks[5];
char stream[25];
void assign()
{
float average=this->avg();
if(average>70&&average<76)
strncpy(stream,"Civil",25);
else if(average>75&&average<81)
strncpy(stream,"Chemical",25);
else if(average>80&&average<86)
strncpy(stream,"Electrical",25);
else if(average>85&&average<91)
strncpy(stream,"Mechanical",25);
else if(average>90&&average<96)
strncpy(stream,"Electronics",25);
public:
student() {};
student(student &r)
rno=r.rno;
for(int i=0;i<5;i++,marks[i]=r.marks[i]);
strncpy(stream,r.stream,25);
strncpy(name,r.name,50);
void input()
cin>>rno;
cin.ignore(1,'\n');
cout<<"Enter name\n";
cin.getline(name,50);
for(int i=0;i<5;i++,cin>>marks[i]);
assign();
void display()
cout<<"Name: "<<name<<endl;
cout<<"Stream: "<<stream<<endl;
}
void avg()
float average=0;
for(int i=0;i<5;i++,average+=marks[i]);
average/=5;
};
int main()
int n;
student *stud;
cin>>n;
stud=new student[n];
for(int i=0;i<n;i++)
stud[i].input();
}
for(int i=0;i<n;i++)
avg(stud.n);
stud[i].display();
return 0;
OUTPUT:
3) Write a program to input elements in a 2D array and then display
the sum of main diaginal elements of this array
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],m,n,sum=0;
cout<<"enter no of rows:";
cin>>m;
cout<<"enter no of columns:";
cin>>n;
cout<<"enter the matrix"<<"\n";
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"the sum of diagonal elements of the matrix is:";
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
if(i==j)
sum+=a[i][j];
}
cout<<sum;
getch();
}
OUTPUT:
4) Write a program to read a string and print out the following
1) No of capital letters
2) No of small letters
3) No of non-alphabets
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
void main()
{
clrscr();
char str[20];
cout<<" Enter string:";
gets(str);
for(int i=0;i<strlen(str);i++)
{
if(str[i]>='a' &&str[i]<='z')
{
cout<<" The capital alphabets are:"<<str[i]<<"\n";
}
else if(str[i]>='a' &&str[i]<='z')
{
cout<<" The small alphabets are:"<<str[i]<<"\n";
}
else if(str[i]!=isalpha(str[i]))
{
cout<<" The non-alphabets are:"<<str[i]<<"\n";
}
getch();
}
OUTPUT:
5) Write a program to create a text file (.txt) and display number of
words, alphabets, vowels and consonants present in the file using the
concept of data file handling.
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
void main()
{
clrscr();
char a[80];
int words=0, alpha=0, vowels=0, consonants=0;
ofstream string("str.txt");
cout<<"\n enter the string:"<<endl;
cin.getline(a,79);
cout<<"\n the string is:"<<a<<endl;
for(int i=0;i<strlen(a);i++)
{
if(a[i]==' ')
while(a[i]==' ')
{i++;
}
if(isalnum(a[i]))
{
while (a[i]!=' ')
{
i++;
}
words++;
}
}
cout<<"\n the number of words in the string are:"<<words<<endl;
cout<<"\n the alphabet in the string are:"<<endl;
for(i=0;i<strlen(a);i++)
{
if(isalpha(a[i]))
{
alpha++;
cout<<a[i];
}
}
cout<<"\n total number of alphabets in the string are:"<<alpha<<endl;
cout<<"\n vowels in the string are:";
for(i=0;i<strlen(a);i++)
{
if(a[i]=='a'||a[i]=='A'||a[i]=='e'||a[i]=='E'||a[i]=='i'|| a[i]=='I'||a[i]=='o'||
a[i]=='O'|| a[i]=='u'|| a[i]=='U')
{
vowels++;
cout<<a[i]<<" ";
}
}
cout<<"\n total number of vowels in the string are:"<<vowels<<endl;
cout<<"\n consonants in the string are:"<<endl;
for(i=0;i<strlen(a);i++)
{
if(a[i]!='a'&& a[i]!='A'&& a[i]!='e'&& a[i]!='E'&& a[i]!='i'&& a[i]!
='I'&& a[i]!='o'&& a[i]!='O'&& a[i]!='u'&& a[i]!='U')
{
consonants++;
cout<<a[i]<<" ";
}
}
cout<<"\n total number of consonants in the string
are:"<<consonants<<endl;
getch();
}
OUTPUT:
6) Write to create a text file to input roll no. And marks of ten
students and display them on screen after reading from text using
data file handling.
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
void main()
{
clrscr();
int a[10];
float b[10];
for(int i=0;i<10;i++)
{
cout<<"\n enter the roll number of the student"<<i+1<<":";
cin>>a[i];
cout<<"\n enter the marks of the student"<<i+1<<"=";
cin>>b[i];
}
ofstream student;
student.open("stud.txt");
for(i=0;i<10;i++)
{
cout<<"\n marks of roll no."<<a[i]<<"="<<b[i];
}
student.close();
i=0;
cout<<"\n student details are:"<<endl;
char str[80][80];
ifstream stude;
stude.open("stud.txt,ios::in");
stude.seekg(0);
while(!stude.eof())
{
stude.getline(str[i],80);
cout<<str[i]<<"\n";
i++;
}
getch();
}
OUTPUT:
7) Write a function in c++ which accepts an integer array and its
size as arguments/parameters and exchanges the values of first half
side elements with the second half side elements of the array. Eg: If
an array of 8 elements 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
#include <iostream>
#include<conio.h>
#include<stdio.h>
int main()
{
int arr[100],size;
for(int i=0;i<size;i++)
{
cin>>arr[i];
}
//now calling the interchange functions
inter_change(arr,size);
cout<<"\t"<<arr[i];
}
getch();
}
//end of main function
int temp;
for(int i=0,j=(size/2);i<(size/2);i++,j++)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
OUTPUT:
#include <iostream>
#include<conio.h>
void main()
{
int *p;
int x;
cout << "Enter a number of elements u want to enter: ";
cin >> x;
p = new int[x];
}
}
for (int i = 0; i < x; i++)
{
if (max < *(p+i))
{
max = *(p+i);//changed
}
}
}
Cout<< largest value is<<max;
Cout<<smallest value is<<min;
}
Output:
#include<iostream.h>
#include<conio.h>
Void main( )
{
Clrscr();
Void swap(int*x,int*y);
{
Cout<<”enter values of a and b:”
Cin>>a>>b;
Cout<<”original values are:”;
Cout<<”\n a=”<<a;
Cout<<”\n b=”<<b;
Swap(a,b);
Cout<<”swapped values are:”;
Cout<<”\n a=”<<a;
Cout<<”\n b=”<<b;
}
Void swap(int*x,int*y)
{
Int temp;
temp=*x;
*x=*y;
*y=temp;
}
Output
Enter the values of a and b:12 45
Original values are:
a= 12
b=45
Swapped values are:
a=45
b=12
11) Write a menu driven program which allows the user to perform
the following operations on a one dimensional array
Insertion, deletion, searching, sorting and display.
The program should automatically perform binary search if the
array is sorted and linear search otherwise.
#include<iostream.h>
#include<conio.h>
class array
{
int m,n,p,val,i,j,key,pos,temp,a[20];
public:
void insert();
void del();
void search();
void sort();
void display();
}
};
void array:: insert()
{
cout<<” enter the position for the new element:”;
cin>>pos;
cout<<”enter the element to be inserted:”;
cin>>val;
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=val;
n=n+1;
}
void array::del()
{
cout<<”enter the position of the element to be deleted:\t";
cin>>pos;
val=a[pos];
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
cout<<”the deleted element is “<<val”;
}
void array::search();
{
cout<<”Enter the element to be searched:\t";
cin>>key;
for(i=0;i<n;i++)
{
if(a[i]==key)
{
cout<<nThe element is present at position <<i;
break;
}
}
if(i==n)
{
cout<<”The search is unsuccessful";}
void array::sort()
{
for(i=0;i<n;i++)
[
cout<<””enter the elements of the array:”;
cin>>a[i];
}
{
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<”After sorting the array elements are:\n"<<a[i];
}
void array::display()
{
cout<<”the deleted element is<<val;
cout<< “The element is present at position “<<i;
for(i=0;i<n;i+)
{
cout<<”After sorting the array elements are:\n"<<a[i];
}
}
void main()
{
array a;
do
{
cout<<”menu”<<endl;
cout<<”1.insert”;
cout<<”2.delete”:
cout<<”3.search”;
cout<<”4.sort”;
cout<<”5.display”;
cout<<”6.exit”;
cout<<”enter your choice:”;
cin>>choice;
switch(choice)
{ case1: a.insert();
Break;
case2: a.del();
break;
case3: a.search();
break;
case4: a.sort();
break;
case5: a.display();
break;
case6: cout<<”exit”;
break;
}
} while(choice!=6);
cout<<”to do search in an array:”;
int b[20],i,num,c=0,post,last,search,middle,m;
for(i=0;i<m;i++)
{
cout<<”enter elements of an array:”;
cin>>b[i]; }
if(b[i]>b[i+1]||b[i+1]>b[i])
{
cout<<”do binary search:”;
cout<<"Enter a number to find :";
cin>>search;
first = 0;
last = m-1;
middle = (first+last)/2;
while (first <= last)
{
if(a[middle] < search)
{
first = middle + 1;
}
else if(a[middle] == search)
{
cout<<search<<" found at location "<<middle+1<<"\
n";
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<"Not found! "<<search<<" is not present in the list.";
}
}
else
{
cout<<”do linear search:”;
cout<<"Enter the number to be searched : ";
cin>>num;
for(i=0; i<m; i++)
{
if(a[i]==num)
{
c=1;
post=i+1;
break;
}
}
if(c==0)
{
cout<<"Number not found..!!";
}
else
{
cout<<num<<" found at position "<<post;
}
getch();
}
OUTPUT
12) Write a program to input data in two arrays. Sort one of the
arrays in ascending and the other in descending order. Then merge
them into a third array so that the data in the third array is in
ascending order. The program should then display the data from all
the three arrays
#include<iostream.h>
#include<conio.h>
void Merge(int[],int,int[],int,int[]);
void main()
{
clrscr();
int arr1[50], arr2[50],arr3[50], N,M,NM=0, i, j;
cout<<"Enter Array 1 Size : "<<endl;
cin>>M;
cout<<”enter array elements:”;
for(i=0; i<M; i++)
{
cin>>arr[i];
}
for(i=0; i<M; i++)
{
for(j=i+1; j<M; j++)
{
if(arr1[i]>arr1[j])
{
temp=arr1[i];
arr1[i]=arr1[j];
arr1[j]=temp;
}
}
}
cout<<"array 1 after sorting:\n";
for(i=0; i<M; i++)
{
cout<<arr1[i]<<" ";
}
cout<<"Enter Array 2 Size : ";
cin>>N;
cout<<”enter elements of array 2:”;
for(i=0; i<N; i++)
{
cin>>arr2[i];
}
for(i=0; i<N; i++)
{
for(j=i+1; j<N; j++)
{
if(arr2[i]<arr2[j])
{
temp=arr2[i];
arr2[i]=arr2[j];
arr2[j]=temp;
}
}
}
cout<<" Array 2 after sorting is :\n";
for(i=0; i<N; i++)
{
cout<<arr2[i]<<" ";
}
MN=M+N;
Merge(arr1[],M,arr2[],N,arr3[]);
Cout<<the merged array is shown below:”;
for(i=0;i<MN;i++)
cout<<”arr3[i]<<” “;
cout<<”the elements of array 1 are:”<<arr1[i]<<endl;
cout<<”the elements of array 2 are:”<<arr2[i]<<endl;
cout<<”the merged array is:”<<arr3[i];
}
void Merge(int arr1[], int M, int arr2[], int N, int arr3[])
{
int a,b,c;
for(a=0,b=-1,c=0;a<M&&b>=0;)
{
If{arr1[a]<=arr2[b]) arr3[c++}=arr1[a++};
else arr3[c++]=arr2[b--];
if(a<M)
{ while(a<M)
arr3[c++]=arr1[a++];
else
{ while(b>=0)
arr3[c++]=arr2[b--];
}
}
OUTPUT:
13) Write a function in c++ which accepts an integer array and its
size as arguments/parameters and exchanges the values of first half
side elements with the second half side elements of the array.
Eg: If an array of 8 elements 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
#include <iostream>
#include<conio.h>
#include<stdio.h>
int main()
{
int arr[100],size;
for(int i=0;i<size;i++)
{
cin>>arr[i];
}
//now calling the interchange function
inter_change(arr,size);
cout<<"\t"<<arr[i];
}
getch();
}
//end of main function
int temp;
for(int i=0,j=(size/2);i<(size/2);i++,j++)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
OUTPUT:
14)
Write a program in c++ to find and display the sum of each row and
each column of a 2-Dimensional array of type float. Use the array
and its size as parameters with float as its return type
#include <iostream>
#include<conio.h>
int main()
{
float arr[100][100],rows,cols;
cout<<"\n\t\t Please enter the number of rows you want in your 2-D
array(MAX 100):";
cin>>rows;
cout<<endl;
cout<<"\n\t\t Please enter the number of columns you want int your 2-
D array(MAX 100):";
cin>>cols;
cout<<endl;
//now calling the function which will calculate column and row sum
respectively
cout<<"\n\t\t The sum of elements of respective rows:";
row_sum(arr,rows,cols);
cout<<endl;
return sum_row;
}
//end of function definition for row_sum
15) Program to accept integer array and its size as an argument and
assigns elements in 2 dimensional array.
#include<iostream.h>
#include<conio.h>
void display(int a[],int n)
{
int b[10][10];
int k1=0,k2=n;
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(k1<k2)
{
b[i][j]=a[k1];
k1++;
}
else
b[i][j]=0;
}
k1=0;
k2--;
}
cou<<"Elements of 2D array is as follows\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<b[i][j]<<"\t";
}
cout<<"\n";
}
}
int main()
{
int n;
clrscr();
cout<<"Enter size of array";
cin>>n;
cout<<"Enter "<<n<<"elements one by one\n";
int a[10];
for(int i=0;i<n;i++)
cin>>a[i];
display(a,n);
getch();
return 0;
}
Output:
Output:
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
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#define MAX 5
typedef struct stack
{
int data[MAX];
int top;
}
stack;
void init(stack*);
int isemp(stack *s);
int isful(stack *s);
void push(stack*,int);
void pop(stack*);
void disp(stack*);
int main()
{
clrscr();
stack s;
int chc;
int n,x;
init(&s);
do
{
cout<<"\n Select your option :";
cout<<"\n1.Push";
cout<<"\n2.Pop";
cout<<"\n3.Display";
cout<<"\n4.Exit";
cout<<"\n Enter the choice:";
cin>>chc;
switch(chc)
{
case 1:
cout<<"\n Enter element to push";
cin>>n;
push(&s,n);
getch();
break;
case 2:
pop(&s);
getch();
break;
case 3:
disp(&s);
getch();
break;
case 4:break;
default:cout<<"\n Invalid Option.";
break;
}
}
while(chc!=4);
return 0;
}
void int(stack *s)
{
s->top=-1;
}
int isemp(stack *s)
{if(s->top==-1)
return 1;
else return 0;
}
int isful(stack *s)
{if(s->top==4)
return 1;
else
return 0;
}
void push(stack *s,int n)
{
if(isful(s))
cout<<"\n Stack full";
else
{
s->top=s->top+1;
s->data[s->top]=n;
cout<<"\n Added to Stack "<<n;
}
}
void pop(stack *s)
{
if(isemp(s))
cout<<"\n The stack is empty";
else
{
int x=s->data[s->top];
s->top=s->top-1;
cout<<"\n popped from stack"<<x;
}
}
void disp(stack *s)
{
int i;
if(isemp(s))
cout<<"\n Stack empty";
else
{
cout<<"\n Stack:";
for(i=0;i<=s->top;i++)
cout<<" "<<s->data[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
#include<iostream.h>
#include<conio.h>
const int maxsize=10;
int insert(int q[],int &front,int &rear,int &count)
{
if(count!=maxsize)
{
int item;
cout<<" Enter value to be inserted :"<<endl;
cin>>item;
if(count==0)
front=rear=0;
else
if(rear!=maxsize-1)
rear++;
else
rear=0;
q[rear]=item;
count++;
return 1;
}
else
{
cout<<" Queue overflow "<<endl;
return 0;
}
}
int del(int q[],int &front,int &rear,int &count)
{
if(count!=0)
{
cout<<" Value deleted is :"<<q[front]<<endl;
if(count==1)
front=rear=-1;
else
if(front!=maxsize-1)
front++;
count--;
return 1;
}
else
{
cout<<" Empty Queue "<<endl;
return 0;
}
}
void main()
{
clrscr();
int q[10],choice;
cout<<"Array implementation of queue "<<endl;
int front ,rear,count=0;
front=rear=-1;
cout<<"Menu:";
cout<<"\n1.Insert";
cout<<"\n2.Delete";
cout<<"\n3.Exit";
do
{
cout<<"\n Enter the choice";
cin>>choice;
int x;
switch(choice)
{
case 1:insert(q,front,rear,count);
if(x=1)
{
cout<<"Succesful operation : "<<endl;
}
else
break;
case 2:del(q,front,rear,count);
if(x=1)
{
cout<<"Succesful operation : "<<endl;
}
else
break;
case 3: break;
}
}
while(choice!=3);
getch();
}
OUTPUT:
19) Write a menu driven program which allows the user to perform
the following operations on a stack (Linked implementation):
Push
Pop
Display
class stack
{
node *x,*y,*temp;
public:
stack() // constructor to initialize variable
{
x= NULL;
}
void push(); // function to add element
void pop(); // function to delete element
void display(); // function to display stack element
};
void stack::push()
{
if(x==NULL)
{
x = new node;
cout<<"\n Enter value :";
cin>>x->info;
x->ptr = NULL;
}
else
{
temp = new node ;
cout<<"\n Enter value :";
cin>>temp->info;
temp->ptr = x;
x = temp;
}
return;
}
void stack::pop(void)
{
if(x==NULL)
{
cout<<"\n Stack is empty";
getch();
}
else
{
temp =x;
x = x->ptr;
delete(temp);
}
return;
}
void stack::display()
{
if(x==NULL)
cout<<"\n Link list empty";
else
{
y = x;
while(y!=NULL)
{
cout<<setw(6)<<y->info;
y = y->ptr;
}
}
return;
}
int main()
{
stack S;
int choice;
do
{
clrscr();
cout<<"\n S T A C K M E N U ";
cout<<"\n 1. Push";
cout<<"\n 2. Pop";
cout<<"\n 3. Display ";
cout<<"\n 4. Exit";
cout<<"\n\n\n Enter your choice :";
cin>>choice;
switch(choice)
{
case 1: S.push();
break;
case 2: S.pop();
break;
case 3: S.display();
getch();
break;
case 4: break;
default:
cout<<"\n Wrong Choice.... Try again";
getch();
}
}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):
Insert
Delete
Display
#include<iostream.h>
#include<conio.h>
#include<process.h> //header file for exist function;
struct Node
{
int data;
struct Node *next;
}*front = NULL,*rear = NULL;
void insert(int);
voiddelet();
void display();
void main()
{
int choice, value;
clrscr();
cout<<"\n:: Queue Implementation using Linked List ::\n";
while(1){
cout<<"\n****** MENU ******\n";
cout<<"1. Insert\n2. Delete\n3. Display\n4. Exit\n";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice){
case 1: cout<<"Enter the value to be insert: ";
cin>>value;
insert(value);
break;
case 2: delet(); break;
case 3: display(); break;
case 4: exit(0);
default: cout<<"\nWrong selection!!! Please try again!!!\n";
}
}
}
void insert(int value)
{
struct Node *newNode;
newNode = new Node;
newNode->data = value;
newNode -> next = NULL;
if(front == NULL)
front = rear = newNode;
else
{
rear -> next = newNode;
rear = newNode;
}
cout<<"\nInsertion is Success!!!\n";
}
voiddelet()
{
if(front == NULL)
cout<<"\nQueue is Empty!!!\n";
else{
struct Node *temp = front;
front = front -> next;
cout<<"\nDeleted element: \n"<< temp->data;
delete temp;
}
}
void display()
{
if(front == NULL)
cout<<"\nQueue is Empty!!!\n";
else{
Node *temp = front;
while(temp->next != NULL){
cout<<temp->data<<"---->";
temp = temp -> next;
}
cout<<temp->data;
}
}
OUTPUT: