My program is outputting me 2 random numbers out of 10, the other 8 are normal, but i can't see the problem

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

My program is outputting me 2 random numbers out of 10, the other 8 are normal, but i can't see the problem

问题

我的程序应该检查用户选择的数字是否是他们可用的数字(35-44),然后返回这些数字(35-44)以及选择它们的人占可用的10个数字的百分比。

我尝试了一切都像正常情况一样,但是数字35和36每次都给我随机的百分比数字。
数字37-44给我正常的百分比数字,所以我真的不明白发生了什么。

输出应该是这样的:

tamanho 36 – percentual: 20
tamanho 37 – percentual: 10
tamanho 38 – percentual: 0
tamanho 39 – percentual: 0
tamanho 40 – percentual: 0
tamanho 41 – percentual: 0
tamanho 42 – percentual: 10
tamanho 43 – percentual: 0
tamanho 44 – percentual: 30
英文:

My program is supposed to see if the user numbers are the ones they have available (35-44) and then return the numbers (35-44) and the percentage of people who have selected them out of the 10 numbers available.

I've tried everything as if it was normal, but the numbers 35 and 36 are giving me random percentage numbers every time.
The numbers 37-44 are giving me normal percentage numbers so I really don't understand what's happening

The output is supposed to be like that:

tamanho 36 – percentual: 20
tamanho 37 – percentual: 10
tamanho 38 – percentual: 0
tamanho 39 – percentual: 0
tamanho 40 – percentual: 0
tamanho 41 – percentual: 0
tamanho 42 – percentual: 10
tamanho 43 – percentual: 0
tamanho 44 – percentual: 30
#include <stdio.h>
int tamanVeri(int taman,int vTam[]){
  int porc,i;
  for(i=0;i<10;i++){
    if(taman==vTam[i]){
      return i;
    }
  }
  return 0;
}
void showTamanPorc(int vTam[],int vAp[],int vezRepetCSucesso){
  int i;
  float porc;
  for(i=0;i<10;i++){
    porc=((float)vAp[i]/(float)vezRepetCSucesso)*100.0;
    printf("Tamanho:  %i - Porcentagem: %.2f\n",vTam[i],porc);
  }
  
}
int main(void) {
  int vAp[10];
  int vTam[10]={35,36,37,38,39,40,41,42,43,44};
  int tamanho,i=0,j=0,k,adicEm;
  printf("Ponha o tamanho do tênis\nEntre 35 e 44\n");
  scanf("%i",&tamanho);
  while(tamanho!=0&&tamanho>0){
    adicEm=tamanVeri(tamanho,vTam);
    if(adicEm!=0){
      vAp[adicEm]++;
    }
    j++;
    printf("Ponha o tamanho do tênis\nEntre 35 e 44\n");
    scanf("%i",&tamanho);
  }
  showTamanPorc(vTam,vAp,j);
  return 0;
}

答案1

得分: 3

你的线性搜索函数(tamanVeri)存在问题,失败时应返回0,而成功找到整数时也返回数组的第一个元素。在失败时应返回一些无效的索引(例如 -1),并更改比较方式:

if (adicEm != -1)

int vAp[10]; 是一个未初始化的数组。它包含的值是不确定的(实际上是随机的)。使用不确定的值,如 vAp[adicEm]++;,会引发未定义行为

将数组初始化为全部为零:

int vAp[10] = { 0 };
英文:

Your linear search function (tamanVeri) has the problem of returning 0 on failure and on successfully finding the integer to be the first element of the array. You should return some non-valid index on failure (e.g., -1), and change the comparison used:

if (adicEm != -1)

int vAp[10]; is an uninitialized array. The values it contains are indeterminate (effectively random). Using an indeterminate value, as in vAp[adicEm]++;, invokes Undefined Behaviour.

Initialize the array to contain all zeroes:

int vAp[10] = { 0 };

huangapple
  • 本文由 发表于 2023年6月12日 23:58:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458388.html
匿名

发表评论

匿名网友

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

确定