Problem Solving Through Programming In C Week 7

Session: JULY-DEC 2023

Course Name: Problem Solving Through Programming In C

Course Link: Click Here

These are Problem Solving Through Programming In C Assignment 7 Answers


Q1. Which of the following statements are correct?
1) A string is a collection of characters terminated by ‘\0’.
2) The format specifier %s is used to print a string.
3) The length of the string can be obtained by strlen().
4) strcon() function is used to join two strings.

a) 1,2
b) 1,2,3
c) 2,4
d) 1,3

Answer: b) 1,2,3


Q2. The right method of initializing a 2D array is
a) int abc[2][2] = {1, 2, 3 ,4 }
b) int abc[ ][ ] = {1, 2, 3 ,4 }
c) int abc[2][ ] = {1, 2, 3 ,4 }
d) all of the above

Answer: a) int abc[2][2] = {1, 2, 3 ,4 }


Q3. Array passed as an argument to a function is interpreted as
a) Address of all the elements in an array
b) Value of the first element of the array
c) Address of the first element of the array
d) Number of element of the array

Answer: c) Address of the first element of the array


These are Problem Solving Through Programming In C Assignment 7 Answers


Q4. What will be the output?

Answer: 10


Q5. Find the output of the following C program.
a) fellows
b) h
c) fello
d) Compiler error

Answer: a) fellows


Q6. What will be the output?
a) n1=18, n2=17
b) n1=18, n2=18
c) n1=17, n1=17
d) n1=17, n2=18

Answer: a) n1=18, n2=17


These are Problem Solving Through Programming In C Assignment 7 Answers


Q7. Consider the following C program segment :
a) gnirts
b) gnirt
c) string
d) no output is printed

Answer: d) no output is printed


Q8. If the starting address of an float array Arr[10][10] is 2000, what would be the memory address of the element Arr[5][6]? (float takes 4 bytes of memory)
a) 2268
b) 2120
c) 2224
d) 2144

Answer: c) 2224


Q9. In C, the placement of elements of a two dimensional array is
a) Row wise
b) Column wise
c) Diagonal wise
d) Bottom to top wise

Answer: a) Row wise


These are Problem Solving Through Programming In C Assignment 7 Answers


Q10. What will be the value of ‘i’ after the execution of the C code fragment given below?

Answer: 0


Assignment

Question 1
Write a C Program to Count Number of Uppercase and Lowercase Letters in a given string. The given string may be a word or a sentence.

Solution:

   int i = 0;
   while (ch[i] != '\0')
   {
      if (ch[i] >= 'A' && ch[i] <= 'Z')
         upper++;
      if (ch[i] >= 'a' && ch[i] <= 'z')
         lower++;
      i++;
   }

Question 2
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:

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

Question 3
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
The elements of the output matrix are separated by one blank space

Solution:

      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;
}

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:

    int i=0,j=0,k=0,a,minIndex=0,maxIndex=0,max=0,min=0;
	char c;
	while(str[k]!='\0')  //for splitting sentence into words
    {
        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 Problem Solving Through Programming In C Assignment 7 Answers

More Weeks of Problem Solving Through Programming In C: Click here

More Nptel Courses: Click here


Session: JAN-APR 2023

Course Name: Problem Solving Through Programming In C

Course Link: Click Here

These are Problem Solving Through Programming In C Assignment 7 Answers


Q1. The searching operation in an array is done using
a) Key and index
b) Only key
c) Only index
d) None of these

Answer: a) Key and index


Q2. Find the output of the following C program.
a) fellows
b) h
c) fello
d) Compiler error

Answer: a) fellows


These are Problem Solving Through Programming In C Assignment 7 Answers


Q3. What will be the output?
a) n1=18, n2=17
b) n1=18, n2=18
c) n1=17, n1=17
d) n1=17, n2=18

Answer: a) n1=18, n2=17


Q4. Consider the following C program segment:
a) gnirts
b) gnirt
c) string
d) no output is printed

Answer: d) no output is printed


These are Problem Solving Through Programming In C Assignment 7 Answers


Q5. What will be the value of ā€˜iā€™ after the execution of the C code given below?
a) 0
b) 1
c) -1
d) None

Answer: a) 0


Q6. If the starting address of an float array Arr[10][10] is 2000, what would be the memory address of the element Arr[5][6]? (float takes 4 bytes of memory)
a) 2268
b) 2120
c) 2224
d) 2144

Answer: c) 2224


These are Problem Solving Through Programming In C Assignment 7 Answers


Q7. In C, the placement of elements of a two dimensional array is
a) Row wise
b) Column wise
c) Diagonal wise
d) Bottom to top wise

Answer: a) Row wise


Q8.

image 22

Answer: 321004


These are Problem Solving Through Programming In C Assignment 7 Answers


Q9. What will be the output?
a) The string is empty
b) The string is not empty
c) Error
d) None of the above

Answer: b) The string is not empty


Q10. What is the output of the following C code?
a) Compilation error
b) 7
c) 1
d) 2

Answer: a) Compilation error


These are Problem Solving Through Programming In C Assignment 7 Answers


Problem Solving Through Programming In C Programming Assignment

Question 1

Write a C Program to Count Number of Uppercase and Lowercase Letters in a given string. The given string may be a word or a sentence.

Solution:

int i = 0;
   while (ch[i] != '\0') {
      if (ch[i] >= 'A' && ch[i] <= 'Z')
         upper++;
      if (ch[i] >= 'a' && ch[i] <= 'z')
         lower++;
      i++;
   }

Question 2

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:

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 Problem Solving Through Programming In C Assignment 7 Answers


Question 3

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
The elements of the output matrix are separated by one blank space

Solution:

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;
}

Question 4

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:

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 Problem Solving Through Programming In C Assignment 7 Answers

More Weeks of Problem Solving Through Programming In C: Click Here

More Nptel courses: https://progiez.com/nptel


Session: JULY-DEC 2022

Course Name: Problem Solving Through Programming In C NPTEL

Link of course: Click Here

These are Problem Solving Through Programming In C Assignment 7 Answers


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 Problem Solving Through Programming In C Assignment 7 Answers


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 Problem Solving Through Programming In C Assignment 7 Answers


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 Problem Solving Through Programming In C Assignment 7 Answers


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:

//Code

These are Problem Solving Through Programming In C Assignment 7 Answers


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:

//Code

These are Problem Solving Through Programming In C Assignment 7 Answers


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:

//Code

These are Problem Solving Through Programming In C Assignment 7 Answers


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:

//Code

These are Problem Solving Through Programming In C Assignment 7 Answers

More NPTEL Solution: https://progiez.com/nptel


These are Problem Solving Through Programming In C Assignment 7 Answers
The content uploaded on this website is for reference purposes only. Please do it yourself first.
Home
Account
Cart
Search