Thread: [Dev-C++] Array increment in C
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: sam <as...@ya...> - 2011-06-17 09:57:04
|
Sorry it has been a while since I program.
It is a stupid question. So, please forgive my ignorance. Maybe I lose my memory or something!!
I have a problem with the below code!!!
I can’t understand this part: ++frequency [ responses [ answer ] ];
I know this part increment 1 to the value position of the result of this one responses [ answer ]
For example responses [ answer ]= 1; then ++frequency [1]
But how did the result of frequency[ rating ]= 2,2,2,2,5,11,5,7,1,3 ?????
So can you please explain it to me?
//////////////////////////////////////////////////////
#include <stdio.h>
#define RESPONSE_SIZE 40
#define FREQUENCY_SIZE 11
int main()
{
int answer, rating, frequency[ FREQUENCY_SIZE ] = { 0 };
int responses[ RESPONSE_SIZE ] =
{ 1, 2, 6, 4, 8, 5, 9, 7, 8, 10,
1, 6, 3, 8, 6, 10, 3, 8, 2, 7,
6, 5, 7, 6, 8, 6, 7, 5, 6, 6,
5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };
for ( answer = 0; answer <= RESPONSE_SIZE - 1; answer++ )
++frequency [ responses [ answer ] ];
printf( "%s%17s\n", "Rating", "Frequency" );
for ( rating = 1; rating <= FREQUENCY_SIZE - 1; rating++ )
printf( "%6d%17d\n", rating, frequency[ rating ] );
getchar();
return 0;}
//////////////////////
|
|
From: Robert A. <ral...@gm...> - 2011-06-17 10:26:27
|
Hello,
As you go though the loop the responses [ answer ] will vary 1 , 2,
6, 4, 8 , etc...
as it goes though the responses array ( hopefully there are then then
40 items in the array
as that is the size of the array. Note the size of the frequency
array is 11, it is just not fully initialized (though it should
default to zeros as it's on the stack, and the {0} may be repeated to
fill all 11 slots.
Since the frequency array has 11 slots, it should start out as
0,0,0,0,0,etc...
As you go though the loop, you increment each slot that is indexed by
the value of responses [ answer ] , ie.. 1, 2, 6, 4, ...
as the largest value I see if 10, that should not overflow the 0 to 10
index of the 11 element array.
At the end you just look though the frequency array, skipping the
first element (index zero).
Hope that helps,
Robert
On Fri, Jun 17, 2011 at 5:56 AM, sam <as...@ya...> wrote:
> Sorry it has been a while since I program.
> It is a stupid question. So, please forgive my ignorance. Maybe I lose my memory or something!!
> I have a problem with the below code!!!
> I can’t understand this part: ++frequency [ responses [ answer ] ];
> I know this part increment 1 to the value position of the result of this one responses [ answer ]
> For example responses [ answer ]= 1; then ++frequency [1]
> But how did the result of frequency[ rating ]= 2,2,2,2,5,11,5,7,1,3 ?????
> So can you please explain it to me?
> //////////////////////////////////////////////////////
> #include <stdio.h>
> #define RESPONSE_SIZE 40
> #define FREQUENCY_SIZE 11
> int main()
> {
> int answer, rating, frequency[ FREQUENCY_SIZE ] = { 0 };
> int responses[ RESPONSE_SIZE ] =
> { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10,
> 1, 6, 3, 8, 6, 10, 3, 8, 2, 7,
> 6, 5, 7, 6, 8, 6, 7, 5, 6, 6,
> 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };
> for ( answer = 0; answer <= RESPONSE_SIZE - 1; answer++ )
> ++frequency [ responses [ answer ] ];
> printf( "%s%17s\n", "Rating", "Frequency" );
> for ( rating = 1; rating <= FREQUENCY_SIZE - 1; rating++ )
> printf( "%6d%17d\n", rating, frequency[ rating ] );
> getchar();
> return 0;}
> //////////////////////
>
> ------------------------------------------------------------------------------
> EditLive Enterprise is the world's most technically advanced content
> authoring tool. Experience the power of Track Changes, Inline Image
> Editing and ensure content is compliant with Accessibility Checking.
> http://p.sf.net/sfu/ephox-dev2dev
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: sam <as...@ya...> - 2011-06-17 11:50:07
|
Thank you
--- On Fri, 6/17/11, Robert Alatalo <ral...@gm...> wrote:
> From: Robert Alatalo <ral...@gm...>
> Subject: Re: [Dev-C++] Array increment in C
> To: "sam" <as...@ya...>
> Cc: dev...@li...
> Date: Friday, June 17, 2011, 4:26 AM
> Hello,
>
> As you go though the loop the responses [ answer ]
> will vary 1 , 2,
> 6, 4, 8 , etc...
> as it goes though the responses array ( hopefully
> there are then then
> 40 items in the array
> as that is the size of the array. Note the size of
> the frequency
> array is 11, it is just not fully initialized (though it
> should
> default to zeros as it's on the stack, and the {0} may be
> repeated to
> fill all 11 slots.
>
> Since the frequency array has 11 slots, it should start out
> as
> 0,0,0,0,0,etc...
>
> As you go though the loop, you increment each slot that is
> indexed by
> the value of responses [ answer ] , ie.. 1, 2, 6, 4, ...
> as the largest value I see if 10, that should not overflow
> the 0 to 10
> index of the 11 element array.
>
> At the end you just look though the frequency array,
> skipping the
> first element (index zero).
>
> Hope that helps,
> Robert
>
> On Fri, Jun 17, 2011 at 5:56 AM, sam <as...@ya...>
> wrote:
> > Sorry it has been a while since I program.
> > It is a stupid question. So, please forgive my
> ignorance. Maybe I lose my memory or something!!
> > I have a problem with the below code!!!
> > I can’t understand this part: ++frequency [
> responses [ answer ] ];
> > I know this part increment 1 to the value position of
> the result of this one responses [ answer ]
> > For example responses [ answer ]= 1; then
> ++frequency [1]
> > But how did the result of frequency[ rating ]=
> 2,2,2,2,5,11,5,7,1,3 ?????
> > So can you please explain it to me?
> >
> //////////////////////////////////////////////////////
> > #include <stdio.h>
> > #define RESPONSE_SIZE 40
> > #define FREQUENCY_SIZE 11
> > int main()
> > {
> > int answer, rating, frequency[ FREQUENCY_SIZE ] =
> { 0 };
> > int responses[ RESPONSE_SIZE ] =
> > { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10,
> > 1, 6, 3, 8, 6, 10, 3, 8, 2, 7,
> > 6, 5, 7, 6, 8, 6, 7, 5, 6, 6,
> > 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };
> > for ( answer = 0; answer <= RESPONSE_SIZE - 1;
> answer++ )
> > ++frequency [ responses [ answer ] ];
> > printf( "%s%17s\n", "Rating", "Frequency" );
> > for ( rating = 1; rating <= FREQUENCY_SIZE -
> 1; rating++ )
> > printf( "%6d%17d\n", rating, frequency[
> rating ] );
> > getchar();
> > return 0;}
> > //////////////////////
> >
> >
> ------------------------------------------------------------------------------
> > EditLive Enterprise is the world's most technically
> advanced content
> > authoring tool. Experience the power of Track Changes,
> Inline Image
> > Editing and ensure content is compliant with
> Accessibility Checking.
> > http://p.sf.net/sfu/ephox-dev2dev
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
>
|
|
From: Per W. <pw...@ia...> - 2011-06-17 16:31:41
|
It doesn't matter if frequency[] is a global or is stored on the stack. It
is initialized by {0}, where the first entry gets the explicit value 0,
and the other entries gets a default of 0.
So there are no repetition of any {0}. If the code had been (global or
stack variable doesn't matter):
history[11] = {1}
then history[] would have been filled with {1,0,0,0,0,0,0,0,0,0,0};
If there hadn't been any assignment, then it would still have been set to
zero if global, but left with unknown contents if it had been on the
stack (as it currently is).
/pwm
On Fri, 17 Jun 2011, sam wrote:
> Thank you
>
> --- On Fri, 6/17/11, Robert Alatalo <ral...@gm...> wrote:
>
> > From: Robert Alatalo <ral...@gm...>
> > Subject: Re: [Dev-C++] Array increment in C
> > To: "sam" <as...@ya...>
> > Cc: dev...@li...
> > Date: Friday, June 17, 2011, 4:26 AM
> > Hello,
> >
> > As you go though the loop the responses [ answer ]Â
> > will vary 1 , 2,
> > 6, 4, 8 , etc...
> > as it goes though the responses array ( hopefully
> > there are then then
> > 40 items in the array
> > as that is the size of the array. Note the size of
> > the frequency
> > array is 11, it is just not fully initialized (though it
> > should
> > default to zeros as it's on the stack, and the {0} may be
> > repeated to
> > fill all 11 slots.
> >
> > Since the frequency array has 11 slots, it should start out
> > as
> > 0,0,0,0,0,etc...
> >
> > As you go though the loop, you increment each slot that is
> > indexed by
> > the value of responses [ answer ] , ie.. 1, 2, 6, 4, ...
> > as the largest value I see if 10, that should not overflow
> > the 0 to 10
> > index of the 11 element array.
> >
> > At the end you just look though the frequency array,
> > skipping the
> > first element (index zero).
> >
> > Hope that helps,
> > Robert
> >
> > On Fri, Jun 17, 2011 at 5:56 AM, sam <as...@ya...>
> > wrote:
> > > Sorry it has been a while since I program.
> > > It is a stupid question. So, please forgive my
> > ignorance. Maybe I lose my memory or something!!
> > > I have a problem with the below code!!!
> > > I canât understand this part:Â Â Â Â ++frequency [
> > responses [ answer ] ];
> > > I know this part increment 1 to the value position of
> > the result of this one responses [ answer ]
> > > For example responses [ answer ]= 1; then
> > Â Â Â ++frequency [1]
> > > But how did the result of frequency[ rating ]=
> > 2,2,2,2,5,11,5,7,1,3 ?????
> > > So can you please explain it to me?
> > >
> > //////////////////////////////////////////////////////
> > > #include <stdio.h>
> > > #define RESPONSE_SIZE 40
> > > #define FREQUENCY_SIZE 11
> > > int main()
> > > {
> > > Â Â int answer, rating, frequency[ FREQUENCY_SIZE ] =
> > { 0 };
> > > Â Â int responses[ RESPONSE_SIZE ] =
> > > Â Â Â Â Â { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10,
> > > Â Â Â Â Â Â Â 1, 6, 3, 8, 6, 10, 3, 8, 2, 7,
> > > Â Â Â Â Â Â Â 6, 5, 7, 6, 8, 6, 7, 5, 6, 6,
> > > Â Â Â Â Â Â Â 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };
> > > Â Â for ( answer = 0; answer <= RESPONSE_SIZE - 1;
> > answer++ )
> > > Â Â Â Â ++frequency [ responses [ answer ] ];
> > > Â Â printf( "%s%17s\n", "Rating", "Frequency" );
> > > Â Â for ( rating = 1; rating <= FREQUENCY_SIZE -
> > 1; rating++ )
> > > Â Â Â Â Â printf( "%6d%17d\n", rating, frequency[
> > rating ] );
> > > Â Â getchar();
> > > Â Â return 0;}
> > > //////////////////////
> > >
> > >
> > ------------------------------------------------------------------------------
> > > EditLive Enterprise is the world's most technically
> > advanced content
> > > authoring tool. Experience the power of Track Changes,
> > Inline Image
> > > Editing and ensure content is compliant with
> > Accessibility Checking.
> > > http://p.sf.net/sfu/ephox-dev2dev
> > > _______________________________________________
> > > Dev-cpp-users mailing list
> > > Dev...@li...
> > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> > >
> >
>
> ------------------------------------------------------------------------------
> EditLive Enterprise is the world's most technically advanced content
> authoring tool. Experience the power of Track Changes, Inline Image
> Editing and ensure content is compliant with Accessibility Checking.
> http://p.sf.net/sfu/ephox-dev2dev
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|