Problem Solving Through Programming In C Week6 Assignment July 2023

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

NPTEL Problem solving through Programming In C Week6 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 Largest Element of an Integer Array. Here the number of elements in the array ‘n’ and the elements of the array is read from the test data.
Use the printf statement given below to print the largest element.

printf(“Largest element = %d”, largest);

				
					#include <stdio.h>
int main()
{
    int i, n, largest;
    int arr[100];

    scanf("%d", &n); /*Accepts total number of elements from the test data */

    for(i = 0; i < n; ++i)
    {
       scanf("%d", &arr[i]); /* Accepts the array element from test data */
    }
    
    largest = arr[0];
    for(i = 1; i < n; ++i)
    {
        if(largest < arr[i])
        largest = arr[i];
    }
    printf("Largest element = %d", largest);
    return 0;
}
				
			

Problem Solving through Programming In C July 2023

Question : 2 Write a C Program to print the array elements in reverse order (Not reverse sorted order. Just the last element will become first element, second last element will become second element and so on)
Here the size of the array, ‘n’ and the array elements is accepted from the test case data. The last part i.e. printing the array is also written.

You have to complete the program so that it prints in the reverse order.

				
					#include<stdio.h>
int main() {
    int arr[20], i, n;
    scanf("%d", &n); /* Accepts the number of elements in the array */

    for (i = 0; i < n; i++) 
        scanf("%d", &arr[i]);
     
    int j, temp;  
    j = i - 1;   // last Element of the array
    i = 0;       // first element of the array
 
    while(i < j)
    {
      temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
      i++;             
      j--;        
    }
  
    for(i = 0; i < n; i++) {
      printf("%d\n", arr[i]); // For printing the array elements 
    }
    return (0);
}
				
			

Problem Solving through Programming In C July 2023

Question : 3  Write a C program to read Two One Dimensional Arrays of same data type (integer type) and merge them into another One Dimensional Array of same type.

				
					#include<stdio.h>
int main() 
{
   int arr1[20], arr2[20], array_new[40], n1, n2, size, i;
 
   scanf("%d", &n1); //Get the size of first array from test data and store it in n1.
   
   for (i = 0; i < n1; i++)
      scanf("%d", &arr1[i]); //Accepts the values for first array 
 
   scanf("%d", &n2); //Get the size of second array from test data and store it in n2.
   
   for (i = 0; i < n2; i++)
      scanf("%d", &arr2[i]);
      
    int j;
    for(i=0;i<n1;++i)
        array_new[i]=arr1[i];
    
    size =  n1 + n2;
    
    for(i=0, j=n1; j<size && i<n2; ++i, ++j)
        array_new[j] = arr2[i];
        
        //Printing after merging

    for(i = 0; i < size; i++) {
      printf("%d\n", array_new[i]);
   }

}
				
			

Problem Solving through Programming In C July 2023

Question : 4 Write a C Program to delete duplicate elements from an array of integers.

				
					#include<stdio.h>
int main() 
{
   int array[50], i, size;
 
   scanf("%d", &size); /*Accepts the size of array from test case data */

   for (i = 0; i < size; i++)
   scanf("%d", &array[i]);
   
   int j, k;
   for(i = 0; i < size; i++) {
        for(j = i + 1; j < size;) {
            if(array[j] == array[i]) {
                for (k = j; k < size; k++) {
                   array[k] = array[k + 1];
                }
                size--;
             } else
                j++;
         }
    }
       
    for (i = 0; i < size; i++) {
      printf("%d\n", array[i]);
   }
}