1. ## Problem With Array

I'm working on this code for an assignment:
Code:
```#include <stdio.h>
#include <math.h>

#define SIZE = 100
double x[SIZE];
#define SENTINEL -1

int fillArray (double[],int);
float x;

int main(void)

{
int n;
int x;

n = fillArray (x, maxsize);
print n
print x

}

// Function to read a collection of type float data from
//    the keyboard and store this data in the array x.
// Returns number of elements read if function completes as //    intended.
// Otherwise: Returns -2 if SIZE <= 0; Returns -1 if file //    is empty.  Returns 0 if too many items in file (array //    is filled while data remains to be processed)

int fillArray
(double x[],     // INOUT: array to be filled
int arraysize)  // IN: size of array
{
double item;  // Temp cell for each item read
int count;    // Count of number of items read

if (arraysize <= 0) return (-2);

count = 0;
// Execute the "priming" or initial read
printf("Enter the first data item: ");
scanf ("%f", &item);
// Repeat until no more data.
while (item != SENTINEL)
{
x[count] = item;
count++;
if (count >= arraysize) return (0); // array full
// Execute the "update" or next read
printf("Enter the next data item: ");
scanf ("%f", &item);
}
if (count == 0) return (-1); // file empty
else return (count);
} // end fillArray```
Can anyone help me and know why it's not compiling? Thanks.

2. It would be helpful if you stated the specific errors your compiler gives you when you try and compile the code.

I will give you a few clues:
1) You define 'x' as an array of doubles and then redefine it as a single float

2) You haven't defined the variable 'maxsize' but you use it none-the-less.

3) Your 'print' statements in your main function don't make any sense.

There may be problems in your fillArray() function, but I haven't gone through it thoroughly.

3. The fillArray function needs a double[], and an int. You're giving it an int and something that isn't even declared.

Fix that, and look at any others you're getting with it. What do they say, what do they mean? You have to start learning what those messages mean, so you can troubleshoot - that's a big part of programming.

And Welcome to the forum, mdthejericho!
Code:
```int fillArray (double[],int);
float x;

int main(void)

{
int n;
int x;

n = fillArray (x, maxsize);```
Edit: the variable x can only be ONE thing. You have here as three different things! Ooops!

4. You have made plenty of small and big mistakes. one of them is declaring x before completing the preprocessor commands.

5. Code:
`#define SIZE = 100`
There's no = in #defines, so this should be
Code:
`#define SIZE 100`

6. Well, on Fridays, I WANT it!!

There's no = in #defines, so this should be
We will be French -- Man zee barricades!! Block zee refinery!! We shall get that equal sign into defines, on Fridays!!

< ROFL >

7. Code:
```#include <stdio.h>
#include <math.h>

#define SENTINEL -1
#define SIZE 100           //my edit: removed equal sign

int fillArray (double*,int);
double x[SIZE];

int main(void)
{
int n;
int maxsize = 20; //sample size
n = fillArray (x, maxsize);
printf("%d\n",n);            // my edit: print n
for (int i = 0; i < SIZE; i++)
{
printf("%g\n",*(x+i));   // my edit: print elements of x
}
getchar();
getchar();
return 0;
}

// Function to read a collection of type float data from
// the keyboard and store this data in the array x.
// Returns number of elements read if function completes as // intended.
// Otherwise: Returns -2 if SIZE <= 0; Returns -1 if file // is empty. Returns 0 if too many items in file (array // is filled while data remains to be processed)

int fillArray
(double *x, // INOUT: array to be filled
int arraysize) // IN: size of array
{
double item; // Temp cell for each item read
int count = 0; // Count of number of items read
if (arraysize <= 0) return (-2);

// Execute the "priming" or initial read
printf("Enter the first data item: ");
scanf ("%lg", &item);             //my edit: changed %f to %lg
// Repeat until no more data.
while (item !=SENTINEL){
x[count] = item;
count++;
if (count >= arraysize) return (0); // array full
// Execute the "update" or next read
printf("Enter the next data item: ");
scanf ("%lg", &item);            //my edit: changed %f to %lg
}
if (count == 0) return (-1); // file empty
else return (count);
} // end fillArray```
the code above compiles. fillarray starts filling x at x[1] though.

edited:

8. scout: you need to indent your code properly. You pretty much butchered what good indentation there was in mdthejericho's code. It is also a good idea if you summarised what you did.

9. Thanks, guys! I'm glad to be here and plan on learning a lot. I'm going to review all the responses and try some out. I'll post what I come up with.

Also, the biggest error I got was a constant error for

Code:
` double x[SIZE];`
It expects a value here and I don't know why. It doesn't matter if I place it in the main program or above the main program.

10. Originally Posted by rags_to_riches
Code:
`#define SIZE = 100`
There's no = in #defines, so this should be
Code:
`#define SIZE 100`
THAT WAS IT!!! Yes. Now it compiles and I can try to get the rest of it working. Eventually, I need to write functions that compute the mean and sum.

11. Okay, the code compiles and I can type in numbers, but how do I display all the values in the main program? That's what I was trying to do with those "print" commands that didn't make sense. Here' the new code:

Code:
```#include <stdio.h>
#include <math.h>

#define SIZE  20
double x[SIZE];
#define SENTINEL -1

int fillArray (double[],int);

int main(void)

{
int n;

n = fillArray (x, SIZE);

}

// Function to read a collection of type float data from
//    the keyboard and store this data in the array x.
// Returns number of elements read if function completes as
//    intended.
// Otherwise: Returns -2 if SIZE <= 0; Returns -1 if file
//    is empty.  Returns 0 if too many items in file (array
//    is filled while data remains to be processed)

int fillArray
(double x[],     // INOUT: array to be filled
int arraysize)  // IN: size of array
{
double item;  // Temp cell for each item read
int count;    // Count of number of items read

if (arraysize <= 0) return (-2);

count = 0;
// Execute the "priming" or initial read
printf("Enter the first data item: ");
scanf ("%f", &item);
// Repeat until no more data.
while (item != SENTINEL)
{
x[count] = item;
count++;
if (count >= arraysize) return (0); // array full
// Execute the "update" or next read
printf("Enter the next data item: ");
scanf ("%f", &item);
}
if (count == 0) return (-1); // file empty
else return (count);
} // end fillArray```
sorry for the triple post!

12. Could anyone point me in the right direction?

13. > else return (count);
You store this in n when you return.
Maybe a for loop to iterate over this range?

14. Here is my main program and I keep getting the value 0.00 when I try to play around with displaying the array:

Code:
```int main(void)

{
int n;

n = fillArray (x, SIZE);
printf("%f", x[1]);

}```

15. Several things need to be tweaked, but start with this. This creates the array in main(), and passes it to the fillArray function. Then displays the data.

If you create the array in the fillArray function, the array will have they call "local scope", meaning it will die when the program leaves the fillArray function. (there are ways around it, but they are more advanced).

So:

Code:
```#include <stdio.h>

#define SIZE 20  //whatever

int main(void)
{
int i, n;
double x[SIZE];

n = fillArray (x, SIZE);

for(i=0;i<SIZE;i++) {
printf("\n %f", x[i]);
}