Note that there are some explanatory texts on larger screens.

plurals
  1. POA simple "for" command doesnt work using only the stdio library
    primarykey
    data
    text
    <p>Using the same machine and IDE as reffered in my other question (third paragraph at <a href="https://stackoverflow.com/questions/13987812/problems-in-code-or-my-ide-comp-is-bugged">Problems in code or my IDE/comp is bugged?</a>)</p> <p>I try to run this code:</p> <pre><code>#include &lt;stdio.h&gt; #define n 3 int main() { int i; float values[n],sumval,svmean,tmp; for(i=0;i&lt;n;++i) { scanf("%f",&amp;tmp); values[i]=tmp; sumval = sumval + values[i]; } svmean = sumval/n; printf("%f \n",svmean); return(0); } </code></pre> <p>The above code is supposed to run this formula </p> <p><img src="https://i.stack.imgur.com/EqaJv.jpg" alt="http://img850.imageshack.us/img850/6894/95871186.jpg"></p> <p>That means that it has to add some values and divide the result by their total number.</p> <p>As you see above I made an array with random <code>n</code> positions and I ask the user to fill in a value for each position then add them all up and divide them.</p> <p>The problem is that it doesnt work. It outputs only the result 7 no matter what the iput is.</p> <p>BUT if I include stdlib.h to the code it works fine.</p> <p>so</p> <ul> <li><strong>Question A</strong>: why the code does not work properly using only the stdio.h library? which element of the code does require the stdlib.h library?</li> </ul> <p>As you see the array <code>values[n]</code> seems to have an random <code>n</code> number of cells but actually I have already set this numer to be equal to 3 (using <code>#define</code>)</p> <ul> <li><strong>Question B</strong>: Is there a way to run a code with the same porpuse but letting the user to define the size of the array <code>values[n]</code> or in other words let the user input an integer that sets the value of <code>n</code> in <code>values[n]</code>?</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload