0% found this document useful (0 votes)
39 views27 pages

Internal Ii Answer

1. The document contains 6 programming problems with sample inputs and outputs. The problems include programs to remove non-alphabet characters from a string, find the student with the highest marks, count elements in a linked list, implement a stack using a linked list, add elements to a queue, and construct and traverse a binary search tree. 2. Sample code is provided for each problem to demonstrate how to programmatically solve the problem in C or C++ using concepts like classes, structures, linked lists, stacks, queues and binary trees. 3. The code examples show how to set up the data structures, write functions for operations like insertion, deletion, traversal and display, and include sample inputs and outputs for each

Uploaded by

4si21ec410
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)
39 views27 pages

Internal Ii Answer

1. The document contains 6 programming problems with sample inputs and outputs. The problems include programs to remove non-alphabet characters from a string, find the student with the highest marks, count elements in a linked list, implement a stack using a linked list, add elements to a queue, and construct and traverse a binary search tree. 2. Sample code is provided for each problem to demonstrate how to programmatically solve the problem in C or C++ using concepts like classes, structures, linked lists, stacks, queues and binary trees. 3. The code examples show how to set up the data structures, write functions for operations like insertion, deletion, traversal and display, and include sample inputs and outputs for each

Uploaded by

4si21ec410
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/ 27

INTERNAL II

1. Write a C++ program to remove all the characters from the given string that are not alphabet.
INPUT

pro$#&gra7m

OUTPUT
Program

PROGRAM
#include<stdio.h>

int main()

char s[100];

scanf("%s",&s);

char res[100];int x=0;

for(int i=0;i<strlen(s);i++){

if((s[i]>='a'&& s[i]<='z') || (s[i]>='A'&& s[i]<='Z'))

res[x]=s[i]; x++;

}
}

printf("%s",res);

2. The principal of the college wants to know who got the highest marks this semester. He has to
appreciate the students and also wants to offer a free course to that student. The topmost
company HR person will conduct the course. He or she can improve technical skills through this
course. The duration of the free course is about 3months. In this way, he wants to bring all other
students to get the highest marks. The class teachers of each and every class discussed and
submit the student's detail to the principal in the paper. Unfortunately, he missed the paper so he
needs to automate the process. Can you help him to find the student's details by developing the
code in the OOPS-c++ program.
INPUT

Kohli

24

480

OUTPUT

Enter student's basic information:

Name:

Age:

Gender:

Enter student's result information:

Total Marks :

Grade:
Name: kohli

Age: 24

Gender: M

Total Marks: 480

Percentage: 96

Grade: A

PROGRAM

#include <iostream>

using namespace std;

class Student

public:

string name,gender,grade;

int age,tmarks;

void set(string n,int x, string g,int y,string r)

cout<<"Enter student's basic information:"<<endl;

cout<<"Name:"<<endl;

cout<<"Age:"<<endl;

cout<<"Gender:"<<endl;

cin>>n>>x>>g;
cout<<endl<<"Enter student's result information:"<<endl;

cout<<"Total Marks:"<<endl;

cout<<"Grade:"<<endl;

cin>>y>>r;

name=n;

age=x;

gender=g;

tmarks= y;

grade = r;

};

class Marks : public Student

public:

void display()

int per=tmarks/5;

cout<<"Name: "<<name<<endl;

cout<<"Age: "<<age<<endl;

cout<<"Gender: "<<gender<<endl;

cout<<"Total Marks: "<<tmarks<<endl;

cout<<"Percentage: "<<per<<endl;

cout<<"Grade: "<<grade<<endl;
}

};

int main()

Marks m;

string n,g,r;

int x,y;

m.set(n,x,g,y,r);

m.display();

return 0;

3. Write a C program to count the number of elements present in a linked list.


INPUT & OUTPUT FORMAT: Input consists of a list of integers. -1 indicates the end of the
linked list. Refer to sample input & output for output formatting specifications.
INPUT
4

-1

OUPUT

4321

The number of elements are 4.


PROGRAM

#include <iostream>

using namespace std;

struct node

int data;

struct node *next;

}*head=0;

struct node *newnode,*temp;

void push(int n)

newnode=(struct node*)malloc(sizeof(struct node));

newnode->data=n;

newnode->next=0;

if(head==0)

head=newnode;

temp=newnode;

else
{

temp->next=newnode;

temp=newnode;

void print()

temp=head;

while(temp!=0)

cout<<temp->data<<" ";

temp=temp->next;

}}

int main()

int n;

while(1)

cin>>n;

if(n!=-1)
push(n);

else

break;

print();

4. Consider implementing a stack using a Linked List. Create a structure struct node { int data ;
struct node * link ; } ; Write a program to implement push and pop operation on the stack and to
display the contents of the stack. Print the message “Stack is empty” in the pop function and
return the value -1000 when an attempt is made to pop data from an empty stack. Refer to the
function specifications for further details. FUNCTION DEFINITIONS: void push (struct node
**q, int num) void display (struct node * q) int pop (struct node ** q) INPUT & OUTPUT
FORMAT: Refer the sample input and output for formatting specifications. Note that the
statement "The contents of the stack are" is in the main function. In the display function, if the
stack is empty, print “ {}”. [All text in bold corresponds to the input and the rest corresponds to
output].
Input (stdin)
1
3
1
5
1
6
3
2
3
2
2
2
3
6

OUTPUT
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
Enter the element to be pushed
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
Enter the element to be pushed
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
Enter the element to be pushed
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
The contents of the stack are 6 5 3
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
The popped element is 6
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
The contents of the stack are 5 3
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
The popped element is 5
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
The popped element is 3
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
Stack is empty
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
The contents of the stack are {}
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice

PROGRAM

#include<iostream>
#include<stdlib.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
Node *root = NULL;
void append(int d)
{
Node* newnode = new Node();
newnode->data = d;
newnode->next = root;
root = newnode;
//return;
}
void display()
{
Node *temp = root;
if(temp==NULL)
{

cout<<"The contents of the stack are {}";


}
else
{
cout<<"The contents of the stack are ";
while (temp != NULL)
{

cout<<temp->data<<" ";
temp = temp->next;
}
}
cout<<endl;
}
int delet()
{
if(root == NULL)
{
cout<<"Stack is empty\n";
return 0;
}
cout<<"The popped element is "<<root->data<<endl;
root = root->next;
return 1;
}
int main()
{
int n,a;
while(1)
{
cout<<"Choice 1 : Push\nChoice 2 : Pop\nChoice 3 : Display\nAny other choice : Exit\n";
cout<<"Enter your choice\n";
cin>>n;
if(n==1)
{
cout<<"Enter the element to be pushed\n";
cin>>a;
append(a);

}
else if(n==2)
delet();
else if(n==3)
display();
else
break;
}
}
5.Write a C program to add the elements into a queue and print the elements in the queue
(Implement this logic using the queue using linked list).
INPUT & OUTPUT FORMAT: Input consists of integers. -1 indicates the end. Refer to sample
input & output for output formatting specifications.
INPUT

a.1

b.2

c.3

d.-1
OUTPUT

PROGRAM

#include<iostream>

#include<stdlib.h>

using namespace std;

class Node{

public:

int data;

Node *next;

};

void append(Node **headadd,int data);

void display(Node *head);

int main()

Node *head=NULL;

int data;

do

{
cin>>data;

if(data>0)

append(&head,data);

}while(data>0);

display(head);

void append(Node **headadd,int data)

Node *temp,*newnode;

temp=*headadd;

newnode=(Node*)malloc(sizeof(Node));

newnode->data=data;

newnode->next=NULL;

if(*headadd==NULL)

*headadd=newnode;

else{

while(temp->next!=NULL){

temp=temp->next;

temp->next=newnode;

}
void display(Node *head)

while(head!=NULL)

cout<<head->data<<" ";

head = head->next;

6. Implement a C program to construct a Binary Search Tree and also display the elements in the
tree using Inorder, Preorder, and Postorder traversals. Binary Search tree is a binary tree in
which each internal node x stores an element such that the elements stored in the left subtree of x
are less than or equal to x and elements stored in the right subtree of x are greater than or equal
to x. This is called binary-search-tree property. INPUT & OUTPUT FORMAT: Refer the sample
input and output for formatting specifications. [All text in bold corresponds to the output and the
rest corresponds to input].

INPUT

yes

yes

yes

no

OUTPUT

Enter the element to be inserted in the tree

Do you want to insert another element?


Enter the element to be inserted in the tree

Do you want to insert another element?

Enter the element to be inserted in the tree

Do you want to insert another element?

Enter the element to be inserted in the tree

Do you want to insert another element?

Inorder Traversal: The elements in the tree are 1 2 3 4

Preorder Traversal: The elements in the tree are 1 2 3 4

PostorderTraversal: The elements in the tree are 4 3 2 1

PROGRAM

#include<iostream>

#include<string.h>

#include<stdlib.h>

using namespace std;

struct btree

int data;

struct btree *left;

struct btree *right;

};

struct btree *root,*temp;

void create(int n)

{
temp = (struct btree*)malloc(sizeof(struct btree));

temp->data = n;

temp->right = temp->right = NULL;

void insert(struct btree *t) // 5 8 9 4 6

if(t->data < temp->data && t->right != NULL)

insert(t->right);

else if(t->data < temp->data && t->right == NULL)

t->right = temp;

else if(t->data > temp->data && t->left != NULL)

insert(t->left);

else if(t->data > temp->data && t->left == NULL)

t->left = temp;

}
void inorder(struct btree *t)

if(root == NULL)

cout<<"No element";

if(t->left!=NULL)

inorder(t->left);

cout<<t->data<<" ";

if(t->right != NULL)

inorder(t->right);

void preorder(struct btree *t)

if(root == NULL)

cout<<"No element";

cout<<t->data<<" ";
if(t->left!=NULL)

preorder(t->left);

if(t->right != NULL)

preorder(t->right);

void postorder(struct btree * t)

if(root == NULL)

cout<<"No element";

return;

if(t->left!=NULL)

postorder(t->left);

if(t->right != NULL)

postorder(t->right);

cout<< t->data<<" ";

int main()

{
int n,input;

char s[10];

root = 0;

while(1)

cout<<"Enter the element to be inserted in the tree\n";

cin>>input;

create(input);

if(root == NULL)

root = temp;

else

insert(root);

cout<<"Do you want to insert another element?\n";

cin>>s;

if(strcmp(s,"no")==0)

break;

cout<<"Inorder Traversal: The elements in the tree are ";

inorder(root);

cout<<"\nPreorder Traversal: The elements in the tree are ";

preorder(root);

cout<<"\nPostorder Traversal: The elements in the tree are ";


postorder(root);

7. . Write a OOPS-C++ program to fine a given no is prime or not using class and object. If the
number is prime print"Prime" else, print "Not Prime" .
INPUT
a. 13
OUTPUT
Prime

PROGRAM

#include<iostream>
using namespace std;
class sum
{
private: int n,i,flag=0;
public:
void add()
{
cin>>n;
for (i = 2; i <= n / 2; ++i) {

// condition for non-prime


if (n % i == 0) {
flag = 1;
break;
}
}
if (n == 1) {
cout<<"1 is neither prime nor composite.";
}
else {
if (flag == 0)
cout<<"Prime";
else
cout<<"Not Prime";
}
}
};
int main()
{
sum s;
s.add();
}
8. Antakshari is a popular parlor game played in India. Many word games are similar to
antakshari. One such game is wordakshari. The game can be played by two or more people. The
first player has to recite a word. The last letter of the word is then used by the next player to
recite another word starting with that letter. The winner or winning team is decided by a process
of elimination. The person or the team that cannot come up with a word with the right consonant
is eliminated. The following rules need to be followed while playing this game. (ii) All other
words have to begin with the last letter of the previous word (iii) No words can be repeated.
write a program to print the wordakshari chain.

Sample Input:

architect

tailor

referee

electrician

nurse
blacksmith

####

Sample Output:

architect

tailor

referee

electrician

nurse

#include<stdio.h>

#include<string.h>

int main()

char s[50][50];

int i=0, n = 0;

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

{
gets(s[i]);

n++;

if(!strcmp(s[i],"####"))

break;

printf("%s\n",s[0]);

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

if(s[i][strlen(s[i])-1]==s[i+1][0])

printf("%s\n",s[i+1]);

return 0;
}

9. Write a C program to sort the given string. INPUT & OUTPUT FORMAT: Input consists of 1
string. Output print the ascending order of given string.

INPUT:

God

OUTPUT:

The sorted string is dgo

PROGRAM

#include<stdio.h>

int main()

//fill your code

char s[1000];

scanf("%s",&s);

int i=0,j=0,n=strlen(s);

char t;

for (i = 0; i < n-1; i++) {

for (j = i+1; j < n; j++) {

if (s[i] > s[j]) {

t= s[i];

s[i] = s[j];

s[j] = t;

}
}

printf("The sorted string is %s", s);

10. Write a C program to perform the binary search on an array using recursion.

SAMPLE INPUT:

SAMPLE OUTPUT:

PROGRAM

#include<stdio.h>
#include<math.h>
int recSearch ( int a[], int l, int r, int index)
{
if ( r < l)
return -1;
if ( a[l] == index)
return 1;
if (a[r] == index)
return 1;
return recSearch ( a, l+1, r-1, index);
}
int main()
{
int a[100];
int n;
scanf("%d",&n);
for ( int i = 0; i < n; i++ )
{
scanf("%d",&a[i]);
}
int x;
scanf("%d",&x);
int index = ( recSearch ( a , 0 , n-1 , x));
if ( index != -1)
printf("%d",x);
else
printf("The number is not present in the list");
}

You might also like