在C中查找完美数-编译错误

问题描述 投票:0回答:2

我正在编写一个简单的程序,该程序可以找到给定范围内的理想数字。这是我所拥有的:

#include<sys/types.h>
#include<sys/time.h>
#include<time.h>
#include<errno.h>
#include<fcntl.h>
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<strings.h>    
#include<unistd.h>


void Compute(double range);

int main(int argc, char** argv[])
{
  double range = 40000000;

  printf("range: %f\n", range);

  Compute(range);
}

void Compute(double range)
{
  double numbers[range];

  double total = 0;
  double sum = 0;
  double num;
  double j;

  for(num = 1; num < range; num++){
    sum = 0;

    for(j = 1; j < num; j++){
      if((num % j) == 0){
        sum+=j;
      }
    }

    if(sum == num){
      numbers[total] = sum;
      total++;
    }

  } 

  printf("Total: %f\n", total);

  for(j = 0; j < total; j++){
    printf("%f \n", numbers[j]);
  }

}

但是,当我尝试编译程序时,对于error: expression must have integral type方法中的几乎所有操作,我总是收到Compute()错误。它适用于整数数据类型,但不适用于double。我正在使用Intel C编译器。关于编译器抱怨的任何想法?

c compiler-errors double
2个回答
7
投票

您不能创建具有浮点大小的数组


1
投票

您的数组:

© www.soinside.com 2019 - 2024. All rights reserved.