Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c-使用passByValue对二十个整数进行排序_C_Sorting_Pass By Value - Fatal编程技术网

c-使用passByValue对二十个整数进行排序

c-使用passByValue对二十个整数进行排序,c,sorting,pass-by-value,C,Sorting,Pass By Value,所以我有一个家庭作业,我有二十个硬编码整数,每个整数都是随机数。有些可能是相同的。使用passByValue,我需要打印三个最高的数字 现在我已经计划好了。这些数字将在main中声明,每个数字将被发送到一个新函数,可能称为sort。这里我有三个整数,取两个数值中较高的一个,或者每个点中已经存在的数值,然后将两个数值中较大的一个分配给该整数,并在该函数中前进到另外两个 不过,我有两个问题。问题一是主要问题。 作业说明明确说明我不能使用数组。所以我所能想象的只是20个不同的整数,每个都有不同的值。但

所以我有一个家庭作业,我有二十个硬编码整数,每个整数都是随机数。有些可能是相同的。使用passByValue,我需要打印三个最高的数字

现在我已经计划好了。这些数字将在main中声明,每个数字将被发送到一个新函数,可能称为sort。这里我有三个整数,取两个数值中较高的一个,或者每个点中已经存在的数值,然后将两个数值中较大的一个分配给该整数,并在该函数中前进到另外两个

不过,我有两个问题。问题一是主要问题。 作业说明明确说明我不能使用数组。所以我所能想象的只是20个不同的整数,每个都有不同的值。但是,这里的问题是,如果我使用passByValue,我不能同时对它们进行排序。也许我可以,但它看起来不漂亮,而漂亮正是老师想要的

问题一是如何有效地将每个int逐个传递给sort函数

问题二:在完成排序之前,我不希望打印函数打印出三个最大的数字。我能想象这种工作方式的唯一方法是,在main中的int行用完之前,以某种方式保存发送到print函数的值。但我不确定这是否可能

显然,我不是在寻找完整的代码。我真正想要的只是一个方向。我是否可以在main和sort函数或结构中使用某种形式的递归,尽管我不知道我的老师是否会喜欢我使用它们,因为它们类似于数组或其他东西

#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);
}
。。。您可能希望在提交代码之前更改某些标识符。。。或者不是


如果CannotUseArrays为true,则很难使用printf,因为它的格式通常是char数组


说得好。处理好了。

您可以很容易地创建一个无阵列、无递归的可伸缩解决方案。i、 e.您可以在不需要额外资源的情况下将20更改为20000

下面是一些示例伪代码:

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

可以对其进行重构以支持a1=13、a2=42…a20=666。你最大的vals变成了globals。if num>第三大值被包装在一个函数中,每个变量调用一次。

使用Bose Nelson算法计算20个整数的数量,我创建了一个简单的函数,该函数对20个整数进行排序,并使用名称索引。然后你只需要返回3个最大的整数

#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;
}
您可以使用任何变量名称,只需稍微修改宏,并将交换调用中的每个数字替换为等效名称


在线版可以在。

上找到,也许我遗漏了你的一些解释,但我想你可以这样做

// 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();
}

我真的不明白这项作业的意义,也许你可以问你的老师,让我们知道……

如果不能使用数组是真的,那么使用printf是很困难的,因为它的格式通常是字符数组。我有二十个硬编码的整数,听起来确实像一个数组。建议发布你尝试过的内容,否则内容太宽泛且不清楚。从你的命名开始:你实际上并没有尝试对任何内容进行排序——你在寻找3个最大的数字。你会惊讶地发现,你的想法可能会受到限制,因为你选择了排序的名称。你想解决的是一个排序问题,而不是手头的问题。我需要打印三个最高的数字。如果有关系,你的老师会考虑It*List= MalCux*siZeOfint,然后使用数组来*+list?使用排序网络来展开排序算法,使用20个整数,而不用对它们进行索引,然后打印3个最高值。我有二十个硬编码。ints@Swordfish如果是硬编码的,只需打印答案。显然OP并不是在寻找这个答案,这个答案增加了价值。因为我们知道这是一个家庭作业,所以我们被告知不要使用数组,像这样在数组中鬼鬼祟祟似乎不安全。另外:如果我戴上语言律师帽,结构中的元素是否保证是连续的?我不这么认为。我现在明白为什么要使用pragma pack1了-来规避不能使用数组的问题。@chux是的,我错过了;-尽管我的观点现在更加坚定了——如果一个看起来像初学者的课程的解决方案依赖于一个pragma,那么它对我来说就有点可疑了……@John3136当然是它的-坚持让我打开我的抓痕和嗅探监视器…这到底是什么意思?在编译时甚至不排序。你刚刚展开了循环,这也是用手做的。@AjayBrahmakshatriya这是一个有效的答案。它在不使用数组的情况下对20个值进行排序,编译时不需要排序,而且它使用了一种非常有用的技术。@AjayBrahmakshatriya其要点是对20个通过值传递的整数进行排序,只需使用20个不同的整数,明确要求不使用数组,然后在排序后不打印它们,但是回到打电话的人。这是 这个问题。通过展开循环,您可以有效地为变量命名任何名称,因为只剩下一些简单的赋值v1=maxv1,v2;v2=minv1,v2;。这个问题不涉及在编译时对它们进行排序,这个函数也不会这样做。
// 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();
}