Ask coding questions

← Back to all posts
AnakNgDiyos (2)

Write a program that sorts ten floating-point numbers in descending order. In this case, the data in the array is initialized (in the code
below). The algorithm is described in Chapter 4.3. After each execution of the inner loop, your program should print the values on the
screen (with another small loop). You can use the "%.2f" format in printf. Your version of the program must print the same result as the
expected output.

#include <stdio.h>
int main()
float numbers[10] = {5.6, 4.3, 6.2, 6.4, 7.3, 2.3, 8.3, 9.2, 0.1, 1.9};
/ your code /
return 0;

Example output
5.60 6.20 6.40 7.30 4.30 8.30 9.20 2.30 1.90 0.10
6.20 6.40 7.30 5.60 8.30 9.20 4.30 2.30 1.90 0.10
6.40 7.30 6.20 8.30 9.20 5.60 4.30 2.30 1.90 0.10
7.30 6.40 8.30 9.20 6.20 5.60 4.30 2.30 1.90 0.10
7.30 8.30 9.20 6.40 6.20 5.60 4.30 2.30 1.90 0.10
8.30 9.20 7.30 6.40 6.20 5.60 4.30 2.30 1.90 0.10
9.20 8.30 7.30 6.40 6.20 5.60 4.30 2.30 1.90 0.10
9.20 8.30 7.30 6.40 6.20 5.60 4.30 2.30 1.90 0.10
9.20 8.30 7.30 6.40 6.20 5.60 4.30 2.30 1.90 0.10

KelvinVerhey (39)

There are few problems you need to address.

  1. C has no way to expose length of arrays, or things that are array-like for instance strings. You must either use a function or #define a macro that can use sizeof() or statically code your length in a variable or hard code the limit inside of an expression such as a for loop.

  2. Basic understanding of stdio.h functions would be useful, but not required. You can refer here for a typical standardised include reference for it.

This will allow you to access the printf() function.

3a. For loops.
3b. While loops.
3c. Do/While loop.

  1. If statements for basic expression logic to test conditions for if you're going need to perform a swap of numbers.

That reference is quite crude, but will equip you with at least enough to complete the assignment.

  1. Writing a swap algorithm for the array of numbers so you can trade positions of numbers while you are looping across them to determine if you need to swap them to get your descending order.

  2. Operators of greater and less than to be able to figure out your expression. Depending how you write your if expression you will need one of those to get your correct sorting order.

  3. How arrays work.

This is an example sorting ascending.

You will still need to address your correct sorting order, and outputting the series of floating points numbers. It would be best to address your progress here instead of taking it offsite, so others can learn from your progress.

mwilki7 (786)

@KelvinVerhey this problem contains a constant-sized array so I don't think he should have trouble with that

mwilki7 (786)

What can we help you with?

AnakNgDiyos (2)

Help me to show the example output there. By using stdio.h only. And the other codes [email protected]

barrycarter (14)

I'm guessing you can't use the built in sort function, so it would be good to know what algorithm Chapter 4.3 describes

AnakNgDiyos (2)

Somebody told me that I should use "for" for [email protected]

barrycarter (14)

@AnakNgDiyos I think I'm going to need more than that. Do you know if you're doing bubble sort, quick sort, or anything?

barrycarter (14)

@AnakNgDiyos OK, I think I can help. Ping me here or on the Discord server in about 2 hours from now (11:30am MDT = 1730 UTC) -- you can make your multiplayer and we can talk using Discord's voice chat.

AnakNgDiyos (2)

Sorry but I can't online that time, because I'm busy at [email protected]

barrycarter (14)

@AnakNgDiyos OK, feel free to ping me anytime after about 1800 UTC on most days-- no promises, but, if I'm available, I should be able to help with this and future assignments.