Problem Solving Through Programming In C Week10 Assignment July 2023

Problem-Solving-Through-Programming-In-C-Week10-Assignment-July-2023

NPTEL Problem Solving Through Programming In C Week10 All Programming Assignment Solutions | Swayam July 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!

Problem Solving through Programming In C July 2023

Question : 1 Write a C program to find the root of the equation using bisection method for different values of allowable error of the root.

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", 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);
}
				
			

Problem Solving through Programming In C July 2023

Question : 2  Write a C code to check if a 3 x 3 matrix is invertible. A matrix is not invertible if its determinant is 0.

				
					#include<stdio.h>
int main()
{
  int a[3][3], i, j;
  long determinant;
  //9 elements of matrix is taken as input from test data
  for(i = 0 ; i < 3; i++)
      for(j = 0; j < 3; j++)
           scanf("%d", &a[i][j]);
           
  determinant = a[0][0] * ((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -a[0][1] * (a[1][0]
   * a[2][2] - a[2][0] * a[1][2]) + a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]);
  if(determinant == 0)
       printf("The given matrix is not invertible");
  else
       printf("The given matrix is invertible");
  return 0;
}
  
				
			

Problem Solving through Programming In C July 2023

Question : 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); // 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;
}
				
			

Problem Solving through Programming In C July 2023

Question : 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);
   
    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;
           }
        }
    }
}