NPTEL Problem Solving Through Programming In C WEEK 10 Assignment 2023

NPTEL-Problem-Solving-Through-Programming-In-C-WEEK-10-Assignment-2023

NPTEL Problem solving through Programming In C Week 10 All Programming Assignment Solutions | Swayam 2023. With the growth of Information and Communication Technology, there is a need to develop large and complex software.

ABOUT THE COURSE:

This course is aimed at enabling the students to:
  • Formulate simple algorithms for arithmetic and logical problems
  • Translate the algorithms to programs (in C language)
  • Test and execute the programs and  correct syntax and logical errors
  • Implement conditional branching, iteration and recursion
  • Decompose a problem into functions and synthesize a complete program using divide and conquer approach
  • Use arrays, pointers and structures to formulate algorithms and programs
  • Apply programming to solve matrix addition and multiplication problems and searching and sorting problems 
  • Apply programming to solve simple numerical method problems, namely rot finding of function, differentiation of function and simple integration
COURSE LAYOUT
    • Week 1 : Introduction to Problem Solving through programs, Flowcharts/Pseudo codes, the compilation process, Syntax and Semantic errors, Variables and Data Types
    • Week 2 : Arithmetic expressions, Relational Operations, Logical expressions; Introduction to Conditional Branching
    • Week 3 : Conditional Branching and Iterative Loops
    • Week 4 : Arranging things : Arrays
    • Week 5 : 2-D arrays, Character Arrays and Strings 
    • Week 6 : Basic Algorithms including Numerical Algorithms
    • Week 7 : Functions and Parameter Passing by Value
    • Week 8 : Passing Arrays to Functions, Call by Reference
    • Week 9 : Recursion
    • Week 10 : Structures and Pointers
    • Week 11 : Self-Referential Structures and Introduction to Lists
    • Week 12 : Advanced Topics

Once again, thanks for your interest in our online courses and certification. Happy learning!

Course Name : “Problem Solving through Programming In C 2023

Program : 1  

Problem-Solving-through-programming-in-C-week10-Question1
				
					#include<stdio.h>
float fun (float x); 
void bisection (float *x, float a, float b, int *itr);

int main ()
{
    int itr = 0, maxmitr=10;
    float x, a=1.0, b=2.0, allerr, x1; 
   

    scanf("%f", &allerr);
    bisection (&x, a, b, &itr);
    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\n", 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);
}

				
			

Course Name : “Problem Solving through Programming In C 2023

Program : 2  

Problem-Solving-through-programming-in-C-week10-Question2
				
					#include<stdio.h>
float f(float x);
float df (float x);

int main()
{
    int itr, maxmitr; 
    float x0=1.0, x1;
    scanf("%d", &maxmitr);
    float h;
    for (itr=1; itr<=maxmitr; itr++)
    {
        h=f(x0)/df(x0);
        x1=x0-h;
        x0=x1;
    }
    printf("Root = %8.6f\n", x1);
    return 0;
}

float f(float x)
{
    return x*x*x - 2*x  - 3;
}

float df (float x)
{
    return 3*x*x-2;
}
				
			

Course Name : “Problem Solving through Programming In C 2023

Program : 3  Write a C program to sort a given 1D array using pointer in ascending order.

				
					#include <stdio.h>
int main()
{
    int a[100],i, n;
    scanf("%d",&n);
    for (i=0; i<n; i++)
    {
        scanf("%d",a+i);
    }
    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;
            }
        }
    }
    
    for (i=0; i<n; i++)
    {
        printf("%d\n",*(a+i));
    }
    return 0;
}


				
			

Course Name : “Problem Solving through Programming In C 2023

Program : 4  Write a C program to sort a 1D array using pointer by applying Bubble sort technique

				
					#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 ii,temp,j2;
    for(ii=1;ii < n;ii++)
    {
        for(j2=0;j2  < n-ii;j2++)
        {
           if(*(a+j2) >= *(a+j2+1))
            {
                temp = *(a+j2);
                *(a+j2)= *(a+j2+1);
                *(a+j2+1)= temp;
            }
        }
    }
}