c array bound Home › Forums › C Programming › c array bound This topic contains 1 reply, has 2 voices, and was last updated by Humayan 9 years, 9 months ago. Author Posts July 16, 2008 at 2:34 am #2115 misfirakarimMember Hai I am getting problem in my c program C <br /> main()<br /> {<br /> int a[3],i,n,s=0;<br /> printf("Enter the n value..n");<br /> scanf("%d",&n);<br /> printf("Enter the values one by one...n");<br /> for(i=0;i<n ;i++)<br></n> {<br /> scanf("%d",&a);<br /> s=s+a;<br /> }<br /> printf("the sum is %d",s);<br /> }<br /> <br /> input:<br /> Enter the n value..<br /> 5<br /> Enter the values one by one...<br /> 1<br /> 3<br /> 2<br /> 3<br /> 6<br /> the sum is 14<br /> 1234567891011121314151617181920212223242526 <br />main()<br />{<br />int a[3],i,n,s=0;<br />printf("Enter the n value..n");<br />scanf("%d",&n);<br />printf("Enter the values one by one...n");<br />for(i=0;i<n ;i++)<br></n>{<br />scanf("%d",&a);<br />s=s+a;<br />}<br />printf("the sum is %d",s);<br />}<br /><br />input:<br />Enter the n value..<br />5<br />Enter the values one by one...<br />1<br />3<br />2<br />3<br />6<br />the sum is 14<br /> problem is array declared size is 3(a[0],a[1],a[2] so it can hold one 3 values) the value enterd through keyboard is 5(ie., n=5) still it gets the 5 values and add all the 5 values how it is possible when the size is 3 please give the solution for the same output. July 17, 2008 at 11:57 am #3406 HumayanMember It looks like in the code below you are you are only using the first item in the array ( a [ 0 ] ) to read in values in the for loop: scanf(“%d”,&a); you then add the address of the address of the first item in the array to ‘s’: s=s+a; and then print this sum. I think you should try: s = s + a [ 0 ] ; for the proper result. To read stepping through the array do: for( i = 0 ; i < n ; i ++ ) { scanf ( “%d” , &a [ i ] ); s = s + a [ i ]; } Author Posts You must be logged in to reply to this topic.