Home

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

NPTEL Problem Solving Through Programming In C - Programming Assignment | Jan-2024 Week 9 to 10

NPTEL Problem Solving Through Programming In C - Programming Assignment | Jan-2024 Week 9 to 10



  Please scroll down for latest Programs ðŸ‘‡ 




Week-09 Program-01

Due on 2024-03-28, 23:59 IST
Write a program to print all the locations at which a particular element (taken as input) is found in a list and also print the total number of times it occurs in the list. The location starts from 1.

For example, if there are 4 elements in the array
5
6
5
7
If the element to search is 5 then the output will be
5 is present at location 1
5 is present at location 3
5 is present 2 times in the array.

Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
   int array[100], search, n, count = 0;
5
   //"search" is the key element to search and 'n' is the total number of element of the array
6
   // "count" is to store total number of elements
7
8
 scanf("%d", &n); //Number of elements is taken from test case
9
10
 int c;
11
   for (c = 0; c < n; c++)
12
      scanf("%d", &array[c]);
13
14
   scanf("%d", &search); // The element to search is taken from test case
15
16
/* Use the printf statements as below:
17
"%d is present at location %d.\n"  for each locations
18
"%d is not present in the array.\n" if the element is not found in the list
19
"%d is present %d times in the array.\n"
20
*/
21
for (c = 0; c < n; c++)
22
    {
23
      if (array[c] == search)
24
      {
25
         printf("%d is present at location %d.\n", search, c+1);
26
         count++;
27
      }
28
    }
29
   if (count == 0)
30
      printf("%d is not present in the array.", search);
31
   else
32
      printf("%d is present %d times in the array.", search, count);
33
34
   return 0;
35
}
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
4
5
6
5
7
5
5 is present at location 1.\n
5 is present at location 3.\n
5 is present 2 times in the array.
5 is present at location 1.\n
5 is present at location 3.\n
5 is present 2 times in the array.
Passed
Test Case 2
5
67
80
45
97
100
50
50 is not present in the array.
50 is not present in the array.
Passed





Week-09 Program-02

Due on 2024-03-28, 23:59 IST
Write a C program to search a given element from a 1D array and display the position at which it is found by using linear search function. The index location starts from 1.
Select the Language for this assignment. 
1
#include <stdio.h>
2
int linear_search(int[], int, int);
3
int main()
4
{
5
   int array[100], search, c, n, position;
6
   /* search - element to search, c - counter, n - number of elements in array,
7
   position - The position in which the element is first found in the list. */
8
9
    scanf("%d", &n); // Number of elements in the array is read from the test case data
10
11
    for (c = 0; c < n; c++)
12
    scanf("%d", &array[c]); //Elements of array is read from the test data
13
14
    scanf("%d", &search);  //Element to search is read from the test case data
15
16
   /* Use the following in the printf statement to print the output
17
   printf("%d is not present in the array.\n", search);
18
   printf("%d is present at location %d.\n", search, position+1); //As array[0] has the position 1
19
   */
20
position = linear_search(array, n, search);
21
22
   if (position == -1)
23
      printf("%d is not present in the array.", search);
24
   else
25
      printf("%d is present at location %d.", search, position+1);
26
   return 0;
27
}
28
29
int linear_search(int a[], int n, int find) {
30
   int c;
31
   for (c = 0 ;c < n ; c++ )
32
    {
33
      if (a[c] == find)
34
         return c;
35
    }
36
   return -1;
37
}
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
5
78
90
34
54
98
90
90 is present at location 2.
90 is present at location 2.
Passed
Test Case 2
6
30
40
50
20
90
60
90
90 is present at location 5.
90 is present at location 5.
Passed





Week-09 Program-03

Due on 2024-03-28, 23:59 IST
Write a C program to search a given number from a sorted 1D array and display the position at which it is found using binary search algorithm. The index location starts from 1.
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
 int c, n, search,
5
 array[100];
6
 scanf("%d",&n); //number of elements in the array
7
8
 for (c = 0; c < n; c++)
9
 scanf("%d",&array[c]);
10
11
 scanf("%d", &search); //The element to search is read from test case.
12
13
/* Use the printf statements as below:
14
 printf("%d found at location %d.\n", search, variable_name);
15
 printf("Not found! %d isn't present in the list.\n", search);
16
*/
17
int first, last, middle;
18
   first = 0;
19
   last = n - 1;
20
   middle = (first+last)/2;
21
22
   while (first <= last) {
23
      if (array[middle] < search)
24
         first = middle + 1;
25
      else if (array[middle] == search) {
26
         printf("%d found at location %d.", search, middle+1);
27
         break;
28
      }
29
      else
30
         last = middle - 1;
31
32
      middle = (first + last)/2;
33
      }
34
   if (first > last)
35
      printf("Not found! %d isn't present in the list.", search);
36
37
     return 0;
38
      }
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
5
10
20
30
40
50
40
40 found at location 4.
40 found at location 4.
Passed
Test Case 2
4
44
55
66
77
10
Not found! 10 isn't present in the list.
Not found! 10 isn't present in the list.
Passed




Week-09 Program-04

Due on 2024-03-28, 23:59 IST
Write a C program to reverse an array by swapping the elements and without using any new array.
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main() {
3
  int array[100], n, c;
4
  scanf("%d", &n); // n is number of elements in the array.
5
  for (c = 0; c < n; c++) {
6
    scanf("%d", &array[c]);
7
  }
8
int temp, end;
9
   end = n - 1;
10
   for (c = 0; c < n/2; c++) {
11
    temp       = array[c];
12
    array[c]   = array[end];
13
    array[end] = temp;
14
15
    end--;
16
  }
0
printf("Reversed array elements are:\n");
1
2
  for (c = 0; c < n; c++) {
3
    printf("%d\n", array[c]);
4
  }
5
  return 0;
6
}
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
4
10
20
30
40
Reversed array elements are:\n
40\n
30\n
20\n
10
Reversed array elements are:\n
40\n
30\n
20\n
10\n
Passed after ignoring Presentation Error
Test Case 2
5
50
60
40
30
20
Reversed array elements are:\n
20\n
30\n
40\n
60\n
50
Reversed array elements are:\n
20\n
30\n
40\n
60\n
50\n
Passed after ignoring Presentation Error







Week-10: Problem 01

Due on 2024-04-04, 23:59 IST
Your last recorded submission was on 2024-03-24, 20:02 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
float fun (float x); //Function fun returns the function value of f(x)
3
void bisection (float *x, float a, float b, int *itr); // This function computes the root of f(x) using bisection method
4
5
int main ()
6
{
7
    int itr = 0, maxmitr=10;
8
    float x, a=1.0, b=2.0, allerr, x1; // x is the value of root in each iteration, x1 is the final value of the root 
9
   // a and b are the initial range for calculating the root using bisection method
10
      
11
scanf("%f", &allerr);  // allerr is the allowable error taken from test case data 
12
    bisection (&x, a, b, &itr);
13
14
/* Use the printf statement as given below to print the root
15
printf("Root = %1.4f\n", x1); */
16
do
17
    {
18
        if (fun(a)*fun(x) < 0)
19
            b=x;
20
        else
21
            a=x;
22
        bisection (&x1, a, b, &itr);
23
        if (((x1-x)<0 && -(x1-x)< allerr) || ((x1-x)>0 && (x1-x)< allerr))
24
        {
25
            printf("Root = %1.4f", x1);
26
            return 0;
27
        }
28
        x=x1;
29
    }
30
    while (itr < maxmitr);
31
    return 1;
32
}
33
float fun (float x)
34
{
35
    return (2*x*x*x - 3*x - 5);
36
}
37
void bisection (float *x, float a, float b, int *itr)
38
/* this function performs and prints the result of one iteration */
39
{
40
    *x=(a+b)/2;
41
    ++(*itr);
42
}
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.
   



Private Test cases used for EvaluationStatus
Test Case 1
Passed

 


Week-10: Problem 02

Due on 2024-04-04, 23:59 IST
Your last recorded submission was on 2024-03-24, 20:04 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
float f(float x);
3
float df (float x);
4
5
int main()
6
{
7
    int itr, maxmitr; // itr is the iteration number and maxitr is the maximum allowable iteration 
8
    float x0=1.0, x1; // x0 is the initial value and x1 is result 
9
    scanf("%d", &maxmitr); // Taken from the test cases 
10
11
// use the printf statement as printf("Root = %8.6f\n", x1);
12
float h;
13
    for (itr=1; itr<=maxmitr; itr++)
14
    {
15
        h=f(x0)/df(x0);
16
        x1=x0-h;
17
        x0=x1;
18
    }
19
    printf("Root = %8.6f", x1);
20
    return 0;
21
}
22
float f(float x)
23
{
24
    return x*x*x - 2*x  - 3;
25
}
26
float df (float x)
27
{
28
    return 3*x*x-2;
29
}
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.
   



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



Week-10: Problem 03

Due on 2024-04-04, 23:59 IST
Write a C program to sort a given 1D array using pointer in ascending order.
Your last recorded submission was on 2024-03-24, 20:05 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
    int a[100],i, n;
5
    scanf("%d",&n);  //Number of elements of the array is taken from the test case data 
6
   
7
   for (i=0; i<n; i++)
8
    {
9
        scanf("%d",a+i); // Input the array elements
10
    }
11
int j,t;
12
for (i=0; i<(n-1); i++) 
13
    {
14
        for (j=i+1; j<n; j++)
15
        {
16
            if (*(a+i)>*(a+j))
17
            {
18
                t=*(a+i);
19
                *(a+i)=*(a+j);
20
                *(a+j)=t;
21
            }
22
        }
23
    }
0
//   Printing sorted array in ascending order 
1
    for (i=0; i<n; i++)
2
    {
3
        printf("%d\n",*(a+i));
4
    }
5
    return 0;
6
   }
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.
   



Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week-10: Problem 04

Due on 2024-04-04, 23:59 IST
Write a C program to sort a 1D array using pointer by applying Bubble sort technique.
Your last recorded submission was on 2024-03-24, 20:05 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
void sort(int *a, int n);
3
int main()
4
{
5
    int a[20];
6
    int n,i; 
7
    scanf("%d",&n); // Enter number of elements to sort is taken from test case data
8
   
9
    for(i=0;i<n;i++)
10
    {
11
        scanf("%d",&a[i]); // The elements of the array is taken from the test data
12
    }
13
14
sort(a, n); // Calling the sorting function
15
16
    //Printing the sorted array 
17
    for(i=0;i<n;i++)
18
    {
19
        printf("%d\n",a[i]);
20
    }
21
   return 0;
22
}
23
void sort(int *a, int n)
24
{
25
    int i,temp,j;
26
    for(i=1;i<n;i++)
27
    {
28
        for(j=0;j<n-i;j++)
29
        {
30
           if(*(a+j)>=*(a+j+1))
31
        {
32
            temp = *(a+j);
33
            *(a+j)= *(a+j+1);
34
            *(a+j+1)= temp;
35
        }
36
        }
37
    }
38
}
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.
   



Private Test cases used for EvaluationStatus
Test Case 1
Passed



























































































































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.