Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

count in array

Status
Not open for further replies.

joebeach

Programmer
Sep 12, 1999
13
0
0
US
Can anyone tell me what is wrong with this program?<br><br>#include &lt;iostream.h&gt;<br>int main()<br>{<br>&nbsp;&nbsp;int count[]= {0};<br>&nbsp;&nbsp;int ch, x;<br>&nbsp;&nbsp;float num , total, average;<br><br>&nbsp;&nbsp;x =0;<br>&nbsp;&nbsp;total = 0.0;<br><br>&nbsp;&nbsp;for (count[x] =0; count[x] &lt; ch; count[x]++)<br>&nbsp;&nbsp;{<br> cout &lt;&lt; &quot;Enter a number:&quot;;<br> cin&gt;&gt; count[ch];<br> total = total + count[ch];<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;average = total / 6;<br>&nbsp;&nbsp;cout&lt;&lt;average&lt;&lt;endl;<br><br>&nbsp;&nbsp;return 0;<br>}
 
joebeach,<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Just a quick look I can see a couple of things that stand out about your <b>for</b> loop. First, <i>ch</i> is not initialized. When you type <i>int ch</i>, you are setting aside memory for the variable but you are not setting it to anything. The value of <i>ch</i> is going to be whatever was already at that memory location unless you set it to something.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Second, your <b>for</b> loop is saying that you are checking the contents of the array to see if it is less than <i>ch</i>. I think that you need to be checking the variable <i>x</i> and not <i>count[x]</i>.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Try this:<br>#include &lt;iostream.h&gt;<br>int main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;int ch = 6;<br>&nbsp;&nbsp;&nbsp;&nbsp;int count[ch]= {0};<br>&nbsp;&nbsp;&nbsp;&nbsp;float total, average;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;total = 0.0;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;for (int x = 0; x &lt; ch; x++)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; &quot;Enter a number:&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin&gt;&gt; count[ch];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total = total + count[ch];<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;average = total / ch;<br>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;average&lt;&lt;endl;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br>} <br><br>&nbsp;&nbsp;&nbsp;&nbsp;I haven't run this to check it but it seems to me that this is more like what you want.<br> <p>James P. Cottingham<br><a href=mailto:main@ivcusa.com>main@ivcusa.com</a><br><a href= Veneer Co., Inc.</a><br>All opinions are mine alone and do not necessarily reflect those of my employer.
 
2ffat,<br>The only thing is that ch has to be an unknow count because the user has to input how many time.&nbsp;&nbsp;It should be dymanically.&nbsp;&nbsp;like one time it would be 2.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top