You are on page 1of 14

K L E F, Guntur

FED: BES-1: AY 2020-21: I year -B.Tech - Odd Sem : Semester in Exam-II (Batch -I)

20SC1101 - Computational Thinking for Design

Scheme of Evaluation

1) Predict the output of the following (4.5 Marks)

#include <stdio.h>
int main()
{
int x[7]={8,4,6,3};
int *p=x+2;
printf("a=%d\tb=%d\tc=%d\n",*(2+x), *(p-2), *(p+2)+2);
p-=2;
printf("d=%d\te=%d\tf=%d\n",*x-2, *p+1,p[2]);
return 0;
}

->2 Marks
Output:
a=6 b=8 c=2
d=6 e=4 f=6 ->2.5 Marks

2) Develop a C function that accepts to search for a given character in the given string. (4.5
Marks)
void search(char *,char);
#include <stdio.h>
int main()
{
char s[1000],c;
printf("Enter a string :");
scanf("%s",s);
printf("\n Enter a character to search :"); ->2.5 Marks
scanf(" %c",&c);
search(s,c);
return 0;
}

void search(char *s, char c)


{
int i, f=0;
for(i=0;s[i]!='\0';i++)
{
if(s[i]==c)
{
printf("Character is found at position %d",i+1);
f=1; ->2 Marks
break;
}
}
if(f==0)
printf("Desired Character is not found");
}

3) Develop a recursive function that prints maximum value in an array that stores integer
values?(8 Marks)

#include <stdio.h>
    int large(int[], int, int);
 
    int main()
  {
         int size, largest, list[20], i;
         printf("Enter size of the list:");
        scanf("%d", &size);
 
  
        for (i = 0; i < size ; i++)
    {
                scanf("%d \t", &list[i]);
    }
 
   
            largest = list[0];
            largest = large(list, size - 1, largest);
            printf("\nThe largest number in the list is: %d\n", largest);
 
  }
 
    int large(int list[], int position, int largest)                             ------>  2M (function definition)
  {
 
        if (position == 0)                                       -------->2M (termination condition)
            return largest;
 
        if (position > 0)                                     ----------->2 (condition)
    {
            if (list[position] > largest)
      {
                largest = list[position];
      }
            return large(list, position - 1, largest);                        ----->2(recursive call)
    }
 
  }
4) Develop a program to calculate XA+YB where A and B are the matrices and X=2 and
Y=3. (8 Marks)

#include <stdio.h>
int main()
{
int x=2,y=3,a[10][10],b[10][10],c[10][10];
int r1,r2,c1,c2,i,j;
printf("enter rows and cols for first matrix");
scanf("%d%d",&r1,&c1);
printf("enter rows and cols for second matrix");
scanf("%d%d",&r2,&c2);
printf("enter values for first matrix");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("enter values for second matrix");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++) ->3 Marks
scanf("%d",&b[i][j]);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
a[i][j]=x*a[i][j];
}
}
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
b[i][j]=y*b[i][j]; ->3 Marks
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("resultant matrix is");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("\t%d",c[i][j]); ->2 Marks
}
printf("\n");
}

return 0;
}

5) Demonstrate the bubble sort with all passes by using following list of elements
79,86,32,43,65,92,64,10,1,72b. Develop a C function to implement bubble sort. (12.5 Marks)

Answer:
Input 79 86 32 43 65 92 64 10 1 72

79 86 32 43 65 92 64 10 1 72
79 32 86 43 65 92 64 10 1 72
79 32 43 86 65 92 64 10 1 72
79 32 43 65 86 92 64 10 1 72
Pass - I 79 32 43 65 86 92 64 10 1 72
79 32 43 65 86 64 92 10 1 72
79 32 43 65 86 64 10 92 1 72
79 32 43 65 86 64 10 1 92 72
79 32 43 65 86 64 10 1 72 92
32 79 43 65 86 64 10 1 72 92
32 43 79 65 86 64 10 1 72 92
32 43 65 79 86 64 10 1 72 92
32 43 65 79 86 64 10 1 72 92
Pass - II
32 43 65 79 64 86 10 1 72 92
32 43 65 79 64 10 86 1 72 92
32 43 65 79 64 10 1 86 72 92
32 43 65 79 64 10 1 72 86 92

32 43 65 79 64 10 1 72 86 92
32 43 65 79 64 10 1 72 86 92
32 43 65 79 64 10 1 72 86 92
Pass-III 32 43 65 64 79 10 1 72 86 92
32 43 65 64 10 79 1 72 86 92
32 43 65 64 10 1 79 72 86 92
32 43 65 64 10 1 72 79 86 92

32 43 65 64 10 1 72 79 86 92
32 43 65 64 10 1 72 79 86 92
32 43 64 65 10 1 72 79 86 92
Pass-IV
32 43 64 10 65 1 72 79 86 92
32 43 64 10 1 65 72 79 86 92
32 43 64 10 1 65 72 79 86 92

32 43 64 10 1 65 72 79 86 92
32 43 64 10 1 65 72 79 86 92
Pass-V 32 43 10 64 1 65 72 79 86 92
32 43 10 1 64 65 72 79 86 92
32 43 10 1 64 65 72 79 86 92

32 43 10 1 64 65 72 79 86 92
32 10 43 1 64 65 72 79 86 92
Pass - VI
32 10 1 43 64 65 72 79 86 92
32 10 1 43 64 65 72 79 86 92

10 32 1 43 64 65 72 79 86 92
Pass - VII 10 1 32 43 64 65 72 79 86 92
10 1 32 43 64 65 72 79 86 92

1 10 32 43 64 65 72 79 86 92
Pass-VIII
1 10 32 43 64 65 72 79 86 92

Pass-IX 1 10 32 43 64 65 72 79 86 92
Sorted
Output: 1 10 32 43 64 65 72 79 86 92
-> 6 Marks
5. B)Develop aC function to implement bubble sort

Answer:
#include<stdio.h>
#define SIZE 1000
void bubble_Sort(int arr[], int n)
{
int i, j, t ;
for(i=0; i<n-1; i++)
{
for(j=0; j < n-1-i ; j++)
{
if(arr[j] >arr[j+1])
{
t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
}
void display(int arr[], int n)
{
for( inti=0; i<n; i++)
printf(“%d ”, arr[i]);
}
int main()
{
int arr[SIZE], n, i;
scanf(“%d”,&n);
for(i=0; i<n; i++)
scanf(“%d”,&arr[i]); -> 6.5 Marks
bubble_Sort(arr, n);
display(arr, n);
return 0;
}

6) Develop appropriate C functions for the following operations on matrices A Matrix


multiplication B. Find difference between sum of principal diagonal elements and
secondary diagonal elements. (12.5 Marks)

#include<stdio.h>    
#include<stdlib.h>  
int main(){  
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;    
system("cls");  
printf("enter the number of row=");    
scanf("%d",&r);    
printf("enter the number of column=");    
scanf("%d",&c);    
printf("enter the first matrix element=\n");    
for(i=0;i<r;i++)    
{    
for(j=0;j<c;j++)    
{    
scanf("%d",&a[i][j]);     ->2Marks
}    
}    
printf("enter the second matrix element=\n");    
for(i=0;i<r;i++)    
{    
for(j=0;j<c;j++)    
{    
scanf("%d",&b[i][j]);    
}    
}    
    
printf("multiply of the matrix=\n");    
for(i=0;i<r;i++)    
{    
for(j=0;j<c;j++)    
{    
mul[i][j]=0;    
for(k=0;k<c;k++)    
{    
mul[i][j]+=a[i][k]*b[k][j];     ->4.5Marks
}    
}    
}    
for(i=0;i<r;i++)    
{    
for(j=0;j<c;j++)    
{    
printf("%d\t",mul[i][j]);    
}    
printf("\n");    
}    
return 0;  
}  
6.b.
#include<stdio.h>
void main()
{
int a[10][10],sum=0,sum1=0,i,j,n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]); ->4Marks
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
sum=sum+a[i][j] ->4Marks
}
if(i+j==n-1)
{
sum1=sum1+a[i][j]
} }
}
printf("%d",sum=sum1);
}
7)Develop a function to find the smallest element of a single linked list? (4.5 Marks)

#include<stdio.h>
struct node
{
int data;
struct node *next;
}head = NULL,*temp,*p, *t; -> 1.5 Marks

void find_smallest()
{
int min;
min=head->data;
for( p=head ; p !=NULL ; p=p->next)
{
if(p->data < min)
min = p->data; ->3 Marks
}
printf("\nSmallest element is %d\n",min);
}

8) Implement push and pop functions of stack data structure. (4.5 Marks)

#include <stdio.h>
#define size 5
int stk[size],top=-1; - > 0.5 Marks

void Push()
{
int value;
printf("Enter the value to be inserted");
scanf("%d",&value);

if(top==size-1)
{
printf("Stack is Full/overflow");
}
else
{
top=top+1; - > 2 Marks
stk[top]=value;
}
}

void Pop()
{
int ele;
if(top==-1)
{
printf("Stack is Empty/Underflow");
}
else
{
ele=stk[top];
top=top-1; - > 2 Marks
printf("The element %d is deleted",ele);
}
}

9) Develop a display function for a Single Linked List (8 Marks)

#include<stdio.h>
struct node
{
int data;
struct node *next;

} *head=NULL, *temp,*p,*t; ->2 Marks

void display()
{
if (head==NULL)
printf(“The SLL is empty”);
else
{
p=head;
while(p!=NULL) ->6 Marks
{
printf(“%d  ”,p->data);
p=p->next;
}
}
}

10) Identify root, siblings, leaf nodes, and height of the tree from the following tree.
(8Marks)

i) The starting point of a tree is called as Root Node; The root node is 1; - > 2 Marks
ii) The children of same parent node is said to be siblings. Node 2 & 3, 4&5 are siblings
->2
Marks
iii) The nodes, which are not having any child is said to be leaf node. Node 8 and 9 are the leaf
node. - > 2 Marks
iv) The longest distance from the bottom most leaf to the root is said to be Height of the tree.
Height of the tree is 4 - > 2 Marks

11) Develop Push function of a stack data structure and perform the following operations
on an empty stack of size 3 and draw the status of the stack after every operation: push
(28), push (13), push (116), pop(), pop(), push (25), push(29), pop(), pop() (12.5 Marks)

Solution:

Step1: Step2: Step3:


push(28) push(13) push(116)

116 2
13 1 13 1
28 0 28 0 28 0
top value is 0 top value is 1 top value is 2

Step 4: Step 5: Step 6:


pop() pop() push(25)

13 1 25 1
28 0 28 0 28 0
top value is 1 top value is 0 top value is 1
popped value is 116 popped value is 13

Step 7: Step 8: Step 9:


push(29) pop() pop()

29 2
25 1 25 1
28 0 28 0 28 0
top value is 2 top value is 1 top value is 0
popped value is 29 popped value is 25

Final the stack contains value 28


->6 Marks
#define MAX 3
struct stack
{
int stk[MAX];
int top;
}s;

void push()
{
int add;
if(s.top==MAX-1)
{
printf("\nStack overflow");
}
else
{
printf("\nInsert element:");
scanf("%d",&add);
s.top=s.top+1;
s.stk[s.top]=add;
}
}
void pop() ->6.5 Marks
{
int del;
if(s.top==-1)
{
printf("stack underflow");
}
else
{
del=s.stk[s.top];
s.top=s.top-1;
printf("Popped value is:%d",del);
}

12) Develop appropriate C function for the following operations on the linked list and
analyze them: a. store element at the end of the list.
b. Prints maximum value in the list (12.5 marks)
a. Store element at the end of the list.

#include<stdio.h>
struct node
{
int data;
struct node *next;

} *head=NULL, *temp,*p,*t;
void insert_at_end()
{
int val;
printf("Entert the value");
scanf("%d",&val);

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


temp->data = val;
temp->next = NULL;
if(head ==NULL)
{
head =temp;
p=temp;
}
else
{
for(p=head; p->next!= NULL; p= p->next);
p->next = temp;
p = temp; ->6.5 Marks
}
printf("The element is inserted ");
}

b. Prints maximum value in the list

//maxNode() will find out the maximum value node in the list
void maxNode()
{
struct node *current = head;
int max=0;
if(head == NULL)
{
printf("List is empty \n");
}
else
{
while(current != NULL)
{

if(max < current->data)


{
max = current->data; ->6 Marks
}
current = current->next;
}
printf("Maximum value node in the list: %d\n", max);
}
}

You might also like