-1

I created a program to calculate the sum of 4 array elements. Is it possible to prompt the user to fill array elements manually during program execution and then show the sum? I can do this while not using the function, but I get stuck when I use functions. Is it still possible to do so?

If I insert a for loop inside the main function, just after variable initialization,

int data[];
int total;
int size = sizeof(data) / sizeof(data[0]);

printf("Enter array elements: ");
for(int i=0; i<size; i++) {
    scanf("%d", &data[i]);
}

the compiler complains:

error: definition of the variable with array type needs an explicit size or an initializer
    int data[];
        ^
1 error generated.

My program looks like this:

#include <stdio.h>

int sum(int data[], int size)
{
    int sum = 0;
    int i;

    for (i = 0; i < size; i++)
        sum += data[i];

    return sum;
}

int main()
{

    int data[] = { 1, 2, 3, 4 }; // I want user input here
    int total;
    int size = sizeof(data) / sizeof(data[0]);

    total = sum(data, size);

    printf("Sum is: %d\n", total);
    return 0;
}
  • 3
    "If I insert a for loop here, the compiler complains that data (the array that stores user input) in uninitialized." Where is "here"? Would you mind showing us the program with problem? – MikeCAT Feb 05 '18 at 15:47
  • 4
    Don't show the code that works, show the code **that does not work**. Don't describe your code but show it. – Jabberwocky Feb 05 '18 at 16:08
  • I will add the answer when I get time. – user2736738 Feb 05 '18 at 16:39
  • @SudarshanKakoty.: First check this answer of mine....try to run this code...It does whatever you want. The program basically asks for numbers and you enter it gradually and then you press "quit" or `q` to exit...https://stackoverflow.com/a/48528760/3796113... – user2736738 Feb 05 '18 at 17:02
  • I am a bit busy and tired both - if I come back tomorrow and see this unanswered and if the other linked answer doesn't help (which is very unlikely) I will edit the answer to meet your requirement. Until then ..all the best – user2736738 Feb 05 '18 at 17:07
  • Well, I need to dynamically allot memory then. No problem, I will keep trying by myself too :) Seems like I landed in a harder surface than expected – Sudarshan Kakoty Feb 05 '18 at 17:12
  • the C operator `sizeof()` returns a `size_t` not a `int` So the variable `size` should be declared as: `size_t size;` (this may require some slight modification to the rest of the code to avoid any compiler warning messages about conversions from `long unsigned` to `int` – user3629249 Feb 05 '18 at 18:18
  • regarding: `int sum(int data[], int size) { int sum = 0;` DO NOT name the local variables the same as the function name. Such code leads to confusion. Suggest: `int sum(int data[], int size) { int mySum = 0;` – user3629249 Feb 05 '18 at 18:19
  • Ow, thanks. :)) I got you :) – Sudarshan Kakoty Feb 05 '18 at 18:42

1 Answers1

0

error: definition of the variable with array type needs an explicit size or an initializer int data[];

Compiler is clear, you can't do this:

int data[];

Need something like:

int data[SIZE];
purec
  • 308
  • 2
  • 6
  • I know the reason for the error, but according to condition, size should be handled automatically depending upon nums of integer user will provide as input to STDIN. (You can't pre-define it) – Sudarshan Kakoty Feb 05 '18 at 18:28