In a function I placed the int array: factor[ g + 1] . g being input from the standard input but the program makes the array into this: factor[-1]. By the way I'm using xCode on mac. Why does this happen?

bool PerfectCalculator(int g)
{
	bool x;
	int i, factor[g + 1], sum;
	
	x = false;
	sum = 0;
	
	
	for (i=2; i<g; i++) 
	{
		if (g % i == 0) {
			factor[i] = i;
		}
		else {
			factor[i] = 0;
		}

	}
	
	
	for (i=0; i<g; i++) 
	{
		sum += factor[i];
	}
	
	
	if (sum == g) {
		x = true;
	}
	else {
		x = false;
	}


	return x;

}

Edited 6 Years Ago by WaltP: Added CODE tags -- with all the help about them, how could you miss using them????

It's because array indexes must be known at compile time (unless you are using malloc to create dynamic memory). Try putting it in as a constant value. I'm not sure what was giving you -1 but it's a good bet it is a compiler extension as that should have given an error.

By the way, how are you using bool in C. Do you have a library?

Edited 6 Years Ago by jonsca: n/a

It's because array indexes must be known at compile time (unless you are using malloc to create dynamic memory). Try putting it in as a constant value. I'm not sure what was giving you -1 but it's a good bet it is a compiler extension as that should have given an error.

1.What exactly do you mean by "array indexes must be known at compile time"?
2. Im not using malloc
3. I can't put it as a constant value
4. It's not giving any errors at all

sorry if I seem unknowledgeable, I am still learning my ways around C. If you don't seem to understand why, do you want me to post my source code?

Non-dynamic arrays must have their sizes known as the file is compiling. That is there must be a "compile time constant" value for the size (I don't believe it can even be a const int declared earlier in the program, but I may be wrong about that) is either:

arraytype arrayname[10];

or you can 

#define MAX_SIZE 10
and have 
arraytype arrayname[MAX_SIZE];

Otherwise you need to declare the memory dynamically using a function like malloc which will give you a pointer to a block of memory.

Anyway, post the code and I'll give it a shot. You don't seem not knowledgeable, I'm not impatient. I just want to make sure I understand the problem.

Take a look at line 31. Heres the code:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>


bool PerfectCalculator(int g);


#define kMaxLength	25

int main (int argc, const char * argv[]) {
	char myChar[kMaxLength];
	int num = {28};
	
	
/******************	***The Calculator's Response*****************/
	
	if (PerfectCalculator(num) == true) {
		printf("The typed number %d is a Perfect Number.\n", num);
	}
	else {
		printf("The typed number %d is NOT a Perfect Number.\n", num);
	}
/***************************************************************/
	
	return 0;
}


bool PerfectCalculator(int g)
{
	bool x;
	int i, factor[g], sum;

	
	x = false;
	sum = 0;
	
	
	for (i=2; i<g; i++) 
	{
		factor[0] = 0;
		factor[1] = 0;
		
		
		
		if (g % i == 0) {
			factor[i] = i;
		}
		else {
			factor[i] = 0;
		}

	}
	
	
	for (i=0; i<g; i++) 
	{
		sum += factor[i];
	}
	
	
	if (sum == g) {
		x = true;
	}
	else {
		x = false;
	}


	return x;

}

I'm still working on it. Just to inform you, this may help a lot, this program is a Perfect Number calculator. It tell's you whether a number is perfect. A number is perfect when all of its factors added up equal the number. Ex: 28 is divisible by 14,7,4,2,1 (Notice that in the program I excluded dividing the number by one because if the number is divided by one, it will add the number to the list and therefore ruin the calculator). When added up they equal 28.

gcc PerfectCalculator.c -o PerfectCalculator -ansi -Wall -pedantic
PerfectCalculator.c: In function `main':
PerfectCalculator.c:13: warning: unused variable `myChar'
PerfectCalculator.c: In function `PerfectCalculator':
PerfectCalculator.c:34: warning: ISO C90 forbids variable-size array `factor'

So the way you have it doesn't compile under standard C. That was more my point. It comes up as a warning but to ensure portability (i.e., your compiler was able to put it through due to an extension) you should probably avoid constructs like that.

But back to the errors you are getting. I'm confused as to where the -1 is coming from? Is that the "value" of array[g+1] because that's two positions too far over the boundary of the array(array[g-1] is the last existing member).

I excluded dividing the number by one

You can fix that by initializing your sum to one on line 38 that way that factor always accounted for. It seems to work for 28 when I run it.

Edited 6 Years Ago by jonsca: text clarifications

This question has already been answered. Start a new discussion instead.