VIT SKILL RACK SOLUTIONS

CSC 1002 PRACTICE PROBLEM 1 SKILLRACK SOLUTIONS 2017 WINTER SEM

CSC 1002 PRACTICE PROBLEM SKILLRACK SOLUTIONS 2017 WINTER SEM

Question 1 (Second Smallest Number)

Given a set of elements, design an Algorithm and write the subsequent C program to determine the second smallest number in that set.
Input Format
Number of elements in ‘n’
element-1
element-2
element-n
Output Format
Second smallest element in the set

Solution

#include< stdio.h >
void main()
{
    int n,i,num,small=999,sec=999;
    scanf("%d",&n);
    for(i=0;i < n;i++)
    {
        scanf("%d",&num);
        if(num < =small)
        {
            sec=small;
            small=num;
        }
        else if(num < =sec)
        sec=num;
    }
    printf("%d",sec);
}

Input

INPUT:

n - number of elements

Output

OUTPUT:

printf("%d",sec);

Processing

for(i=0;i < n;i++)
    {
        scanf("%d",&num);
        if(num < =small)
        {
            sec=small;
            small=num;
        }
        else if(num < =sec)
        sec=num;
    }

Pseudocode

BEGIN

Read n
for(i=0;i < n;i++)
    {
        scanf("%d",&num);
        if(num < =small)
        {
            sec=small;
            small=num;
        }
        else if(num < =sec)
        sec=num;
    }
printf("%d",sec);

END

Question 2 (Recursive Fibonacci)

Given the value of ‘n’, write a recursive routine in C to print the first ‘n’ elements of the Fibonacci series.
Input Format
Value of ‘n’
Output Format
Fibonacci series of ‘n’ terms, each term separated by a space

Solution

#include< stdio.h >
void main()
{
    int i,n,f=0,sum=0,num=1;
    scanf("%d",&n);
    printf("0 1");
    for(i=0;i < n-2;i++)
    {
        sum=f+num;
        f=num;
        num=sum;
        printf(" %d",sum);
    }
}

Input

INPUT:

n - number of terms

Output

OUTPUT:

printf("0 1");
printf(" %d",sum);

Processing

for(i=0;i < n-2;i++)
    {
        sum=f+num;
        f=num;
        num=sum;
    }

Pseudocode

BEGIN

Read n
printf("0 1");
for(i=0;i < n-2;i++)
    {
        sum=f+num;
        f=num;
        num=sum;
        printf(" %d",sum);
    }

END

Question 3 (Cyclic Right shift of Elements)

Given a set of elements stored in an array and a number ‘m’, design an Algorithm and write the subsequent C program to perform cyclic right shift of the array by ‘m’ places. For example, if the elements are 12, 13, 16, 7, 10 and m =2 then the resultant set will be 7, 10, 12, 13, 16.
Input Format
Number of elements in the set: ‘n’
element-1
element-2
element-n
value of ‘m’
Output Format
Elements in the set after right shift by ‘m’ places

Solution

#include< stdio.h >
void main()
{
    int a[20],n,i,m,t,j;
    scanf("%d",&n);
    for(i=0;i < n;i++)
    scanf("%d",&a[i]);
    scanf("%d",&m);
    for(j=0;j < m;j++) 
    { 
        t=a[n-1];  
        for(i=n-1;i > 0;i--)
        a[i]=a[i-1];
        a[0]=t;
    }
    for(i=0;i < n;i++)
    printf("%d\n",a[i]);
}

Input

INPUT:

n - number of terms
for(i=0;i < n;i++)
scanf("%d",&a[i]);
m - number of shifts

Output

OUTPUT:

for(i=0;i < n;i++)
printf("%d\n",a[i]);

Processing

for(j=0;j < m;j++)  
{       t=a[n-1];  
        for(i=n-1;i > 0;i--)
        a[i]=a[i-1];
        a[0]=t;
}

Pseudocode

BEGIN

Read n, array and m
for(j=0;j < m;j++)
{       t=a[n-1];  
        for(i=n-1;i > 0;i--)
        a[i]=a[i-1];
        a[0]=t;
}
for(i=0;i < n;i++)
    printf("%d\n",a[i]);

END

Qestion 4 (Leaders of Elements)

Given a set of ‘n’ elements in an order, identify all the leaders and print them. An element is said to be a leader if all the elements to its right are smaller than it. For example, if the elements are 12, 13, 16, 7, 10 then there is only one leader element 16. If there are no leaders in the given set of elements then print ‘No leaders’.
Input Format
Number of elements in the given set: ‘n’
element-1
element-2
element-n
Output Format
Elements that are leaders. Else, print ‘No leaders’ when there is no leader.

Solution

#include< stdio.h >
void main()
{
    int a[20],f=1,flag=0,i,j,n;
    scanf("%d",&n);
    for(i=0;i < n;i++)
    scanf("%d",&a[i]);
    for(i=0;i < n-1;i++)
    {
        for(j=i+1;j < n;j++) 
        if(a[i] > a[j])
        f=1;
        else
        {
            f=0;
            break;
        }
        if(f==1)
        {
            printf("%d\n",a[i]);
            flag=1;
        }
        f=0;
    }
    if(flag==0)
    printf("No leaders");
}

Input

INPUT:

n - number of elements
for(i=0;i < n;i++)
scanf("%d",&a[i]);

Output

OUTPUT:
if(f==1)
printf("%d\n",a[i]);
if(flag==0)
printf("No leaders");

Processing

for(i=0;i < n-1;i++)
    {
        for(j=i+1;j < n;j++) 
        if(a[i] > a[j])
        f=1;
        else
        {
            f=0;
            break;
        }
        if(f==1)
        flag=1;
        f=0;
    }

Pseudocode

BEGIN

Read n
for(i=0;i < n-1;i++)
    {
        for(j=i+1;j < n;j++) 
        if(a[i] > a[j])
        f=1;
        else
        {
            f=0;
            break;
        }
        if(f==1)
        {
            printf("%d\n",a[i]);
            flag=1;
        }
        f=0;
    }

END

Question 5 (Recursive reverse)

Given a string, write a recursive routine to reverse it. For example, given the string ‘and, the reversal of the string is ‘dna’.
Input Format
A string
Output Format
Reverse of the string

Solution

#include< stdio.h >
#include< string.h >
void main()
{
    char s[20],ch;
    int i,len;
    scanf("%s",s);
    len=strlen(s);
    for(i=0;i < (len/2);i++)
    {
        ch=s[i];
        s[i]=s[len-1-i];
        s[len-1-i]=ch;
    }
    puts(s);
}

Input

INPUT:

s - string

Output

OUTPUT:

puts(s);

Processing

for(i=0;i < (len/2);i++)
    {
        ch=s[i];
        s[i]=s[len-1-i];
        s[len-1-i]=ch;
    }

Pseudocode

BEGIN

Read s
for(i=0;i < (len/2);i++)
    {
        ch=s[i];
        s[i]=s[len-1-i];
        s[len-1-i]=ch;
    }
puts(s);

END

zero

Phasellus facilisis convallis metus, ut imperdiet augue auctor nec. Duis at velit id augue lobortis porta. Sed varius, enim accumsan aliquam tincidunt, tortor urna vulputate quam, eget finibus urna est in augue.

No comments:

Post a Comment