Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
Hi,
I have this code that I need to complete. It should generate 10 random snumbers and find the third smallest.
Any ideas?
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Description: 
 *                                                                             *
 * Written by *** for COMP9021                                                 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#define SIZE 10
#define BOUND 10
 
int main(int argc, char **argv) {
    /* We run the program with one number as command line argument. */
    if (argc != 2) {
        printf("Please provide 1 command line argument.\n");
        return EXIT_FAILURE;
    }
    /* We use it as a seed for the random number generator. */
    srand(strtoul(argv[1], NULL, 10));
    int data[SIZE];
    /* We randomly generate 10 numbers in the range [0, 10) and store them in data. */
    for (int i = 0; i < SIZE; ++i)
        data[i] = rand() % BOUND;
    int third_smallest_number = BOUND;
    /* ... REPLACE THIS COMMENT WITH YOUR CODE ... */
    for (int i = 0; i < SIZE; ++i)
        printf("%4d", data[i]);
    if (third_smallest_number == BOUND)
        printf("\n  does not contain three distinct numbers.\n");
    else
        printf("\n  has %d as third smallest number.\n", third_smallest_number);
    return EXIT_SUCCESS;
}
Posted 17-Mar-13 22:05pm
Edited 17-Mar-13 22:20pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
 
Try it yourself, you may find it is not as difficult as you think!
 
But this isn't a difficult task: Personally, I would sort the data into ascending or descending order, and then just read out the third smallest. There are other ways, but they are messy and difficult to maintain if the next question is "OK, so what's the 5th smallest?"
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mathew Soji 334
1 Maciej Los 275
2 BillWoodruff 260
3 Afzaal Ahmad Zeeshan 232
4 OriginalGriff 201
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 18 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100