473,396 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Loops and compitency of recent CS grads

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?

Oct 28 '05 #1
15 1528

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

Oct 28 '05 #2
in*****@gmail.com 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

Oct 28 '05 #3
On 2005-10-28, in*****@gmail.com <in*****@gmail.com> 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
Oct 28 '05 #4
On 2005-10-28, Josh Mcfarlane <da*****@gmail.com> 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
Oct 28 '05 #5

Neil Cerutti wrote:
On 2005-10-28, Josh Mcfarlane <da*****@gmail.com> 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.

Oct 28 '05 #6

"Josh Mcfarlane" <da*****@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...

Neil Cerutti wrote:
On 2005-10-28, Josh Mcfarlane <da*****@gmail.com> 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.

Oct 28 '05 #7

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.

Oct 28 '05 #8
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
Oct 28 '05 #9
TIT
Josh Mcfarlane sade:
Neil Cerutti wrote:
On 2005-10-28, Josh Mcfarlane <da*****@gmail.com> 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
Oct 28 '05 #10
TIT wrote:
A complex mind lacks simple thoughts =)


Sometimes, but rarely do these over-the-top cases work as intended,
which is part of what brought about this post.

Oct 28 '05 #11

Josh Mcfarlane wrote:
TIT wrote:
A complex mind lacks simple thoughts =)


Sometimes, but rarely do these over-the-top cases work as intended,
which is part of what brought about this post.

Why don't you show us a sample of your code? Thus, the coding problems
may be inherent to a completely different source. Additionally, your
English writing doesn't reflect any teacher/professorial
attributes...can't say no more, for it is already off-topic...

Oct 29 '05 #12
Ian
Josh Mcfarlane 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:

Many people "know" a language, which often means one way of doing
something and they are often unwilling to experiment or expand their
knowledge. I've seen this a lot over the years and not just with junior
staff.

Ian
Oct 29 '05 #13
puzzlecracker wrote:
Why don't you show us a sample of your code? Thus, the coding problems
may be inherent to a completely different source. Additionally, your
English writing doesn't reflect any teacher/professorial
attributes...can't say no more, for it is already off-topic...


This wasn't about my code. This was about a common problem I noticed
with loops and some of the programmers around here. I end up having to
go in and fix things, not because they're unclear in meaning, but in
attempting to be complex or doing things in the first mentioned
example, they screw something up (such as record iteration) and it
brings in undefined behavior.

Oct 29 '05 #14

"Josh Mcfarlane" <da*****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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;
}
A few ways this could come about. Some new programmer trying to think how
to do something to a variable. "lets see. I want to initialize it to 0.
That would be X = 0. Oh, X is an array, so I need X[0] = 0. There got it.
Oh, gotta do the rest. lets see, already did 0 so..."

Or, most likely. "Ahh, let me initialize my size variable array. for (int
i = 1; i < size; ++i)" later "Dang, what bug, oh, forgot to initialize 0.
Umm.. I'll just throw it up top."

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?


Actually, for people new to programming iteration is a total mystery. They
still have a hard time understanding how X = X + 1 can be true when their
algebra teacher told them both sides need to be equal...

Just unlearning a lot of things and learning back over.

I remember going through this phase many many years ago. When I didn't even
understand 2/3 of what I wrote, but it worked dag nab it so it must be
right! I better not touch it or I might break it again...
Oct 29 '05 #15
TIT
Josh Mcfarlane sade:
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:


I doubt that you'll only find irritating code in iterations. The
recent decade of mass production of programmers may have led to
a decay of the avarage programmers capabilities.

I once accused a c++ teacher at a university in 2001 for
teaching the concept of pointers way too late, and he replied:
"You know, in Java you don't even have pointers."

TIT
Oct 29 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: lawrence | last post by:
Am I right to say mysql_data_seek allows me to walk through a database return as I would an array? for ($1=0; $i < $num; $i++) { mysql_data_see($result, $i); $row =...
1
by: python-list | last post by:
Hello, I posted this to the tutor list, but didn't get any responses, unless my email client really messed up. So I'll try here. I'm starting to work with threads, but I'm a little confused. I...
9
by: Klaus Neuner | last post by:
Hello, I wrote a program that does essentially the following: for rule in rules: for line in line_list: line = my_apply(rule, line) line_list contains the lines of some input text.
15
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
7
by: Efrain Marrero | last post by:
i want to now how to do this in python this is java for(int i=1 ; i<=lim ; i++){ for(int j=i+1; j<=lim+1; j++){ for(int k =j+1; k<=lim+2;k++){
17
by: John Salerno | last post by:
I'm reading Text Processing in Python right now and I came across a comment that is helping me to see for loops in a new light. I think because I'm used to the C-style for loop where you create a...
10
by: Putty | last post by:
In C and C++ and Java, the 'for' statement is a shortcut to make very concise loops. In python, 'for' iterates over elements in a sequence. Is there a way to do this in python that's more concise...
4
by: luislupe | last post by:
Hi, I've created a method where the script defines twenty variables and several of them should be random having a maximum and a minimum value. What I did was this: from random import...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.