Problem Solving Through Programming in C Week 9: Programming Assignments Jan-Jun 2022

  NPTEL PROGRAMMING ASSIGNMENTS


Problem Solving Through Programming in C

Week-09 Program-01

Due Date of Submission 2022-03-31, 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.


#include <stdio.h>
int main()
{
   int array[100], search, n, count = 0;
 scanf("%d", &n);
int c;
   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);
   scanf("%d", &search);

 

 


Week-09 Program-02

Due Date of Submission 2022-03-31, 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.

#include <stdio.h>
int linear_search(int[], int, int);
int main()
{
   int array[100], search, c, n, position;
    scanf("%d", &n);
    for (c = 0; c < n; c++)
    scanf("%d", &array[c]);
    scanf("%d", &search);

 

 



Week-09 Program-03

Due Date of Submission 2022-03-31, 23:59 IST
Write a C program to marge two given sorted arrays (sorted in ascending order).
The code for input and output is already written. You have to write the merge function so that the merged array is displayed in ascending order.

#include <stdio.h>
void merge(int a[], int m, int b[], int n, int sorted[]);
int main() 
{
int a[100], b[100], m, n, c, sorted[200];
scanf("%d", &m); 
  for (c = 0; c < m; c++) 
    {
    scanf("%d", &a[c]); 
    }
  scanf("%d", &n);
  for (c = 0; c < n; c++) 
  {
    scanf("%d", &b[c]); 
  }
 merge(a, m, b, n, sorted); 
  printf("Sorted array:\n");
  for (c = 0; c < m + n; c++) {
    printf("%d\n", sorted[c]);
  }
  return 0;
}

 

 



Week-09 Program-04

Due Date of Submission 2022-03-31, 23:59 IST
Write a C program to reverse an array by swapping the elements and without using any new array.

#include <stdio.h>
int main() 
 {
  int array[100], n, c;
  scanf("%d", &n);
  for (c = 0; c < n; c++) 
  {
    scanf("%d", &array[c]);
  }

 

 


Post a Comment (0)
Previous Post Next Post