Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Loops and compitency of recent CS grads

Reply
Thread Tools

Loops and compitency of recent CS grads

 
 
Josh Mcfarlane
Guest
Posts: n/a
 
      10-28-2005
Just sort of curious because of a trend I've been noticing.

Around here, most of the newer students and even some professionals
that pick up C++ or another similar language tend to do inefficient
things with loops / iterations, such as:

array[0] = 0;
for (int i =1; i < size; ++i)
{
array[i] = i;
}

or my favorite
for (int i = 0; i < 2*size; ++i)
{
if (i % 2 == 0)
dofunct(array[i/2]);
else
dootherfunct(array[i/2]);
}

Is proper iteration really that hard of a concept to grasp?

 
Reply With Quote
 
 
 
 
int2str@gmail.com
Guest
Posts: n/a
 
      10-28-2005

It's "competency"

Josh Mcfarlane wrote:
> array[0] = 0;
> for (int i =1; i < size; ++i)
> {
> array[i] = i;
> }


I may be outing myself as incompetent here, but what's wrong with this
loop?

Cheers,
Andre

 
Reply With Quote
 
 
 
 
Josh Mcfarlane
Guest
Posts: n/a
 
      10-28-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> It's "competency"
>
> Josh Mcfarlane wrote:
> > array[0] = 0;
> > for (int i =1; i < size; ++i)
> > {
> > array[i] = i;
> > }

>
> I may be outing myself as incompetent here, but what's wrong with this
> loop?


Sorry for the mispelling. =P

Basically, in this example it may have been misleading, but I see code
where they duplicate the initial value.

A better example may have been

array[0] += x
for (int i = 1; i < size; ++i)
{
array[i] += x;
}

Or some other common operation that needs to be performed on the entire
iteration. To me, it's code duplication and more of a hassle to
maintain code where the initial iteration (0) is outside of the loop,
while the remaining iterations are inside.

Josh McFarlane

 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      10-28-2005
On 2005-10-28, (E-Mail Removed) <(E-Mail Removed)> wrote:
>
> It's "competency"
>
> Josh Mcfarlane wrote:
>> array[0] = 0;
>> for (int i =1; i < size; ++i)
>> {
>> array[i] = i;
>> }

>
> I may be outing myself as incompetent here, but what's wrong with this
> loop?


He may wish it were the more idiomatic:

for (int i = 0; i < size; ++i)
{
array[i] = i;
}

If I were to see the original code, with

for (int i = 1;

at the beginning, little alarms would start going off in my head,
and I'd have to waste time proving to myself it wasn't wrong.

--
Neil Cerutti
 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      10-28-2005
On 2005-10-28, Josh Mcfarlane <(E-Mail Removed)> wrote:
> Just sort of curious because of a trend I've been noticing.
>
> Around here, most of the newer students and even some professionals
> that pick up C++ or another similar language tend to do inefficient
> things with loops / iterations, such as:


> or my favorite
> for (int i = 0; i < 2*size; ++i)
> {
> if (i % 2 == 0)
> dofunct(array[i/2]);
> else
> dootherfunct(array[i/2]);
> }
>
> Is proper iteration really that hard of a concept to grasp?


The second loop just looks like nonsense. I can't think of an
improvement other than erasing it.

--
Neil Cerutti
 
Reply With Quote
 
Josh Mcfarlane
Guest
Posts: n/a
 
      10-28-2005

Neil Cerutti wrote:
> On 2005-10-28, Josh Mcfarlane <(E-Mail Removed)> wrote:
> > Just sort of curious because of a trend I've been noticing.
> >
> > Around here, most of the newer students and even some professionals
> > that pick up C++ or another similar language tend to do inefficient
> > things with loops / iterations, such as:

>
> > or my favorite
> > for (int i = 0; i < 2*size; ++i)
> > {
> > if (i % 2 == 0)
> > dofunct(array[i/2]);
> > else
> > dootherfunct(array[i/2]);
> > }
> >
> > Is proper iteration really that hard of a concept to grasp?

>
> The second loop just looks like nonsense. I can't think of an
> improvement other than erasing it.


It baffled me too, but more or less what they were trying to achieve
was

for(int i = 0; i < size; ++i)
{
dofunct(array[i]);
dootherfunct(array[i]);
}

Now, why they didn't do that, I have no clue.

 
Reply With Quote
 
Dave Townsend
Guest
Posts: n/a
 
      10-28-2005

"Josh Mcfarlane" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>
> Neil Cerutti wrote:
> > On 2005-10-28, Josh Mcfarlane <(E-Mail Removed)> wrote:
> > > Just sort of curious because of a trend I've been noticing.
> > >
> > > Around here, most of the newer students and even some professionals
> > > that pick up C++ or another similar language tend to do inefficient
> > > things with loops / iterations, such as:

> >
> > > or my favorite
> > > for (int i = 0; i < 2*size; ++i)
> > > {
> > > if (i % 2 == 0)
> > > dofunct(array[i/2]);
> > > else
> > > dootherfunct(array[i/2]);
> > > }
> > >
> > > Is proper iteration really that hard of a concept to grasp?

> >
> > The second loop just looks like nonsense. I can't think of an
> > improvement other than erasing it.

>
> It baffled me too, but more or less what they were trying to achieve
> was
>
> for(int i = 0; i < size; ++i)
> {
> dofunct(array[i]);
> dootherfunct(array[i]);
> }
>
> Now, why they didn't do that, I have no clue.
>

Your original post was about efficiency, but you seem to be commenting on
clarity
which is quite a different matter.



 
Reply With Quote
 
Josh Mcfarlane
Guest
Posts: n/a
 
      10-28-2005

Dave Townsend wrote:
> Your original post was about efficiency, but you seem to be commenting on
> clarity
> which is quite a different matter.


Well, by efficiency I was more referring to the efficiency of the
programmer and the overall effect it had on the code / program
development (in this case, it made debugging it more pesky as I had to
check two areas instead of one.)

Adding a logic check each loop and looping twice as much as necessary
seems inefficient to me as you're executing 2x the instructions
necessary.

 
Reply With Quote
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      10-28-2005
Josh Mcfarlane wrote:

> Around here, most of the newer students and even some professionals
> that pick up C++ or another similar language tend to do inefficient
> things with loops / iterations, such as:


I have seen many bad code over the years, don't think is a recent trend. In
C++, C, Java, Pascal, Basic....

--
Salu2
 
Reply With Quote
 
TIT
Guest
Posts: n/a
 
      10-28-2005
Josh Mcfarlane sade:
> Neil Cerutti wrote:
>
>>On 2005-10-28, Josh Mcfarlane <(E-Mail Removed)> wrote:
>>
>>>Just sort of curious because of a trend I've been noticing.
>>>
>>>Around here, most of the newer students and even some professionals
>>>that pick up C++ or another similar language tend to do inefficient
>>>things with loops / iterations, such as:

>>
>>>or my favorite
>>>for (int i = 0; i < 2*size; ++i)
>>>{
>>>if (i % 2 == 0)
>>>dofunct(array[i/2]);
>>>else
>>>dootherfunct(array[i/2]);
>>>}
>>>
>>>Is proper iteration really that hard of a concept to grasp?

>>
>>The second loop just looks like nonsense. I can't think of an
>>improvement other than erasing it.

>
>
> It baffled me too, but more or less what they were trying to achieve
> was
>
> for(int i = 0; i < size; ++i)
> {
> dofunct(array[i]);
> dootherfunct(array[i]);
> }
>
> Now, why they didn't do that, I have no clue.
>


A complex mind lacks simple thoughts =)

TIT
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dads & Grads Superstore best Cisco 0 05-21-2009 05:57 PM
Dads & Grads Superstore fighter ASP .Net 0 05-21-2009 03:48 PM
Dads & Grads Superstore diehard4.0 C Programming 0 05-21-2009 03:19 PM
Dads & Grads Superstore diehard4.0 Python 0 05-21-2009 03:19 PM
Opinions on college grads in this field Matthew Superstar Swass HTML 4 07-28-2004 03:44 PM



Advertisments