Hello. I am having trouble figuring out how to return the sum. I have 2 files p1.c and p2.c

p1.c contains my main which calls createAndCOunt like this:

printf("The value is %d\n",createAndCount());

p1.c also contains count which is called by createAndCOunt
count is:

int		count	(int*	array)
{
  int	sum	= 0;
  int	i;

  for  (i = 0;  i < n-1;  i++)
    sum += array[i+1] - array[i];

  return(sum);
}

p2 contains createAndCount (which I wrote but the p1.c was provided completed)

int createAndCount(int n)
{
	int * intArray = calloc (n, sizeof (int));
	
	int i;
	for (i = 0; i < n; i++)
	{
		intArray[i] = rand() % 256;
		
		
		
	}
	
	return (int) count(*intArray);
	
	
}

The whole point of this exercise I believe is to have createAndCount initialize the array and fill it with Random integers and then return the sum from count.
Everything seems to work except for returning the sum from "count" and having createAndCount return that to main. What am I doing wrong by trying to return
"return (int count(*intArray));" from createAndCount????

PS I had it moved around before and the program asked for the integer of the size of the array and then I receive a SEGMENTATION FAULT?????

I am really new to C. Thanks. I will post full files if needed.

****UPDATE TO createAndCount*****

I updated createAndCount to this

int createAndCount(int n)
{
	int * intArray = calloc (n, sizeof (int));
	int sum;
	int i;
	for (i = 0; i < n; i++)
	{
		intArray[i] = rand() % 256;
		sum = int count(int intArray[i]);
		
		
	}
	free(intArray);
	return sum;
	
}

I just need to figure out how to return the sum from count????? the error I am now receiving is
"p2.c:23: error: expected expression before ‘int’"

p2.c:23 is sum = int count(int intArray);

You don't give the datatype when you call a function. Your code should be :

sum = count(intArray[i])

I find lot of bugs in your code.

1. In the main function you are calling createAndCount() without any arguments, but where as from the signature of the function, it is seen that it accepts an integer.

2. In count() you are using a variable 'n' in the for loop. But it is not declared anywhere. Or do you have any global variable which you haven't posted along with your code?

3. The call to count() from createAndCount() is wrong

//sum = int count(int intArray[i]); ---wrong---
//It should be
sum = count(intArray[i]);

4. Provided the 3rd suggestion is exactly what you want, you have another bug. The prototype of count(). When you pass an integer, count() cannot have an integer pointer as a parameter.

5. But if the 3rd suggestion is wrong, you have another problem. At first glance, what I see is that you are doing some array operations inside count() which means that you are supposed to pass an array to it from createAndCount() which is like this.

sum = count(intArray);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.