Friday, December 4, 2020

Problem solving through Programming In C Session-1 Problem - 1 Programming Solution

 

Session-1 Problem - 1

Due on 2020-12-04, 11:00 IST
Write a program to accept 'n' (taken from test data) numbers of elements using dynamic memory allocation and then print the element at position 'p' (taken from test data) after sorting the elements in ascending order.
For example:
If the value of n is 5 and value of p is 3 then the program will accept five numbers as input from the test data, sort them in ascending order and display the third number as output.
If the input is 20,30,90,40,80 then it will display 40 as output.
Your last recorded submission was on 2020-12-04, 10:17 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
#include <malloc.h>
3
4
int main()
5
{
6
int *Array, n, p;
7
scanf("%d", &n); //Accepts total number of elements from test data
8
scanf("%d", &p); //Accepts which position to find
9
10
//Write the program to accept n elements and then print the element
11
//at position p after sorting in ascending order
12
//Use the printf statement as:
13
//printf("The desired element is %d", variable_name);
14
for(int i = 0;i<n;i++){
15
 scanf("%d",&Array[i]); 
16
}
17
18
for(int i = 0;i<n-1;i++){
19
  for(int j =0;j<n-i-1;j++){
20
    if (Array[j] > Array[j+1]) {
21
      int temp = Array[j];
22
        Array[j] = Array[j+1];
23
        Array[j+1] = temp;
24
    }
25
             
26
  }
27
}
28
29
printf("The desired element is %d",Array[p-1]);
30
return 0;
31
32
}

No comments:

Post a Comment