-2

So I got a homework assignment where I have twenty hardcoded ints, each a random number. Some can be the same. Using passByValue, I need print the three highest numbers.

Now I already have this planned out. The numbers will be declared in main, each number will be sent to a new function, probably called sort. There I'd have three ints that will take whichever number is higher (the number being passed through, or the number already in each spot) and then assign the bigger of the two to that int, progressing to the other two in that function.

I have two problems, though. Problem one is in main. The homework instructions explicitly state that I cannot use arrays. So all I'm able to imagine is just simply twenty different ints, each with a different value. The problem here is, though, I can't really just sort them all at the same time if I'm utilizing passByValue. Or perhaps I can, but it wouldn't look pretty, and pretty is what the teacher is looking for.

So problem one, is how do I effectively pass each of the ints to the sort function one-by-one?

Problem two: I don't want the print function to print out the three largest numbers until it is done sorting. The only way I can imagine this working, is somehow holding the values from being sent to the print function until the line of ints in main has run out. But I'm not sure if that is possible...

Obviously I'm not looking for the entire code. All I really am looking for is just a direction to follow. Whether I can utilize some form of recursion both in the main and sort function or structs (though I don't know if my teacher would appreciate me using those, since they're similar to arrays) or something else.

DFT95
  • 27
  • 5
  • If "cannot use arrays" is true, it is hard to use `printf()` given its format is usually a `char` array. "I have twenty hardcoded ints" certainly sounds like an array. Recommend to post what you have tried, else this is too broad and unclear. – chux - Reinstate Monica Oct 17 '18 at 22:27
  • Start with your naming: You aren't actually trying to sort anything - you're looking for the 3 biggest numbers. You'd be surprised how much your thoughts may be limited because you have picked the name `sort`. You're trying to solve a sorting problem not the problem at hand. – John3136 Oct 17 '18 at 22:30
  • `I need print the three highest numbers.` what if there are ties? – wildplasser Oct 17 '18 at 22:31
  • Are you not allowed to use arrays at all? Or only in the main? Or only in the function that "sorts"? – DYZ Oct 17 '18 at 22:33
  • @chux `int a0=1,a1=10,a2=-1,...,a19=77;` is twenty hardcoded ints and yet not an array at all. – DYZ Oct 17 '18 at 22:35
  • 1
    Does your teacher consider `int *list = malloc(n * sizeof(int))` and then `*(list+x)` using an array? – Jongware Oct 17 '18 at 22:36
  • @wildplasser If there's a duplicate, it'll stay the way it is. – DFT95 Oct 17 '18 at 22:43
  • 4
    Use a sorting network to unfold sorting algorithm using 20 integers without indexing them, and then print 3 highest. – KamilCuk Oct 17 '18 at 23:05
  • It sounds more like you just need to read the integer values from `stdin` (or as arguments to the program) and then simply keep track of the top three highest and print them out in order... – David C. Rankin Oct 18 '18 at 00:22
  • @chux It is not my post. – DYZ Oct 18 '18 at 02:56
  • DFT95, with a [mcve] example that code issue would have been apparent and may you post easier to understand, more interesting and useful to answer. With seeing your code, the post will attract down votes and close votes. Re-consider and post your code. – chux - Reinstate Monica Oct 18 '18 at 03:50

4 Answers4

2

You can make a scalable solution with no arrays and no recursion quite easily. i.e. You can change 20 to 20,000 with no addition resources required.

Here is some sample pseudo code:

for(int i = 0; i < 20; i++)
{
    // TODO
    // -1 is a sample start value. You can find a better one
    int biggest = -1;
    int second_biggest = -1;
    int third_biggest = -1;
    int num = generate_random_num();
    if (num > third_biggest)
    {
        // TODO
        // check against second_biggest and biggest too
        // set one of the variables but remember: if you set biggest, what was
        // in biggest is now second_biggest and the old second_biggest is now
        // third_biggest
    }
    // TODO
    // Print the 3 results

This can be refactored to support a1=13, a2=42...a20=666. Your biggest vals become globals. Your if (num > third_biggest) gets wrapped in a function that you call once for each variable.

John3136
  • 28,809
  • 4
  • 51
  • 69
2

Using the Bose-Nelson Algorithm for the number of 20 integers I have created a simple function which sorts 20 integers indexed with names, used this site. Then you just need to return 3 biggest integers.

#include <stdio.h>

struct max3_from_20_s {
    int first;
    int second;
    int third;
} max3_from_20(
    int v0,
    int v1,
    int v2,
    int v3,
    int v4,
    int v5,
    int v6,
    int v7,
    int v8,
    int v9,
    int v10,
    int v11,
    int v12,
    int v13,
    int v14,
    int v15,
    int v16,
    int v17,
    int v18,
    int v19
) {
#define SORT20_MIN(a, b)  ((a<b)?a:b)
#define SORT20_MAX(a, b)  ((a<b)?b:a)
#define SORT20_SWAP(idx1, idx2)  do{ \
        const int min = SORT20_MIN(v##idx1, v##idx2); \
        const int max = SORT20_MAX(v##idx1, v##idx2); \
        v##idx1 = min; \
        v##idx2 = max; \
    }while(0)
#define swap SORT20_SWAP

    swap(0, 1);
    swap(3, 4);
    swap(5, 6);
    swap(8, 9);
    swap(10, 11);
    swap(13, 14);
    swap(15, 16);
    swap(18, 19);
    swap(2, 4);
    swap(7, 9);
    swap(12, 14);
    swap(17, 19);
    swap(2, 3);
    swap(1, 4);
    swap(7, 8);
    swap(6, 9);
    swap(12, 13);
    swap(11, 14);
    swap(17, 18);
    swap(16, 19);
    swap(0, 3);
    swap(5, 8);
    swap(4, 9);
    swap(10, 13);
    swap(15, 18);
    swap(14, 19);
    swap(0, 2);
    swap(1, 3);
    swap(5, 7);
    swap(6, 8);
    swap(10, 12);
    swap(11, 13);
    swap(15, 17);
    swap(16, 18);
    swap(9, 19);
    swap(1, 2);
    swap(6, 7);
    swap(0, 5);
    swap(3, 8);
    swap(11, 12);
    swap(16, 17);
    swap(10, 15);
    swap(13, 18);
    swap(1, 6);
    swap(2, 7);
    swap(4, 8);
    swap(11, 16);
    swap(12, 17);
    swap(14, 18);
    swap(0, 10);
    swap(1, 5);
    swap(3, 7);
    swap(11, 15);
    swap(13, 17);
    swap(8, 18);
    swap(4, 7);
    swap(2, 5);
    swap(3, 6);
    swap(14, 17);
    swap(12, 15);
    swap(13, 16);
    swap(1, 11);
    swap(9, 18);
    swap(4, 6);
    swap(3, 5);
    swap(14, 16);
    swap(13, 15);
    swap(1, 10);
    swap(2, 12);
    swap(7, 17);
    swap(4, 5);
    swap(14, 15);
    swap(3, 13);
    swap(2, 10);
    swap(6, 16);
    swap(8, 17);
    swap(4, 14);
    swap(3, 12);
    swap(5, 15);
    swap(9, 17);
    swap(8, 16);
    swap(4, 13);
    swap(3, 11);
    swap(6, 15);
    swap(9, 16);
    swap(4, 12);
    swap(3, 10);
    swap(7, 15);
    swap(4, 11);
    swap(8, 15);
    swap(7, 12);
    swap(4, 10);
    swap(9, 15);
    swap(6, 11);
    swap(8, 13);
    swap(5, 10);
    swap(9, 14);
    swap(8, 12);
    swap(6, 10);
    swap(9, 13);
    swap(8, 11);
    swap(9, 12);
    swap(7, 10);
    swap(9, 11);
    swap(8, 10);
    swap(9, 10);
#undef swap

    struct max3_from_20_s ret = {
        v19, v18, v17,
    };
    return ret;
}

int main()
{
    struct max3_from_20_s num = max3_from_20(
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
        20, 19, 18, 17, 100, 15, 14, 13, 12, 11
    );
    printf("%d %d %d\n", num.first, num.second, num.third);

    return 0;
}

will output:

100 20 19

You can use any variables names, just slightly modify the macros and replace each number within the swap call to the equivalent name.

Live version available at onlinegdb.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    What is the exact point of this? Doesn't even sort during compile time. You just unrolled the loops, that too by hand. – Ajay Brahmakshatriya Oct 18 '18 at 00:36
  • 1
    @AjayBrahmakshatriya This is a valid answer. It sorts 20 values without using an array, sorting during compile time wasn't a requirement, and it uses a [Sorting Network](https://en.wikipedia.org/wiki/Sorting_network) (a very useful technique). – Blastfurnace Oct 18 '18 at 03:05
  • @AjayBrahmakshatriya The point of this is to sort 20 integers passed by value using simply 20 different ints with the explicit requirement not to use an array, then after the sort not printing them, but returning to the caller. This was the question. By unrolling loops, you can effectively name your variables anything, because your are left with just a number of simple assignments `v1 = max(v1, v2); v2=min(v1, v2);`. The question didn't involve sorting them on compile-time, nor this function does that. – KamilCuk Oct 18 '18 at 05:36
0
#include <stdlib.h>
#include <stdio.h>

#pragma pack(1)
typedef struct stupid_assignment_data_tag {
    int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t;
} stupid_assignment_data_t;

int cmp(void const *a, void const *b)
{
    if(*(int*)a > *(int*)b) return  1;
    if(*(int*)a < *(int*)b) return -1;
    return 0;
}

void stupid_assignment_function(stupid_assignment_data_t stupid_assignment_data)
{
    qsort(&stupid_assignment_data, 20, sizeof(int), cmp);

    struct { char a, b, c, d, e, f, g, h, i, j, k; }
        stupid_format_string = { '%', 'd', ' ', '%', 'd', ' ', '%', 'd', '\0' };

    printf((char const*)&stupid_format_string, stupid_assignment_data.r, stupid_assignment_data.s, stupid_assignment_data.t);
}

int main(void)
{
    stupid_assignment_data_t stupid_assignment_data{
        2, 45, 765, 345, 15, 56, 3, 47, 457, 1235, 234,
        324, 234, 56346, 567, 4, 457, 4356, 24, 42 };

    stupid_assignment_function(stupid_assignment_data);
}

... you might want to change some of the identifiers before you hand in the code ... or not.

If "cannot use arrays" is true, it is hard to use printf() given its format is usually a char array.

Good point. Took care of that.

Swordfish
  • 12,971
  • 3
  • 21
  • 43
0

Maybe I'm missing something of your explanaition, but I guess you could do something like this...

// Globals to store the highest numbers. Initialize them to 0 considering the values are all positive.
int high_num_1 = 0;
int high_num_2 = 0;
int high_num_3 = 0;

void look_for_highest_numbers(int new_value)
{
    // You can put the code to get the highest values here
    // and assign them to the global ints declared above
}

void print_highest(void)
{
    // print the highest numbers
}

void main(void)
{
    // very stupid way of extracting the highest values from 20 ints
    look_for_highest_numbers(100);
    look_for_highest_numbers(42);
    ... // dots are to show that here you place another 17 calls to look_for_highest_numbers()
    look_for_highest_numbers(12345);
    // print solution
    print_highest();
}

I really don't see the point of this assignment, maybe you can ask your teacher and let us know...

Marcos
  • 139
  • 1
  • 6