0

I am currently writing a program in c that is to do a few equations via functions. The possible data that is going to be read is up to 1 million two precision floating numbers excluding 0. I am getting an error when I try to count the number of numbers read into the array, but for some reason I get a default value of 264 every single time I run the program. So for instance if dont input any values I get a count of 264, 1 value 265, 2 values 266 and so on. I guess I could subtract 264 from count to get the accurate total but I want to know why this is happening and where the 264 comes from. I have attached the code I have so far below. Thank you.

#include <stdio.h>
#include <stdlib.h>

#define N 1000000

int count_num(double numbers[]);
double sum(double numbers[]);
double max(double numbers[]);
double min(double numbers[]);
double ar_mean(double numbers[]);
double har_mean(double numbers[]);
double variance(double numbers[]);

int main(void)
{
    double numbers[N];
    int i =0;

    while(scanf("%lf.2",&numbers[i])!=EOF&&i<N)
    {
        i++;
    }

    int count=count_num(numbers);
    printf("Count: %d\n", count);
}

int count_num(double numbers[])
{
    int count=0;
    for(int i=0;i<N;i++)
    {
        if((numbers[i]!=0)&&(numbers[i]!=0.0))
        {
            count++;
        }
    }

    return count;
}
WedaPashi
  • 3,561
  • 26
  • 42
  • There's a couple issues, but I think the main one is that C does not initialize values to defaults (i.e. your double array is not initialized to an array of 0s). That still shouldn't quite explain why you *keep* getting 264 though... – information_interchange Nov 29 '17 at 03:56
  • 1
    First of all, in `count_num` you are always looking through the entire array of size `N`, while only first `i` values in that array are meaningful. The rest is unpredictable garbage. What is the point of analyzing and counting that garbage? – AnT stands with Russia Nov 29 '17 at 03:57
  • Putting a 3.8 MB array on the stack doesn't seem wise to me. – Jonathon Reinhart Nov 29 '17 at 04:17
  • Thank you for the help. I thought I had to loop through the entire array since I don't know the size of the input just its max size. I know I could use something like an ArrayList in Java, but I think at this point in the class we arent supposed to be using things in C such as dynamic memory allocation to solve this problem. I would think that would be the best way to not waste memory to hold 1 million double values if I didnt have to. – Jacob Greene Nov 29 '17 at 04:22

1 Answers1

2

Both commenters, information_interchange and AnT, are correct, the uninitialized array is the cause of the problem-it contains unknown values. You need to initialize the array, either by looping and setting all elements to 0.0 or by using the construct double numbers[N] = {0.0};

Another one: your main function does not return int but has to according to ISO/IEC 9899:2011 5.1.2.2.1.

deamentiaemundi
  • 5,502
  • 2
  • 12
  • 20
  • Thanks! adding the {0.0} worked and now my program is running smoothly. – Jacob Greene Nov 29 '17 at 04:23
  • `main` does not need to have a `return` statement. It is special. – Eric Postpischil Nov 29 '17 at 10:59
  • @EricPostpischil Added citation to clarify. There are excemptions to this rule (e.g.: J.2, J.3.2, J.5.2) but they have to be documented (J.3), so without any such notes the default must be taken. – deamentiaemundi Nov 29 '17 at 17:47
  • Your citations are about defining `main`. I did not say `main` does not need to be defined. I said it does not need to have a `return` statement. 5.1.2.2.3 says that reaching the `}` that terminates the `main` function returns a value of 0. The OP did define `main` with a type as shown in 5.1.2.2.1. – Eric Postpischil Nov 29 '17 at 18:59
  • @EricPostpischil a return is not implicit in `main` and 5.1.2.2.3 says exactly that. The `main` function is just a normal function with some explicitly listed exceptions and if you define a function returning a type instead of `void` you have to return that type which is `int` in the case of `main`. A function `void main(){...}` as the entry function is not allowed anymore. But you don't have to trust me, you can ask the rest of stackoverflow. I think the proper tag is `language-lawyer` together with the respective language tag. – deamentiaemundi Nov 29 '17 at 20:20
  • 5.1.2.2.3 includes the text “reaching the } that terminates the main function returns a value of 0.” This is saying `main` is special: If execution reaches the closing brace of `main`, a return of 0 is automatically performed. `main` does not need to have an explicit `return` statement. – Eric Postpischil Nov 29 '17 at 20:35
  • [Q: What was the rationale for making `return 0` at the end of `main` optional?](https://stackoverflow.com/questions/31394171/what-was-the-rationale-for-making-return-0-at-the-end-of-main-optional). – Eric Postpischil Nov 29 '17 at 20:37
  • On top of that, I do not think even regular functions have to return values, as long as the caller does not use them. 6.9.1 12 says “If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.” I do not see anything else requiring functions that are not declared `void` to return a value. – Eric Postpischil Nov 29 '17 at 20:49
  • @EricPostpischil Ah, ok. Which would normally lead to the question: Who paid for that? But we all know who did, don't we? ;-) – deamentiaemundi Nov 29 '17 at 21:02