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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array

Status
Not open for further replies.

joebeach

Programmer
Sep 12, 1999
13
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>}
 
<br>Try this<br><br>#define MAXNUMS 6 <br>// set MAXNUMS to whatever you want<br>include &lt;iostream.h&gt;<br>int main()<br>{<br>&nbsp;&nbsp;int count[MAXNUMS]= {0};// sets all array elements to 0<br>&nbsp;&nbsp;float total = 0.0;<br>&nbsp;&nbsp;float average = 0.0;<br><br>&nbsp;&nbsp;for (int x = 0; x &lt; MAXNUMS; x++)// 0 to MAXNUMS - 1<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; &quot;Enter a number: &quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin &gt;&gt; count[x];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total += count[x];// same as total = total + count[x];<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;if(MAXNUMS != 0)<br>&nbsp;&nbsp;&nbsp;&nbsp;average = (total / MAXNUMS);<br>&nbsp;&nbsp;cout &lt;&lt; average &lt;&lt; endl;<br><br>&nbsp;&nbsp;return 0;<br>}
 
Your program doesn't initializes ch . And also why are you incrementing the value of count[x]. <br><br>
 
Like TomMcL2 said -- you need to allocate storage for the array.&nbsp;&nbsp;Declaring it as:<br><FONT FACE=monospace><br>&nbsp;&nbsp;&nbsp;&nbsp;int count[]= {0};<br></font><br>doesn't reserve any memory for the contents of count[].&nbsp;&nbsp;IOW, you reserved space for 0 elements.&nbsp;&nbsp;It's good practice to do like TomMcL2 did, and use a constant for array bounds.&nbsp;&nbsp;That way, if you ever need to change the array size, there's only one place you need to go to to make the change.&nbsp;&nbsp;Otherwise, you'd have to change where the array was declared, then search through your code for every place where the array indexer might go out of bounds, etc. etc. (IOW, a real pain).<br><br>Chip H.<br><br>&nbsp;&nbsp;<br><br>
 
Actually, int count[] = {0} reserves 1 element, the 0 element.&nbsp;&nbsp;<br><br>As stated by those above, allocating space for your array is the key issue.&nbsp;&nbsp;Using a constant is good for making the array a static size. Previous suggestions illustrate how to do this very well.&nbsp;&nbsp;Judging from the original code, you need a dynamic solution.<br><br>To retain the dynamics you need,&nbsp;&nbsp;explore malloc and realloc and their relationship to your array.&nbsp;&nbsp;Also look at the interface to existing dynamic array classes.&nbsp;&nbsp;The main point of interest being their GrowBy property.&nbsp;&nbsp;<br><br>Both malloc and realloc are ANSI so you should have them available reguardless of platform.<br><br>One last thing you need to remember, you'll need to free the array when done or you'll end up having to reboot somewhere down the line. <p>Wil Mead<br><a href=mailto:wmead@optonline.net>wmead@optonline.net</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top