gpt4 book ai didi

c - 打印出指向序列中某个值的指针变量

转载 作者:太空宇宙 更新时间:2023-11-04 03:15:37 25 4
gpt4 key购买 nike

我被困在这个练习中,我无法在屏幕上正确打印“maxcount”和“mostnum”的值。

#include<stdio.h>
#include<stdlib.h>
#define size 100
void analyzeFrequencies(int sequence[], int* mostnum, int *maxcount, int
length)
{
int i=0, j=0, count=0;
for(i=0;i < length;i++)
{
for(j=i; j < length; j++)
{
if(sequence[i]==sequence[j])
{
count++;
}
}
if(count>=*maxcount)
{
maxcount=&count;
mostnum=&sequence[i];
}
}
}

int main(void)
{
int i, length;
int *mostnum, *maxcount;
int sequence[size]={};
char answer;

do
{
length=0;
for(i=0; i<size; i++)
{
printf("Enter numbers between 0-1000: ");
scanf("%d",&sequence[i]);
if(sequence[i]<0)
{
break;
}
else if(sequence[i]>1000)
{
printf("Wrong input, try again.\n");
}
length++;
}
analyzeFrequencies(sequence, mostnum, maxcount, length);
printf("The number %d occurs the most time, in total %d times\n", *mostnum, *maxcount);
printf("Do you want to enter another sequence (yes = y, no= n)? ");
scanf("%c",&answer);
}while(answer == 'y');

return 0;
}

我在这个练习中要做的是在屏幕上打印序列中出现次数最多的数字以及出现了多少次。当我必须使用指针执行此操作时,我的问题就出现了,因为我不确定我得到了什么并且没有任何意义。在这里你可以看到规则。


You​ ​should​ ​write​ ​a​ ​function​ ​​analyzeFrequencies()​​ ​that​ ​computes​ ​which​ ​of​ ​the integers​ ​that​ ​have​ ​occurred​ ​the​ ​most​ ​times​ ​in​ ​the​ ​sequence​ ​and​ ​how​ ​many​ ​times​ ​it​ ​has occurred.​ ​Since​ ​the​ ​function​ ​computes​ ​two​ ​different​ ​results,​ ​the​ ​function​ ​should​ ​take these​ ​results​ ​as​ ​pointer​ ​variables​ ​as​ ​arguments.

Rules​ ​for​ ​​analyzeFrequencies()​:

  • It​ ​should​ ​not​ ​return​ ​a​ ​value​ ​(the​ ​results​ ​are​ ​computed​ ​via​ ​pointers).
  • The​ ​function​ ​must​ ​take​ ​the​ ​​sequence​ ​entered​ ​by​ ​the​ ​user​​ ​as​ ​an​ ​array​ ​argument
  • The​ ​two​ ​computed​ ​values​ ​(number​ ​that​ ​occurs​ ​the​ ​most​ ​and​ ​number​ ​of occurrences)​ ​should​ ​be​ ​taken​ ​as​ ​pointer​ ​variable​ ​arguments.
  • The​ ​function​ ​may​ ​not​ ​take​ ​any​ ​further​ ​input​ ​from​ ​the​ ​user​ ​and​ ​may​ ​not​ ​print anything​ ​on​ ​the​ ​screen.

Other​ ​rules:

  • The​ ​sequence​ ​must​ ​be​ ​entered​ ​in​ ​​main()​​ ​and​ ​all​ ​printouts​ ​should​ ​also​ ​be​ ​from main()​.
  • After​ ​the​ ​sequence​ ​have​ ​been​ ​analysed,​ ​the​ ​user​ ​should​ ​be​ ​asked​ ​if​ ​he​ ​or​ ​she​ ​wants to​ ​enter​ ​a​ ​new​ ​sequence.

最佳答案

您误解了在这种情况下使用指针的原因。由于 C 语言只允许您从函数返回一项,因此练习改用指针在函数内部设置值。

这意味着您不应该在main 中声明指针。相反,您应该声明 int,并将应用 address-of & 运算符的结果传递给它们:

int mostnum, maxcount;
analyzeFrequencies(sequence, &mostnum, &maxcount, length);

analyzeFrequencies 中,您保留指针,并像这样分配:

if(count>=*maxcount)
{
*maxcount = count;
*mostnum = sequence[i];
}

现在,您在函数内分配的值是在您在 main 中声明的局部变量上执行的,因为您是通过指针传递它们的。

因为你在赋值之前读取了 analyzeFrequencies 中的 *maxcount,所以你必须在进入循环之前添加一个赋值,如下所示:

int i=0, j=0, count=0;
*maxcount = 0; // <<== Add this line
for(i=0;i < length;i++)

注意:您在代码中还有其他与传递指针无关的错误。例如,当你打印“输入错误,重试”时,你应该调整 i 再次指向相同的位置,否则错误的输出将保留在序列中。

关于c - 打印出指向序列中某个值的指针变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52243769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com