Wednesday, 27 September 2017

Department of Computer Engineering has student's club named 'Pinnacle Club'. Students ofSecond, third and final year of department can be granted membership on request. Similarlyone may cancel the membership of club. First node is reserved for president of club and lastnode is reserved for secretary of club. Write C++ program to maintain club member‘sinformation using singly linked list. Store student PRN and Name. Write functions to

a) Add and delete the members as well as president or even secretary.
b) Compute total number of members of club
c) Display members
d) Display list in reverse order using recursion
e) Two linked lists exists for two divisions. Concatenate two lists.
--------------------------------------------------------------------------------------------------------------------------
#include<iostream>
#include<cstdlib>
#include<string.h>
//Author : Vivek S. Sharma
//Copyright : Your copyright notice
using namespace std;
struct node{
int prn;
char name[30];
struct node *next;
}*start;

class pinnacle{
public :
node *create(int ,char []);
void add_president();
void add_secretary();
void display();
void add_member();
void d_president();
void d_secretary();
void d_member();
void no_members();
void reverse();

};



int main(){
 int choice;
    pinnacle sl;
    start = NULL;
    while (1)   {
        cout<<"1.Add President"<<endl;
        cout<<"2.Add Secretary"<<endl;
        cout<<"3.Display"<<endl;
cout<<"4.Add_Member"<<endl;
cout<<"5.Delete President"<<endl;
cout<<"6.Delete Secretary"<<endl;
cout<<"7.Delete Member"<<endl;
cout<<"8.No of members"<<endl;
        cout<<"9. Reverse "<<endl;
cout<<"10.Exit "<<endl;
        cout<<"Enter your choice : ";
        cin>>choice;
        switch(choice){
        case 1:
            cout<<"Add President : "<<endl;
            sl.add_president();
            cout<<endl;
            break;
        case 2:
            cout<<"Add Secretary : "<<endl;
            sl.add_secretary();
            cout<<endl;
            break;
        case 3:
            cout<<"Display"<<endl;
            sl.display();
            cout<<endl;
            break;
case 4:
            cout<<"Add_Member "<<endl;
            sl.add_member();
            cout<<endl;
            break;
case 5:
            cout<<"DElete President "<<endl;
            sl.d_president();
            cout<<endl;
            break;
case 6:
            cout<<"Delete Secreatery "<<endl;
            sl.d_secretary();
            cout<<endl;
            break;
case 7:
            cout<<"Delete Member "<<endl;
            sl.d_member();
            cout<<endl;
            break;
case 8:
            cout<<"Total Members : "<<endl;
            sl.no_members();
            cout<<endl;
            break;
case 9:
            cout<<"Reverse Members : "<<endl;
            sl.reverse();
            cout<<endl;
            break;
case 10:
            cout<<"Concatenate Members : "<<endl;
           // sl.cancatenate();
            cout<<endl;
            break;
case 0 :cout<<"Exiting"<<endl;
exit(1);
        default:
            cout<<"Wrong choice"<<endl;
        }
    }
return 0;
}




node *pinnacle::create(int prn, char name[30]){
struct node *temp;
    temp = new(struct node);
    if (temp == NULL)
    {
        cout<<"Memory not allocated "<<endl;
        return 0;
    }
    else
    {
    strncpy(temp->name, name, 30);
        temp->prn = prn;
        temp->next = NULL;
        return temp;
}
}

void pinnacle:: add_president(){
int prn;
char name[30];
cout<<"Add President : \n Enter prn no. :"<<endl;
cin>>prn;
cout<<"Enter President name : "<<endl;
cin>>name;
struct node *temp, *p;
    temp = create(prn,name);
    if (start == NULL)
    {
        start = temp;
        start->next = NULL;
    }
    else
    {
        p = start;
        start = temp;
        start->next = p;
    }
    cout<<"President Inserted at beginning"<<endl;
}

void pinnacle:: add_secretary(){
int prn;
char name[30];
cout<<"Add Secretary : \n Enter prn no. :"<<endl;
cin>>prn;
cout<<"Enter Secretary name : "<<endl;
cin>>name;
  struct node *temp, *s;
    temp = create(prn,name);
    s = start;
    while (s->next != NULL)
    {
        s = s->next;
    }
    temp->next = NULL;
    s->next = temp;
    cout<<"Secretary Inserted"<<endl;
}


void pinnacle :: display(){
 struct node *temp;
    if (start == NULL)
    {
        cout<<"The List is Empty"<<endl;
        return;
    }
    temp = start;
    cout<<"Members of list are: "<<endl;
    while (temp != NULL)
    {
        cout<<"\n \n PRN NO.="<<temp->prn<<" and NAME : "<<(temp->name)<<" \n ";
        temp = temp->next;
    }
    cout<<"NULL"<<endl;
}


void pinnacle :: add_member(){
int prn,counter=0;
char name[30];
cout<<"Add Member : \n Enter prn no. :"<<endl;
cin>>prn;
cout<<"Enter Member name : "<<endl;
cin>>name;
  struct node *temp, *s, *ptr;
    temp = create(prn,name);
s=start;
 while (s != NULL)  {
        s = s->next;
        counter++;
   }
 if (start == NULL)
    {
        start = temp;
        start->next = NULL;
    }
else{
s=start;
   for (int i = 1; i < (counter) ; i++)
        {
            ptr = s;
            s = s->next;
        }
        ptr->next = temp;
        temp->next = s;

}}


void pinnacle :: d_president(){
struct node *s;
s = start;
start = s->next;
free(s);
}


void pinnacle :: d_secretary(){
struct node *s;
s=start;
while(s->next->next != NULL){
s=s->next;
}
free(s->next);
s->next=NULL;
}



void pinnacle :: d_member(){
 int pos, i, counter = 0;
    if (start == NULL){
        cout<<"List is empty"<<endl;
        return;
    }
    cout<<"Enter the position of member to be deleted: ";
    cin>>pos;
    struct node *s, *ptr;
    s = start;
    if (pos == 1)
    {
        start = s->next;
    }
    else
    {
        while (s != NULL)
        {
            s = s->next;
            counter++;
        }
        if (pos > 0 && pos <= counter)
        {
            s = start;
            for (i = 1;i < pos;i++)
            {
                ptr = s;
                s = s->next;
            }
            ptr->next = s->next;
        }
        else
        {
            cout<<"Position out of range"<<endl;
        }
        free(s);
        cout<<"Member Deleted"<<endl;
}
}


void pinnacle :: no_members(){
int count=0;
struct node *s;
s=start;
while(s != NULL){
s=s->next;
count++;
}
cout<<"Number of members in the group = "<<count<<endl;
}


void pinnacle :: reverse(){
struct node *ptr1, *ptr2, *ptr3;
    if (start == NULL){
        cout<<"List is empty"<<endl;
        return;
    }
    if (start->next == NULL)
    {
        return;
    }
    ptr1 = start;
    ptr2 = ptr1->next;
    ptr3 = ptr2->next;
    ptr1->next = NULL;
    ptr2->next = ptr1;
    while (ptr3 != NULL)
    {
        ptr1 = ptr2;
        ptr2 = ptr3;
        ptr3 = ptr3->next;
        ptr2->next = ptr1;
    }
    start = ptr2;
}


--------------------------------------------------------------------------------------------------------------------------


**If some mistakes, plz let me know!-----Vivek S. Sharma
The ticket booking system of Cinemax theater has to be implemented using C++ program. There are 10 rows and 7 seats in each row. Doubly circular linked list has to be maintained to keep track of free seats at rows. Assume some random booking to start with. Use array to store pointers (Head pointer) to each row. On demand
a) The list of available seats is to be displayed
b) The seats are to be booked
c) The booking can be cancelled.
--------------------------------------------------------------------------------------------------------------------------

#include<iostream>
using namespace std;

//Author : Vivek S. Sharma
//Copyright : Your copyright notice
struct node{
int seatc,seatr;
string status;
struct node *next ,*prev;
}*head[10],*last[10];

class ticket{
public:
ticket(){
for(int j=0 ; j<10 ; j++){
head[j]=last[j]=NULL;
struct node* temp;
for(int i=1 ; i<=7 ; i++){
temp=create_node(i,j+1);
if(head[j]==last[j] && head[j]==NULL){
head[j]=last[j]=temp;
head[j]->next=last[j]->next=NULL;
head[j]->prev=last[j]->prev=NULL;

}
else{
temp->next=head[j];
head[j]->prev=temp;
head[j]=temp;
head[j]->prev=last[j];
last[j]->next=head[j];
}
}
}
}

node* create_node(int x,int y){
struct node*temp;
temp=new(struct node);
if(temp==NULL){
cout<<"\nMemory not allocated";
return 0;
}
else{
temp->seatc=x;
temp->seatr=y;
temp->status="A";
temp->next=NULL;
temp->prev=NULL;
return temp;
}

}
void book(){
int x,y;
cout<<"\nEnter row and column";
cin>>x>>y;
struct node* temp;
temp=head[x-1];
for(int i=0 ; i<7 ; i++){
if(temp->seatc==y){
if(temp->status=="A"){
temp->status="B";
}
else{
cout<<"\nSORRY !! Already booked!!";
}
}
temp=temp->next;
}
display();
}

void cancel(){
int x,y;
cout<<"\nEnter row and column to cancel booking : ";
cin>>x>>y;
struct node* temp;
temp=head[x-1];
for(int i=0 ; i<7 ; i++){
if(temp->seatc==y){
if(temp->status=="B"){
temp->status="A";
}
else{
cout<<"\nSORRY !! Already unbooked!!";
}
}
temp=temp->next;
}
display();
}

void display(){
struct node* temp;
for(int j=0 ; j<10 ; j++){
temp=head[j];
for(int i=0 ; i<7 ; i++){
cout<<temp->seatr<<","<<temp->seatc;
cout<<""<<temp->status<<"\t";
temp=temp->next;
}
cout<<"\n";
}
}

};

int main(){
ticket t;
int ch;
t.display();
do{
cout<<"\n1.Book Ticket \n2.Cancel Booking  \n3.EXIT";
cin>>ch;
switch(ch){
case 1:t.book();break;
case 2:t.cancel();break;
}
}while(ch!=3);

return 0;
}

--------------------------------------------------------------------------------------------------------------------------


**If some mistakes, plz let me know!-----Vivek S. Sharma
An m x n matrix is said to have a saddle point if some entry a[i][j] is the smallest value inrow i and the largest value in j. Write C/ C++ function that determines the location of asaddle point if one exists.
--------------------------------------------------------------------------------------------------------------------------

#include<iostream>
using namespace std;
//Author : Vivek S. Sharma
//Copyright : Your copyright notice
class matrix{
int a[10][10];
int r,c;
public :
void get_data(){
cout<<"Enter no of rows and columns";
cin>>r>>c;
cout<<"\n Enter the elements \n";
for(int i=0 ; i<r ; i++){
for(int j=0 ; j<c ; j++){
cin>>a[i][j];
}
}
}

void display(){
cout<<"\n the matrix u entered is : \n";
for(int i=0 ; i<r ; i++){
for(int j=0 ; j<c ; j++){
cout<<" "<<a[i][j];
}
cout<<"\n";
}
}
void saddle(){
int x,y;
for(int i=0 ; i<r ; i++){
for(int j=0 ; j<c ; j++){
x=0,y=0;
for(int k=0 ; k<c ; k++){
if(a[i][j]<a[i][k] || j==k)
x++;
}
if(x==c){
for(int l=0 ; l<r ; l++){
if(a[i][j]>a[l][j] || i==l){
y++;
if(y==r)
{
cout<<"Yes, saddle point exists, and it is : "<<a[i][j];


}
else cout<<"\nSaddle point does not exist";
}

}
}
}
}
}
};
int main(){
matrix m;
m.get_data();
m.display();
m.saddle();
return 0;
}
--------------------------------------------------------------------------------------------------------------------------


**If some mistakes, plz let me know!-----Vivek S. Sharma

Set A=(1,3, a, s, t, i} represent alphanumeric characters permitted to be used to set the password of length 4. Write C/C++ program to generate all possible passwords.
--------------------------------------------------------------------------------------------------------------------------

#include<iostream>
using namespace std;
//Author : Vivek S. Sharma
//Copyright : Your copyright notice
class password{
char a[6]={'1','3', 'a','s','t','i'};
int n,count;
public:
password(){
n=6;
count=0;
}
void dispaly(){
for(int i=0 ; i<n ; i++){
for(int j=0 ; j<n ; j++){
for(int k=0 ; k<n ; k++){
for(int l=0 ; l<n ; l++){
cout<<"\n"<<a[i]<<a[j]<<a[k]<<a[l];
count++;
}
}
}
}
cout<<"\nNo. of combinatons : "<<count;
}
};
int main(){
password p;

p.dispaly();
return 0;
}
--------------------------------------------------------------------------------------------------------------------------


**If some mistakes, plz let me know!-----Vivek S. Sharma
A magic square is an n * n matrix of the integers 1 to n2 such that the sum of each row, column, and diagonal is the same. The figure given below is an example of magic square for case n=5. In this example, the common sum is 65. Write C/C++ Program for magic square.

--------------------------------------------------------------------------------------------------------------------------
#include<iostream>
using namespace std;
//Author : Vivek S. Sharma
//Copyright : Your copyright notice
class magic_square{
int a[10][10];
int n;
public:
magic_square(){
cout<<"\nEnter n : ";
cin>>n;
}
void get(){
cout<<"\nEnter the elements : ";
for(int i=0 ; i<n ; i++){
for(int j=0 ; j<n ; j++){
cin>>a[i][j];
}
}
}
bool row(){
int sum[10];
for(int i=0 ; i<n ; i++){
sum[i]=0;
}
for(int i=0 ; i<n ; i++){
for(int j=0 ; j<n ; j++){
sum[i]=sum[i]+a[i][j];
}
}
for(int i=0 ; i<n ; i++){
if(sum[0]!=sum[i])
return false;

}
return true;
}

bool column(){
int sum[10];
for(int i=0 ; i<n ; i++){
sum[i]=0;
}
for(int i=0 ; i<n ; i++){
for(int j=0 ; j<n ; j++){
sum[i]=sum[i]+a[j][i];
}
}
for(int i=0 ; i<n ; i++){
if(sum[0]!=sum[i])
return false;

}
return true;
}
bool diagnol(){
int d1,d2;
d1=d2=0;
for(int i=0 ; i<n ; i++){
for(int j=0 ; j<n ; j++){
if(i==j)
d1=d1+a[i][j];
if(i+j==n-1)
d2=d2+a[i][j];
}
}

if(d1!=d2)
return false;
return true;
}
};
int main(){
magic_square m;
m.get();
if(m.diagnol() && m.row() && m.column())
cout<<"\nIt is a magic matrix !!";
else
cout<<"\nSoory !! It is not a magic matrix";
return 0;
}


--------------------------------------------------------------------------------------------------------------------------


**If some mistakes, plz let me know!-----Vivek S. Sharma

Write C/C++ program to store marks scored for first test of subject 'Data Structures and Algorithms' for N students. Compute
I. The average score of class
ii. Highest score and lowest score of class
iii. Marks scored by most of the students
iv. list of students who were absent for the test

--------------------------------------------------------------------------------------------------------------------------
#include<iostream>
using namespace std;
//Author : Vivek S. Sharma
//Copyright : Your copyright notice
class Data_Structure{
int m[100],n;
public :
Data_Structure(){
n=0;
}
void get(){
cout<<"\nEnter no. of students : ";
cin>>n;
cout<<"\nEnter the Dsa marks(-1 for absent students) : ";
for(int i=0 ; i<n ; i++){
cout<<"\nMarks of Roll No. "<<i+1<<" : ";
cin>>m[i];
}
}
void repeated_marks(){
int a[100];
for(int i=0 ; i<100; i++){
a[i]=0;
}
for(int i=0 ; i<n ; i++){
for(int j=0 ; j<100 ; j++){
if( m[i]==j)
a[j]++;
}
}

int max=a[0],temp=0;
cout<<"\nmax="<<max;
for(int i=0 ; i<100; i++){

if(max<a[i]){
max=a[i];
temp=i;
}
}
cout<<"\nMarks scored by most of the students : "<<temp;
}
void averrage(){
int sum,avg;
sum=0,avg=0;
int x=n;
for(int i=0 ; i<n ; i++){
if(m[i] != -1){
sum+=m[i];
}
else if(m[i]==-1) x--;
}
avg=sum/x;
cout<<"\nAverage Marks : "<<avg;
}
void highest_lowest(){
int max,min;
max=min=m[0];
for(int i=0 ; i<n ; i++){
if(m[i]!=-1){
if(m[i]<min)
min=m[i];
if(m[i]>max)
max=m[i];
}}
cout<<"\nMaximum Marks : "<<max<<"\nMinimum Marks : "<<min;
}
void absent(){
cout<<"\nStudents absent for test are : ";
for(int i=0 ;i<n ; i++){
if(m[i]==-1)
cout<<"\nRoll No. : "<<i+1;
}
}

};
int main(){
Data_Structure d;
int ch;
do{
cout<<"\n1.Get Deatails \n2.Average \n3.Highest and lowest marks \n4.Marks scored by most students \n5.Absent Students \n6.EXIT";
cin>>ch;
switch(ch){
case 1:d.get();break;
case 2:d.averrage();break;
case 3:d.highest_lowest();break;
case 4:d.repeated_marks();break;
case 5:d.absent();break;

}
}while(ch!=6);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------


**If some mistakes, plz let me know!-----Vivek S. Sharma

Sunday, 24 September 2017

In Second year Computer Engineering class of M students, set A of students play cricket and set B of students play badminton. Write C/C++ program to find and display-
i. Set of students who play either cricket or badminton or both
ii. Set of students who play both cricket and badminton
iii. Set of students who play only cricket
iv. Set of students who play only badminton
v. Number of students who play neither cricket nor badminton
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include<iostream>
using namespace std;
//Author : Vivek S. Sharma
//Copyright : Your copyright notice
class Students{
int m,A[100],B[100],a,b,count;
public:
Students(){
m=a=b=count=0;
}

void getData(){
cout<<"\nEnter total no. of students :";
cin>>m;
cout<<"\nEnter no. of students who play cricket :";
cin>>a;
cout<<"\nEnter the roll nos : ";
for(int i=0 ; i<a ; i++){
cin>>A[i];
}
cout<<"\Enter no. of students who play badminton : ";
cin>>b;
cout<<"\nEnter the roll nos : ";
for(int i=0 ; i<b ; i++){
cin>>B[i];
}

}
void neither(){
cout<<"\nNo. of Students who play neither of the games are : ";
cout<<m-count;
}
void either(){
int flag =0;
cout<<"\nStudents who play either of the games : ";
for(int i=0 ; i<a ; i++){
cout<<"\n"<<A[i];
count++;
}
for(int i=0 ; i<b ; i++){
for(int j=0 ; j<a ; j++){
if(B[i]==A[j]){
flag=1;}
}
if(flag==0){
cout<<"\n"<<B[i];
count++;

}
flag=0;
}
}
void both(){
int flag=0;
cout<<"\nStudents who play both games are : ";

for(int i=0 ; i<a ; i++){
for(int j=0 ; j<b ; j++){
if(A[i] == B[j])
flag=1;
}
if(flag==1){
cout<<"\n"<<A[i];
flag=0;
}
}


}
void ony_Cricket(){
int flag=0;
cout<<"\nStudents who play only Cricket are : ";
for(int i=0 ; i<a ; i++){
for(int j=0 ; j<b ; j++){
if(A[i] == B[j]){
flag=1;
}

}
if(flag==0){
cout<<"\n"<<A[i];
}
flag=0;
}
}
void ony_Badminton(){
int flag=0;
cout<<"\nStudents who play only Badminton are : ";
for(int i=0 ; i<b ; i++){
for(int j=0 ; j<a ; j++){
if(B[i] == A[j]){
flag=1;
}

}
if(flag==0){
cout<<"\n"<<B[i];
}
flag=0;
}
}


};
int main(){
Students s;
int ch;
do{
cout<<"\n1.Get Data \n2.Only Badminton \n3.Only Cricket \n4.Both \n5.Either \n6.Neither \n7.EXIT";
cin>>ch;
switch(ch){
case 1:s.getData();break;
case 2:s.ony_Badminton();break;
case 3:s.ony_Cricket();break;
case 4:s.both();break;
case 5:s.either();break;
case 6:s.neither();break;

}
}while(ch!=7);


return 0;
}
--------------------------------------------------------------------------------------------------------------------------


**If some mistakes, plz let me know!-----Vivek S. Sharma