-2

Using the same machine and IDE as reffered in my other question (third paragraph at Problems in code or my IDE/comp is bugged?)

I try to run this code:

#include <stdio.h>
#define n 3
int main()
{
    int i;
    float values[n],sumval,svmean,tmp;
    for(i=0;i<n;++i)
    {
        scanf("%f",&tmp);
        values[i]=tmp;
        sumval = sumval + values[i];
    }
    svmean = sumval/n;
    printf("%f \n",svmean);
    return(0);
}

The above code is supposed to run this formula

http://img850.imageshack.us/img850/6894/95871186.jpg

That means that it has to add some values and divide the result by their total number.

As you see above I made an array with random n positions and I ask the user to fill in a value for each position then add them all up and divide them.

The problem is that it doesnt work. It outputs only the result 7 no matter what the iput is.

BUT if I include stdlib.h to the code it works fine.

so

  • Question A: why the code does not work properly using only the stdio.h library? which element of the code does require the stdlib.h library?

As you see the array values[n] seems to have an random n number of cells but actually I have already set this numer to be equal to 3 (using #define)

  • Question B: Is there a way to run a code with the same porpuse but letting the user to define the size of the array values[n] or in other words let the user input an integer that sets the value of n in values[n]?
Community
  • 1
  • 1
John
  • 11
  • 3

6 Answers6

2

First of all, you forgot to initialize sumval. You want it to be 0 at the beginning.

If you want the size of the array to be decided at runtime, you have to allocate it dynamically using malloc, for example like this:

int n;
float *values,sumval=0,svmean,tmp;
scanf("%d", &n);
values = (float *) malloc (n * sizeof(float));

Later, you should release the memory allocated by calling free:

free(values);
piokuc
  • 25,594
  • 11
  • 72
  • 102
  • I did that in other trials of the same code but it does not seem to change anything (I suppose the compiler accept the sumval default value to be 0) even when not sumval is initialized using stdlib.h it works fine. thanks for your response. – John Dec 21 '12 at 10:51
  • 2
    @John wrong guess. `sumval` is an automatic variable (allocated on stack) which means that it is not guaranteed to be initialized to any specific value, it will normally contain some 'garbage'. – piokuc Dec 21 '12 at 10:58
0

Initialize sumVal to o. Because for first iteration it adds garbage+values[i] into sumValue.

 #include <stdio.h>
 #define n 3
 int main()
 {
 int i;
 float values[n],sumval=0,svmean,tmp;
 for(i=0;i<n;++i){
 scanf("%f",&tmp);
 values[i]=tmp;
 sumval = sumval + values[i];
 }
  sumean = sumval/n;
  printf("%f \n",svmean);
  return(0);

}

Vallabh Patade
  • 4,960
  • 6
  • 31
  • 40
0

The problem is that you do not initialize sumval. You should set it to 0.0 before you for loop.

The change occurring when including/not-including stdio.h is probably due to some initialization functions using the stack, and changing the values in memory, before you enter your function, and it happen that this memory is used for your sumval variable.

But you should NOT rely on this.

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
0

You can allocate an array with variable size in the heap of the program like that:

#include <stdio.h>
int main(int argc, char **argv)
{
   int i;
   //Get the size of the array from input parameter
   int n = atoi(argv[1]);                
   float sumval,svmean,tmp;
   //Allocate the array of values
   float *values = malloc(sizeof(float)*n);
   // Initialize sumval
   for(sumval=0,i=0;i<n;++i){
      scanf("%f",&tmp);
      values[i]=tmp;
      sumval = sumval + values[i];
   }
   svmean = sumval/n;
   printf("%f \n",svmean);
   //Free it
   free( values );               
   return(0);
  }

You also need to initialize sumval to 0. The parameter of the size of the array is passed when launching the program (if you use an IDE you should check how it does that)

Genís
  • 1,468
  • 2
  • 13
  • 24
  • Genis thanks for your code... but 1) i couldnt make it run... first the debugger asks for n var to be defined and needs a ";" at line 9 I defined n but again nothing... 2) what exactly are the paremeters in main? also there is no scanf for getting the size of the array.. I also added one but again it does not work.. – John Dec 22 '12 at 16:56
  • Well you're right, I left a ";" and didn't declare "n", but those are trivial corrections, isn't it? In what concerns the scanf for getting the size of the array, it doesn't scanf it, but get it from the parameters in the command line, but you can change it if you like. – Genís Dec 26 '12 at 19:57
0

Question A answer In the code you posted there is no need for stdlib.h. This library is needed if you use a function for allocating memory dynamically, such as malloc().

Question B answer: This is a way to do what you want:

#include <stdio.h>

int main()
{
    int i, choice;
    float *values,sumval,svmean,tmp;
    printf("Please enter the size of the array: ");
    scanf("%d", &choice);
    values = (float*) malloc(choice*sizeof(float));
    for(i=0;i<n;i++){
        scanf("%f",&tmp);
        values[i]=tmp;
        sumval = sumval + values[i];
    }
    svmean = sumval/n;
    printf("%f \n",svmean);
    free(values);
    return 0;
}

Also, I modified the incrementation of i in the for statement to increase after running the loop.

nick.katsip
  • 868
  • 3
  • 12
  • 32
  • 1
    Thanks popanik I see that you are using pointers which I cant remember almost anything about their "mechanics"... running your code though leads to an error at line 10 by my GNU GCC compiler for windows " error: 'n' undeclared (first use in this function) ||=== Build finished: 1 errors, 4 warnings (0 minutes, 0 seconds) ===| – John Dec 21 '12 at 13:15
  • It doesnt seem to run "error: 'n' was not declared in this scope " in line 10 and 15 :/ – John Dec 22 '12 at 17:15
  • Yes John. My bad. Actually, instead of n I use the variable choice. So at the lines that the error occurs, just replace n with choice and everything will work just fine. – nick.katsip Dec 22 '12 at 18:17
0

Try this..

#include <stdio.h>

int main()
{
    int i = 0;
    int n = 0;
    float sumval = 0;
    float svmean = 0;
    float tmp = 0;

    printf("Enter count : ");
    scanf("%d", &n);

    for (i = 0; i < n; ++i) {
        scanf("%f", &tmp);
        sumval = sumval + tmp;
    }

    svmean = sumval/n;
    printf("%f \n",svmean);

    return(0);
}

In your code, values[] array is not necessary to calculate output. Are you storing the values for any reason??..

Ajith
  • 613
  • 1
  • 15
  • 32