英文:
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 <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[] = {
"PROGRAM STARTED!",
"Enter an option:",
"1 : Write the numbers.",
"2 : Choose from a file.",
"0 : Exit."
};
do {
for (i = 0; i < 5; i++)
printf("%s\n", menu[i]);
check=scanf("%d", &mode);
if (check != 1)
printf("ERROR! Try again!");
switch (mode)
{
case 1: {
//в случай 1 числата се въведждат от потребителя
call = vavedi(a, b);
break;
}
case 2: {
//в случай 2 потребителя използва съществуващ файл
printf("Enter the path of the file you want to open:\n");
scanf("%s", fail);
call = readFile(fail);
if (call == NULL) {
printf("The file doesn't exist! Try again!\n");
}
break;
}
case 0:
break;
default:
{
printf("ERROR! Try again!\n");
}
}
} while (mode != 0);
printf("\nThe program ended!\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("\nCount of the elements:");
scanf("%d", &count);
if (count <= 0 || count > 100)
printf("Invalid input! Try again!\n");
} while (count <= 0 || count > 100);
for (i = 0; i < count; i++)
{
printf("\nEnter an element:");
scanf("%d", &c[i]);
}
printf("\n");
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h> // Put this after your header files
..
.. //Rest of the code.
..
for (i = 0; i < count; i++)
{
printf("\nEnter an element:");
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];
}
// Print b like this:
for(i = 0 ; i < size; i++)
printf("%d" , 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, "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("\nARRAY1:\n");
for(i = 0 ; i < size; i++)
{
buble(b, i);
printf(" Element[%d]=%1d\n", i, b[i]);
}
printf("\nARRAY2:");
buble(c, num);
show(c, num);
printf("\n");
}
fclose(fp);
return fp;
}
It doesn't calculate the rigth way.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论