“Array issue in C” would be translated as “C中的数组问题.”

huangapple go评论77阅读模式
英文:

Array issue in C

问题

我有一个C编程的大学项目。我在以下任务中遇到了问题。我的程序应该将数字排序在两个数组中。在第一个数组中,我必须保存(每五个元素中的最大值),这是我的问题。我不确定如何制作循环,读取五个元素进行比较,取出最大的元素,然后继续处理其他元素。我希望有人能帮助,因为我卡住了。

#define A 100

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>;

void show(int x[], int nx); // 用于显示数组的函数
float vavedi(int x[], int nx); // 手动输入数字并处理它们的函数
FILE* readFile(char* fname); // 读取文件并将其表示为数组的函数

int main()
{
    int call, a = 0, b = 0, mode = 0, i = 0;
    int check = 0;
    char fail[A];
    char* menu[] = {
        "程序已启动!",
        "输入选项:",
        "1:输入数字。",
        "2:从文件中选择。",
        "0:退出。"
    };

    do {
        for (i = 0; i < 5; i++)
            printf("%s\n", menu[i]);

        check = scanf("%d", &mode);

        if (check != 1)
            printf("错误!请重试!");

        switch (mode)
        {
        case 1: {
            // 在选项1中,从用户输入数字
            call = vavedi(a, b);
            break;
        }

        case 2: {
            // 在选项2中,用户使用现有文件
            printf("输入要打开的文件的路径:\n");
            scanf("%s", fail);
            call = readFile(fail);
            if (call == NULL) {
                printf("文件不存在!请重试!\n");
            }
            break;
        }
        case 0:
            break;

        default:
        {
            printf("错误!请重试!\n");
        }
        }

    } while (mode != 0);

    printf("\n程序已结束!\n");
    return 0;
}
void show(int x[], int nx)
{
    int k;
    for (k = 0; k < nx; k++)
    {
        printf("\n Element[%d]= %d", k, x[k]);
    }
}

float vavedi(int x[], int nx)
{
    int call = 0;
    int enter = 0;
    int imin, max;
    int b[A], c[A];
    int i, j, j1, count;
    j = 0;
    do {
        printf("\n元素数量:");
        scanf("%d", &count);
        if (count <= 0 || count > 100)
            printf("无效输入!请重试!\n");
    } while (count <= 0 || count > 100);

    for (i = 0; i < count; i++)
    {
        printf("\n输入一个元素:");
        scanf("%d", &c[i]);
    }


    printf("\n");
    return enter;
}

请注意,上述代码是翻译后的内容,不包括代码部分。如果您需要有关代码的进一步帮助或解释,请提出具体问题。

英文:

I have an university project in C programming. I ran into a problem with the following task. My program should order numbers in two arrays. In the first array i must save (the biggest of every fifth element) and that is my problem. I am not sure how to make the loop which reads five elements compare them, take the biggest one, and then continue doing this with the other elements. I am hoping someone to help because I blocked.

#define A 100
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;math.h&gt;
void show(int x[], int nx); //функция за показване на масивите
float vavedi(int x[], int nx); //функция, чрез която ръчно въвеждаме числата и ги обработваме
FILE* readFile(char* fname); //функция която чете файл и представя съдържанието му като масиви
int main()
{
int call, a = 0, b = 0, mode = 0, i = 0;
int check = 0;
char fail[A];
char* menu[] = {
&quot;PROGRAM STARTED!&quot;,
&quot;Enter an option:&quot;,
&quot;1 : Write the numbers.&quot;,
&quot;2 : Choose from a file.&quot;,
&quot;0 : Exit.&quot;
};
do {
for (i = 0; i &lt; 5; i++)
printf(&quot;%s\n&quot;, menu[i]);
check=scanf(&quot;%d&quot;, &amp;mode);
if (check != 1)
printf(&quot;ERROR! Try again!&quot;);
switch (mode)
{
case 1: {
//в случай 1 числата се въведждат от потребителя
call = vavedi(a, b);
break;
}
case 2: {
//в случай 2 потребителя използва съществуващ файл
printf(&quot;Enter the path of the file you want to open:\n&quot;);
scanf(&quot;%s&quot;, fail);
call = readFile(fail);
if (call == NULL) {
printf(&quot;The file doesn&#39;t exist! Try again!\n&quot;);
}
break;
}
case 0:
break;
default:
{
printf(&quot;ERROR! Try again!\n&quot;);
}
}
} while (mode != 0);
printf(&quot;\nThe program ended!\n&quot;);
return 0;
}
void show(int x[], int nx)
{
int k;
for (k = 0; k &lt; nx; k++)
{
printf(&quot;\n Element[%d]= %d&quot;, k, x[k]);
}
}
float vavedi(int x[], int nx)
{
int call=0;
int enter=0;
int imin, max;
int b[A], c[A];
int i, j, j1, count;
j = 0;
do {
printf(&quot;\nCount of the elements:&quot;);
scanf(&quot;%d&quot;, &amp;count);
if (count &lt;= 0 || count &gt; 100)
printf(&quot;Invalid input! Try again!\n&quot;);
} while (count &lt;= 0 || count &gt; 100);
for (i = 0; i &lt; count; i++)
{
printf(&quot;\nEnter an element:&quot;);
scanf(&quot;%d&quot;, &amp;c[i]);
} 
printf(&quot;\n&quot;);
return enter;
}

答案1

得分: 0

Update: 包括由 @pmg 提到的头文件。

如果我理解正确,你的问题是要找到每五个元素中的最大元素:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h> // 在你的头文件之后放置这个
..
.. // 代码的其余部分。
..
for (i = 0; i < count; i++)
{
   printf("输入一个元素:");
   scanf("%d", &c[i]);
}

size_t size = ceil(count/5.0);
memset(b, INT_MIN, size);
for(int i = 0; i < count; i++)
{
  b[i/5] = b[i/5] > c[i] ? b[i/5] : c[i];
}

// 像这样打印b:
for(i = 0; i < size; i++)
   printf("%d", b[i]);

让我们看看对于大小为 9 的数组,这是如何工作的。数组的索引将从 0 到 8。如果我将它们每个都除以 5,对于 i = 0 到 4,我得到 0,对于 i = 5 到 8,我得到 1。现在我们可以在每个 5 个元素的桶中取最大值。你似乎是个初学者,希望这有助于你更好地理解。

英文:

Update : Including header file mentioned by @pmg.

If i understand correctly, your problem is to find largest element for every five elements:

    #include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;math.h&gt;
#include &lt;limits.h&gt; // Put this after your header files
..
.. //Rest of the code.
..
for (i = 0; i &lt; count; i++)
{
printf(&quot;\nEnter an element:&quot;);
scanf(&quot;%d&quot;, &amp;c[i]);
} 
size_t size = ceil(count/5.0);
memset(b , INT_MIN , size);
for(int i = 0 ; i &lt; count ; i++)
{
b[i/5] = b[i/5] &gt; c[i] ? b[i/5] : c[i];
}
// Print b like this:
for(i = 0 ; i &lt; size; i++)
printf(&quot;%d&quot; , b[i] );       

Let's see how this works for array of size 9. array's indices will go from 0 to 8 . If I divide each of them by 5, I get 0 for i = 0 to 4 and 1 for i = 5 to 8. Now we can take max over elements in buckets of 5. You seem to be a beginner, hope this helps you in creating better understanding.

答案2

得分: 0

以下是翻译好的部分:

另一个问题如下:

FILE* readFile(char* fname)
{

    int c[A], b[A];
    int i = 0, j = 0, k = 0, num;
    FILE* fp= NULL; //文件指针
    fp = fopen(fname, "r");
    if (fp)
    {
        while (fscanf(fp, "%d", &c[i]) != EOF)
            i++;

        num = i;
        size_t size = ceil(num/5.0);
        memset(b , INT_MIN , size);
        for(i = 0 ; i < num ; i++)
        {
            b[i/5] = b[i/5] > c[i] ? b[i/5] : c[i];
        }
        printf("\n数组1:\n");
        for(i = 0 ; i < size; i++)
        {
            bubble(b, i);
            printf(" 元素[%d]=%1d\n", i, b[i]);
        }

        printf("\n数组2:");
        bubble(c, num);
        show(c, num);

        printf("\n");

    }
    fclose(fp);
    return fp;
}

它没有正确计算。
英文:

The other problem is the following:

{
int c[A], b[A];
int i = 0, j = 0, k = 0, num;
FILE* fp= NULL; //указател за файл
fp = fopen(fname, &quot;r&quot;);
if (fp)
{
while (fscanf(fp, &quot;%d&quot;, &amp;c[i]) != EOF)
i++;
num=i;
size_t size = ceil(num/5.0);
memset(b , INT_MIN , size);
for(i = 0 ; i &lt; num ; i++)
{
b[i/5] = b[i/5] &gt; c[i] ? b[i/5] : c[i];
}
printf(&quot;\nARRAY1:\n&quot;);
for(i = 0 ; i &lt; size; i++)
{
buble(b, i);
printf(&quot; Element[%d]=%1d\n&quot;, i, b[i]);
}
printf(&quot;\nARRAY2:&quot;);
buble(c, num);
show(c, num);
printf(&quot;\n&quot;);
}
fclose(fp);
return fp;
}
It doesn&#39;t calculate the rigth way.
</details>

huangapple
  • 本文由 发表于 2020年1月3日 22:44:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定