Home

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

Problem Solving Through Programming In C | Week-07: Programs | Jan-2022 | NPTEL

 NPTEL » Problem Solving Through Programming In C




Subscribe to our YouTube Channel :  Swayam Solver



Week-07: Program-01

Due on 2022-03-17, 23:59 IST
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.
Select the Language for this assignment. 
1
#include<stdio.h>
2
int main() {
3
   int upper = 0, lower = 0;
4
   char ch[100];
5
   scanf(" %[^\n]s", ch);  /*A word or a sentence is accepted from test case data */
6
7
/* Complete the remaining part of the code to store number of uppercase letters
8
in the variable upper and lowercase letters in variable lower.
9
The print part of already written. You can declare any variable if necessary */
10
11
int i = 0;
   while (ch[i] != '\0') {
      if (ch[i] >= 'A' && ch[i] <= 'Z')
         upper++;
      if (ch[i] >= 'a' && ch[i] <= 'z')
         lower++;
      i++;
   }
0
   printf("Uppercase Letters : %d\n", upper); /*prints number of uppercase letters */
1
   printf("Lowercase Letters : %d", lower); /*prints number of lowercase letters */
2
 
3
   return (0);
4
}
5
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
National Programme on Technology Enhanced Learning is a joint initiative of the IITs and IISc.
Uppercase Letters : 11\n
Lowercase Letters : 68\n
Uppercase Letters : 11\n
Lowercase Letters : 68
Passed after ignoring Presentation Error
Test Case 2
Problem Solving through Programming in C.
Uppercase Letters : 4\n
Lowercase Letters : 31\n
Uppercase Letters : 4\n
Lowercase Letters : 31
Passed after ignoring Presentation Error



Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed






Week-07 Program-02

Due on 2022-03-17, 23:59 IST
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

Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
    int matrix[20][20];
5
    int i,j,r,c;
6
7
    scanf("%d",&r); //Accepts number of rows
8
    scanf("%d",&c); //Accepts number of columns 
9
10
    for(i=0;i< r;i++) //Accepts the matrix elements from the test case data
11
    {
12
        for(j=0;j< c;j++)
13
        {
14
            scanf("%d",&matrix[i][j]); 
15
        }
16
    }
17
/*Complete the code to print the sum of each rows. Use the printf() statement as
18
 printf("%d\n",sum); Where sum is the sum of a row. 
19
*/  
20
21
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);
    }
 
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
3
3
1
1
1
2
2
2
3
3
3
3\n
6\n
9\n
3\n
6\n
9\n
Passed
Test Case 2
2
3
1
2
3
4
5
6
6\n
15\n
6\n
15\n
Passed




Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed





Week-07 Program-03

Due on 2022-03-17, 23:59 IST
Write a C program to find subtraction of two matrices i.e. matrix_A  - matrix_B = matrix_C.
If the given matrix are
2 3 5        and       1 5 2
4 5 6                     2 3 4
6 5 7                     3 3 4
Then the output will be
1 -2  3
2  2  2
3  2  3
(The elements of the output matrix are separated by one blank space)


Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
    int matrix_A[20][20], matrix_B[20][20], matrix_C[20][20];
5
    int i,j,row,col;
6
    scanf("%d",&row); //Accepts number of rows
7
    scanf("%d",&col); //Accepts number of columns 
8
 
9
    /* Elements of first matrix are accepted from test data */
10
    for(i=0; i<row; i++)
11
    {
12
        for(j=0; j<col; j++)
13
        {
14
            scanf("%d", &matrix_A[i][j]);
15
        }
16
    }
17
18
     /* Elements of second matrix are accepted from test data */
19
    
20
    for(i=0; i<row; i++)
21
    {
22
        for(j=0; j<col; j++)
23
        {
24
            scanf("%d", &matrix_B[i][j]);
25
        }
26
    }
27
28
/* Complete the program to get the desired output. Use printf() statement as below
29
    printf("%d ", matrix_C[i][j]); You can declare your own variables if required. 
30
*/
31
32
/* 
    Subtract both matrices and store the result in matrix C
  */
   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;
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
3
3
2
3
5
4
5
6
6
5
7
1
5
2
2
3
4
3
3
4
1 -2 3 \n
2 2 2 \n
3 2 3\n
1 -2 3 \n
2 2 2 \n
3 2 3 \n
Passed after ignoring Presentation Error



Private Test cases used for EvaluationStatus
Test Case 1
Passed






Week-07 Program-04

Due on 2022-03-17, 23:59 IST
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.
Select the Language for this assignment. 
1
#include<stdio.h>
2
#include<string.h>
3
int main()
4
{
5
    char str[100]={0},substr[100][100]={0}; 
6
//str[100] is for storing the sentence and substr[50][50] is for storing each word.
7
    
8
scanf("%[^\n]s", str); //Accepts the sentence from the test case data.
9
10
/* Complete the program to get the desired output.
11
The print statement should be as below
12
 
13
printf("Largest Word is: %s\nSmallest word is: %s\n", -------,--------);
14
15
*/
16
17
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]);
    //After splitting getting length of string and finding its index having max length and index having min length
    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;  
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
Problem Solving through Programming in C.
Largest Word is: Programming\n
Smallest word is: C\n
Largest Word is: Programming\n
Smallest word is: C\n
Passed
Test Case 2
NPTEL is a joint initiative of the IITs and IISc.
Largest Word is: initiative\n
Smallest word is: a
Largest Word is: initiative\n
Smallest word is: a\n
Passed after ignoring Presentation Error




Private Test cases used for EvaluationStatus
Test Case 1
Passed





Watch the video to see the program in motion.


Please Subscribe to our YouTube Channel :  Swayam Solver

This will help the creator to continue making quality content...

Have a great day !

No comments:

Post a Comment

Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.