# Thread: Little second question from newbie(

1. ## Little second question from newbie(

Any advise? What is wrong? It gives meaningless result but as always it looks fine by me (

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

int largest(int a[], int n)
{
int largestD;
largestD = a[0];
for(int i = 1; i < n; i++)
if(a[i] > largestD) largestD = a[i];
return largestD;
}

int average(int a[], int n)
{
int averageD = 0;
for(int i = 0; i < n; i++)
averageD += a[i];
return averageD / n;
}

int positive(int a[], int n)
{
int h = 0;
for(int i = 0; i < n; i++)
if(a[i] > 0) h++;
return h;
}

int main(void)
{
int n;
int a[n];

printf("Enter a lenght of array a: ");
scanf("%d", &n);

printf("Enter %d elements of array: ", n);
for(int i = 0; i <= n - 1; i++)
scanf("%d", &a[i]);

for(int i = 0; i <= n - 1; i++)
printf("%d ", a[i]);

printf("\nLargest number is: %d\n", largest);

printf("Average of aray is : %d\n", average);

printf("Numbers of positive digits is: %d", positive);

return 0;
}```
Enter a lenght of array a: 5
Enter 5 elements of array: 1
2
3
4
5
1 2 3 4 5
Largest number is: -444131880
Average of aray is : -444131774
Numbers of positive digits is: -444131693
PS C:\Users\nirva\Desktop\c files\.vscode\Book_2\9_Function>

2. You need to "call" the functions, not just mention their names.
For example, instead of largest it should be largest(a, n)

BTW, it is more idiomatic to write your loops like for (int i = 0; i < n; i++)

3. Code:
```int n;
int a[n];

printf("Enter a lenght of array a: ");
scanf("%d", &n);```
C doesn't revise the size of the array just because you assign a new value to n.

Input n, then make your array.

4. To add to Salem's post, the "int a[n]" syntax creates what is known as a Variable-Length Array (VLA) which is not necessarily supported (you can read more about possible shortcomings here).

Instead, you should preferably malloc the array like this (note that stdlib.h is also #included):
Code:
```#include <stdio.h>
#include <stdlib.h>

/* largest, average, positive definitions */

int main(void)
{
int n;

printf("Enter a lenght of array a: ");
scanf("%d", &n);

int *a = malloc(n * sizeof (*a)); // allocate n elements of the correct byte size

/* With VLAs, the following would be a crash instead! */
if (a == NULL) // A.K.A. the array can't fit in memory
{
fprintf(stderr, "Sorry, there's not enough memory for %d elements!\n", n);
return 1; // no point in continuing execution
}

/* rest of main until last printf */

free(a); // avoid memory leak
return 0;
}```
malloc will "make room" in memory (usually the heap part) for the array. "n" is the number of elements of a, and "sizeof (*a)" is the number of bytes in memory each of these elements needs.

If there is not "enough" heap space left, it will simply return a NULL pointer. On the contrary, VLA allocation would not even try to make sure the array fits in the stack (another part of the memory).

Remember, however, to always, ALWAYS free whatever you malloc by hand when you're done with it (never have a malloc without a matching free)!

5. Thx to all for help! Appreciate it!