0% found this document useful (0 votes)
3 views

WEEK 5

The document outlines a C program that generates 100 real numbers between 10.0 and 20.0 and sorts them in descending order. It includes software requirements, an algorithm, source code, and instructions for compilation and execution. The program uses nested loops to compare and swap elements to achieve the desired order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

WEEK 5

The document outlines a C program that generates 100 real numbers between 10.0 and 20.0 and sorts them in descending order. It includes software requirements, an algorithm, source code, and instructions for compilation and execution. The program uses nested loops to compare and swap elements to achieve the desired order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

WEEK 5

Problem Statement:

Write a Program, which generate 100 real numbers in the range of 10.0 to 20.0,
and sort them in descending order.

Aim:

Write a C Program which generate 100 real numbers and sort them in
descending order.

Software Requirements:

Operating system: Windows 11

Dev C++ 5.6

Compiler Version: TDM-GCC 4.8.1, 64-bit debug.

Description:

In this program, we need to sort the given array in descending order such that
elements will be arranged from largest to smallest. This can be achieved
through two loops. Outer loop will select an element and inner loop allow us to
compare selected element with rest of the elements.

Original array:

Array after sorting:

Elements will be sort in such a way that largest element will appear on extreme
left which in this case is 8. Smallest element will appear on extreme right which
in this case is 1.

Algorithm:

Step 1: Start

Step 2: Create an array of fixed size (maximum capacity),


1
Step 3: Take n, a variable which stores the number of elements of the array, less
than maximum capacity of array.

Step 4: Iterate for loop to take array elements as input, and print them.

Step 5: The array elements are in unsorted fashion, to sort them, make a nested
loop.

Step 6: In the nested loop, the each element will be compared to all the
elements below it.

Step 7: In case the element is smaller than the element present below it, then
they are interchanged

Step 8: After executing the nested loop, we will obtain an array in descending
order arranged elements.

Step 9: Stop

Flow Chart:

Source Code:

#include <stdio.h>
#include<conio.h>
int main ()
{

int number[30];

int i, j, a, n;
printf("Enter the value of N\n");
scanf("%d", &n);

printf("Enter the numbers \n");


for (i = 0; i < n; ++i)
scanf("%d", &number[i]);

/* sorting begins ... */

for (i = 0; i < n; ++i)


{
for (j = i + 1; j < n; ++j)
{

2
if (number[i] < number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}

printf("The numbers arranged in descending order are given below\n");

for (i = 0; i < n; ++i)


{
printf("%d\n", number[i]);
}

Save the file: Sort.c

Compiler (F9): Click on F9 to compile the program which helps us to find the
errors so that the file extends to Sort.exe file name.

Run (F10): After compilation of the program click on F10 to run the program
and gives output.

Output:

Enter the value of N


10
Enter the numbers
23
45
56
4
10
58
99
89
70
65
The numbers arranged in descending order are given below
99

3
89
70
65
58
56
45
23
10
4

--------------------------------
Process exited after 46.72 seconds with return value 10
Press any key to continue . . .

You might also like