I’m writing a simple program that finds the perfect numbers up to a given range. Here’s what I have:
#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]);
}
}
However, when I try to compile the program, I keep getting error: expression must have integral type error for almost all the operations in the Compute() method. It works fine for integer data types, but not for double. I’m using Intel C Compiler. Any ideas why the compiler is complaining?
You cannot create an array with a floating point size
the argument must be an integer. Imagine if
rangewas 2.5 – C won’t allow an array of 2.5 doubles. An array must have an integer number of elements