NPTEL Problem Solving Through Programming In C Jan-2024 Week 9


 

Week-09 Program-01

Due on 2024-03-28, 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.

Select the Language for this assignment. 
#include <stdio.h>
int main()
{
   int array[100], search, n, count = 0;
   //"search" is the key element to search and 'n' is the total number of element of the array
   // "count" is to store total number of elements
 
 scanf("%d", &n); //Number of elements is taken from test case
 
 int c;
   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);
 
   scanf("%d", &search); // The element to search is taken from test case
 
/* Use the printf statements as below:
"%d is present at location %d.\n"  for each locations
"%d is not present in the array.\n" if the element is not found in the list
"%d is present %d times in the array.\n"
*/
for (c = 0; c < n; c++)
    {
      if (array[c] == search)
      {
         printf("%d is present at location %d.\n", search, c+1);
         count++;
      }
    }
   if (count == 0)
      printf("%d is not present in the array.", search);
   else
      printf("%d is present %d times in the array.", search, count);
 
   return 0;
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
4
5
6
5
7
5
5 is present at location 1.\n
5 is present at location 3.\n
5 is present 2 times in the array.
5 is present at location 1.\n
5 is present at location 3.\n
5 is present 2 times in the array.
Passed
Test Case 2
5
67
80
45
97
100
50
50 is not present in the array.
50 is not present in the array.
Passed





Week-09 Program-02

Due on 2024-03-28, 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.
Select the Language for this assignment. 
#include <stdio.h>
int linear_search(int[], int, int);
int main()
{
   int array[100], search, c, n, position;
   /* search - element to search, c - counter, n - number of elements in array,
   position - The position in which the element is first found in the list. */
 
    scanf("%d", &n); // Number of elements in the array is read from the test case data
 
    for (c = 0; c < n; c++)
    scanf("%d", &array[c]); //Elements of array is read from the test data
 
    scanf("%d", &search);  //Element to search is read from the test case data
 
   /* Use the following in the printf statement to print the output
   printf("%d is not present in the array.\n", search);
   printf("%d is present at location %d.\n", search, position+1); //As array[0] has the position 1
   */
position = linear_search(array, n, search);
 
   if (position == -1)
      printf("%d is not present in the array.", search);
   else
      printf("%d is present at location %d.", search, position+1);
   return 0;
}
 
int linear_search(int a[], int n, int find) {
   int c;
   for (c = 0 ;c < n ; c++ )
    {
      if (a[c] == find)
         return c;
    }
   return -1;
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
78
90
34
54
98
90
90 is present at location 2.
90 is present at location 2.
Passed
Test Case 2
6
30
40
50
20
90
60
90
90 is present at location 5.
90 is present at location 5.
Passed





Week-09 Program-03

Due on 2024-03-28, 23:59 IST
Write a C program to search a given number from a sorted 1D array and display the position at which it is found using binary search algorithm. The index location starts from 1.
Select the Language for this assignment. 
#include <stdio.h>
int main()
{
 int c, n, search,
 array[100];
 scanf("%d",&n); //number of elements in the array
 
 for (c = 0; c < n; c++)
 scanf("%d",&array[c]);
 
 scanf("%d", &search); //The element to search is read from test case.
 
/* Use the printf statements as below:
 printf("%d found at location %d.\n", search, variable_name);
 printf("Not found! %d isn't present in the list.\n", search);
*/
int first, last, middle;
   first = 0;
   last = n - 1;
   middle = (first+last)/2;
 
   while (first <= last) {
      if (array[middle] < search)
         first = middle + 1;
      else if (array[middle] == search) {
         printf("%d found at location %d.", search, middle+1);
         break;
      }
      else
         last = middle - 1;
 
      middle = (first + last)/2;
      }
   if (first > last)
      printf("Not found! %d isn't present in the list.", search);
 
     return 0;
      }
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
10
20
30
40
50
40
40 found at location 4.
40 found at location 4.
Passed
Test Case 2
4
44
55
66
77
10
Not found! 10 isn't present in the list.
Not found! 10 isn't present in the list.
Passed




Week-09 Program-04

Due on 2024-03-28, 23:59 IST
Write a C program to reverse an array by swapping the elements and without using any new array.
Select the Language for this assignment. 
#include <stdio.h>
int main() {
  int array[100], n, c;
  scanf("%d", &n); // n is number of elements in the array.
  for (c = 0; c < n; c++) {
    scanf("%d", &array[c]);
  }
int temp, end;
   end = n - 1;
   for (c = 0; c < n/2; c++) {
    temp       = array[c];
    array[c]   = array[end];
    array[end] = temp;
 
    end--;
  }
printf("Reversed array elements are:\n");
 
  for (c = 0; c < n; c++) {
    printf("%d\n", array[c]);
  }
  return 0;
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
4
10
20
30
40
Reversed array elements are:\n
40\n
30\n
20\n
10
Reversed array elements are:\n
40\n
30\n
20\n
10\n
Passed after ignoring Presentation Error
Test Case 2
5
50
60
40
30
20
Reversed array elements are:\n
20\n
30\n
40\n
60\n
50
Reversed array elements are:\n
20\n
30\n
40\n
60\n
50\n
Passed after ignoring Presentation Error

No comments

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...