Problem Solving Through Programming In C NPTEL Assignment 7

                     

Course Name: Problem Solving Through Programming In C NPTEL

Link of course: Click Here

These are answers for Problem Solving Through Programming In C Assignment 7


1) Which of the following statement/s are false?
I. Array elements are stored in memory in contiguous locations.
II. An integer array always terminates with ‘\0’ (NULL).

a)l
b) ll
c) Both I and II
d) None

Answer: b) ll


2) If two strings are identical, then strcmp() function returns
a) 1
b) 0
c)-1
d) None of these

Answer: b) 0


3) Which of the following function is more appropriate for reading in a multi word string?
a) scanf()
b) gets()
c) printf()
d) puts()

Answer: b) gets()


These are answers for Problem Solving Through Programming In C Assignment 7


4) What will be printed after execution of the following code?

Answer: 40


5) What will be the output of the program?
a) Array
b) Array String
c) Array\0String
d) Compilation error

Answer: a) Array


6) What will be the output?
a) Programming
b) Language
c) Programming Language
d) Language Programming

Answer: c) Programming Language


These are answers for Problem Solving Through Programming In C Assignment 7


7) What will be the output?
a) n1= 8, n2=8
b) n1=9, n2=9
c) n1=8, n2=9
d) n1=9, n2=8

Answer: d) n1=9, n2=8


8) What will be the output?

Answer: 720


9) What will be the output?
a) assignment
b) tnemngissa
c) nothing will be printed
d) tttttttttt

Answer: c) nothing will be printed


These are answers for Problem Solving Through Programming In C Assignment 7


10) What will be the output?
a) Two strings are equal
b) Two strings are not equal
c) Would not print anything
d) Compilation error

Answer: c) Would not print anything


Assignment

Question 1
Write a C program to find the sum of all elements of each row of a matrix.
Example:
For a matrix
 4 5 6
 6 7 3
 1 2 3
 The output will be
 15
 16
 6

Solution:

#include < stdio.h>
int main()
{
    int matrix[20][20];
    int i,j,r,c;

    scanf("%d",&r);
    scanf("%d",&c);

    for(i=0;i< r;i++)
    {
        for(j=0;j< c;j++)
        {
            scanf("%d",&matrix[i][j]);
        }
    }
    int sum;
    for(i=0;i< r;i++)
    {
        sum=0;
        for(j=0;j< c;j++)
        {
            sum += matrix[i][j];
        }
        printf("%d\n",sum);
    }

}

These are answers for Problem Solving Through Programming In C Assignment 7


Question 2
Write a C program to find subtraction of two matrices i.e, matrix_A – matrix_B=matrix_C.
If the given martix are:
 2 3 5 and 1 5 2
Then the output will be 1 -2 3
 4 5 6        2 3 4                                          2 2 2
 6 5 7        3 3 4                                          3 2 3

Solution:

#include < stdio.h>
int main()
{
    int matrix_A[20][20];
    int matrix_B[20][20];
    int matrix_C[20][20];
    int i,j,row,col;
    scanf("%d",&row);
    scanf("%d",&col);

    for(i=0; i< row; i++)
    {
        for(j=0; j< col; j++)
        {
            scanf("%d", &matrix_A[i][j]);
        }
    }

    for(i=0; i< row; i++)
    {
        for(j=0; j< col; j++)
        {
            scanf("%d", &matrix_B[i][j]);
        }
    }
    for(i=0; i< row; i++)
    {
        for(j=0; j< col; j++)
        {
         matrix_C[i][j] = matrix_A[i][j] - matrix_B[i][j];
        }
    }

    for(i=0; i< row; i++)
    {
        for(j=0; j< col; j++)
        {
            printf("%d ", matrix_C[i][j]);
        }
        printf("\n");
    }
    return 0;
}

These are answers for Problem Solving Through Programming In C Assignment 7


Question 3
Write a C program to print lower triangle of a square matrix.
For example the output of a given matrix
2 3 4 will be 2 0 0
5 6 7             5 6 0
4 5 6             4 5 6

Solution:

#include < stdio.h>
int main()
{
    int matrix[20][20];
    int i,j,r;

    scanf("%d", &r);

    for(i=0;i< r;i++)
    {
        for(j=0;j< r; j++)
        {
            scanf("%d",&matrix[i][j]);
        }
    }
    for(i=0; i< r; i++)
    {
    	for(j=0; j< r; j++)
        {
          if(i>=j)
              printf("%d ", matrix[i][j]);
          else
              printf("%d ", 0);
        }
        printf("\n");
    }
    return 0;
}

These are answers for Problem Solving Through Programming In C Assignment 7


Question 4
Write a C program to print Largest and Smallest Word from a given sentence. If there are two or more words of same length, then the first one is considered. A single letter in the sentence is also consider as a word.

Solution:

#include< stdio.h>
#include< string.h>
int main()
{
    char str[100]={0},substr[100][100]={0};
    scanf("%[^\n]s", str);
     int i=0,j=0,k=0,a;
     int minIndex=0,maxIndex=0,max=0,min=0;
    char c;
    //for splitting sentence into words
    while(str[k]!='\0')
    {
      j=0;
      while(str[k]!=' '&&str[k]!='\0' && str[k]!='.')
        {
            substr[i][j]=str[k];
            k++;
            j++;
        }
        substr[i][j]='\0';
        i++;
        if(str[k]!='\0')
        {
            k++;
        }
    }
    int len=i;
    max=strlen(substr[0]);
    min=strlen(substr[0]);

    for(i=0;i< len;i++)
    {
       a=strlen(substr[i]);
       if(a>max)
        {
            max=a;
            maxIndex=i;
        }
        if(a< min)
        {
            min=a;
            minIndex=i;
        }
    }
    printf("Largest Word is: %s\nSmallest word is: %s\n",substr[maxIndex],substr[minIndex]);
    return 0;
}

These are answers for Problem Solving Through Programming In C Assignment 7

All weeks of Problem Solving Through Programming In C: https://progies.in/answers/nptel/problem-solving-through-programming-in-c

More NPTEL Solution: https://progies.in/answers/nptel


* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first.


These are answers for Problem Solving Through Programming In C Assignment 7