I took the liberty of re-indenting your code, and enclosing your existing comments in /* */ because I added some more starting with //. I hope it will help you (I left the naming and the logic of your code intact).
[offtopic ? ]
Adding to what other people suggested in this thread, also consider the following:
Try to avoid global variables. Instead try to define local variables inside the functions that need them. If those functions call other functions that need access to any of those variables, then pass those variables as arguments (parameters) to the functions being called.
If a function being called does not need to return back to its caller the value of a passed argument (modified or not), then the caller just passes the variable to that function.
However, if a function being called modifies the value of a passed argument and needs to return the new value back to the caller through the same argument, then the caller passes the address of the variable to the function (which in turn treats it as a pointer inside its body).
An example of the above would be to define both your Data array and its user-defined length n locally in the main() function, and then pass them as arguments to any other functions being called by main() that need access to those variables.
Here is an example of the suggestions above, for your getArray() and printArray() functions...
Code:
/* ---------------------------------------------------------- */
void flush_stdin( void )
{
int c;
while ( '\n' != (c=getchar()) && EOF != c )
; /* void */
}
/* ----------------------------------------------------------
* Read numbers into array.
*/
int getArray( int Data[50], int *n )
{
int i, temp=0; /* temp is for validating scanf's return value */
/* keep asking for n until user inputs a valid value */
do {
puts("Enter number of data points.");
temp = scanf("%d", n);
/* on bad input flush stdin (does not work for input like: 12df) */
if ( 1 != temp ) {
flush_stdin();
continue;
}
} while ( *n < 1 || *n > 50 );
puts("Please enter data points.");
for (i=0; i < *n; i++) {
/* keep asking until user inputs a valid int */
for (;;) {
printf("%d = ", i);
fflush( stdout );
temp = scanf("%d", &Data[i]);
/* on bad input flush stdin (does not work for input like: 12df) */
if ( 1 != temp ) {
flush_stdin();
continue;
}
break;
}
}
return 0;
}
/* ----------------------------------------------------------
* Prints all numbers in array.
*/
int printArray( const int Data[50], int n )
{
int i;
/* sanity check */
if ( n < 1 || n > 50 ) {
fputs( "*** internal error!\n", stderr );
return 1;
}
for (i=0; i < n; i++) {
printf("%d, ", Data[i]);
}
putchar('\n');
return 0;
}
/* ---------------------------------------------------------- */
int main( void )
{
int Data[50]; // the array (WITH ROOM FOR UP TO 50 ELEMENTS)
int n; // <--- user defined total count of array elements (YOU MUST VALIDATE IT ON INPUT)
getArray( Data, &n );
printArray( Data, n );
...
}
Admittedly, the modified getArray() function is convoluted because it tries to reject non-integer values on input using the return value of scanf(). It also fails to flush the stdin when input is given of the form "123jklsj", but at least it works.
Generic interactive input via standard C functions is sadly quite complicated (here is an enlightening article, if you are up to it), but since this is not the main point in this thread, here's a simplified version of the modified getArray() function...
Code:
int getArray( int Data[50], int *n )
{
int i;
/* keep asking for n until user inputs a valid value */
do {
puts("Enter number of data points.");
scanf("%d", n);
} while ( *n < 1 || *n > 50 );
puts("Please enter data points.");
for (i=0; i < *n; i++) {
printf("%d = ", i);
fflush( stdout ); /* play it safe on all platforms */
scanf("%d", &Data[i]);
}
return 0;
}
One thing to remember here, imho, is that Data[50], n, and i are defined in their logically minimal scope, and they are passed as function arguments only if they need to be accessed in deeper scopes.
Well, I didn't express the last paragraph as nicely as I intended, perhaps a native English speaker can revise it using more precise wording.
The other thing to remember, is to always validate the user input before using it.
PS. Is there a tag equivalent for the [offtopic] or [spoiler] tags used in other boards?
[/offtopic ?]