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

 


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. 
#include<stdio.h>
float fun (float x); //Function fun returns the function value of f(x)
void bisection (float *x, float a, float b, int *itr); // This function computes the root of f(x) using bisection method
 
int main ()
{
    int itr = 0, maxmitr=10;
    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 
   // a and b are the initial range for calculating the root using bisection method
      
scanf("%f", &allerr);  // allerr is the allowable error taken from test case data 
    bisection (&x, a, b, &itr);
 
/* Use the printf statement as given below to print the root
printf("Root = %1.4f\n", x1); */
do
    {
        if (fun(a)*fun(x) < 0)
            b=x;
        else
            a=x;
        bisection (&x1, a, b, &itr);
        if (((x1-x)<0 && -(x1-x)< allerr) || ((x1-x)>0 && (x1-x)< allerr))
        {
            printf("Root = %1.4f", x1);
            return 0;
        }
        x=x1;
    }
    while (itr < maxmitr);
    return 1;
}
float fun (float x)
{
    return (2*x*x*x - 3*x - 5);
}
void bisection (float *x, float a, float b, int *itr)
/* this function performs and prints the result of one iteration */
{
    *x=(a+b)/2;
    ++(*itr);
}
   



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. 
#include<stdio.h>
float f(float x);
float df (float x);
 
int main()
{
    int itr, maxmitr; // itr is the iteration number and maxitr is the maximum allowable iteration 
    float x0=1.0, x1; // x0 is the initial value and x1 is result 
    scanf("%d", &maxmitr); // Taken from the test cases 
 
// use the printf statement as printf("Root = %8.6f\n", x1);
float h;
    for (itr=1; itr<=maxmitr; itr++)
    {
        h=f(x0)/df(x0);
        x1=x0-h;
        x0=x1;
    }
    printf("Root = %8.6f", x1);
    return 0;
}
float f(float x)
{
    return x*x*x - 2*x  - 3;
}
float df (float x)
{
    return 3*x*x-2;
}
   



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. 
#include <stdio.h>
int main()
{
    int a[100],i, n;
    scanf("%d",&n);  //Number of elements of the array is taken from the test case data 
   
   for (i=0; i<n; i++)
    {
        scanf("%d",a+i); // Input the array elements
    }
int j,t;
for (i=0; i<(n-1); i++) 
    {
        for (j=i+1; j<n; j++)
        {
            if (*(a+i)>*(a+j))
            {
                t=*(a+i);
                *(a+i)=*(a+j);
                *(a+j)=t;
            }
        }
    }
//   Printing sorted array in ascending order 
    for (i=0; i<n; i++)
    {
        printf("%d\n",*(a+i));
    }
    return 0;
   }
   



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. 
#include<stdio.h>
void sort(int *a, int n);
int main()
{
    int a[20];
    int n,i; 
    scanf("%d",&n); // Enter number of elements to sort is taken from test case data
   
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]); // The elements of the array is taken from the test data
    }
 
sort(a, n); // Calling the sorting function
 
    //Printing the sorted array 
    for(i=0;i<n;i++)
    {
        printf("%d\n",a[i]);
    }
   return 0;
}
void sort(int *a, int n)
{
    int i,temp,j;
    for(i=1;i<n;i++)
    {
        for(j=0;j<n-i;j++)
        {
           if(*(a+j)>=*(a+j+1))
        {
            temp = *(a+j);
            *(a+j)= *(a+j+1);
            *(a+j+1)= temp;
        }
        }
    }
}
   



Private Test cases used for EvaluationStatus
Test Case 1
Passed

No comments

File Uploading in PHP

  File Uploading in PHP PHP allow you to upload any type of a file i.e. image, binary or text files.etc..,PHP has one in built global variab...