473,660 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Seriously struggling with C

RG
Greetings friends,

This semester I have started a course in C programming. I was moving
along fine until I reached to the topic of loops (feeling embarrassed
among you elite programmers). My prof. would post program questions
and the solutions online. For practice I would try to do the problems.
I would reach to a certain point in the code, for example as far as
error trapping, but when the loop arrives, like knowing whether to use
for, while do, how to properly use the increment and decrements, and
counters,I am just not proficient in it and the class is moving ahead.
Eventually i would have to look at the solution and wondering to
myself, the reason i could not think of it. What ticks me off is that
other kids are getting this stuff easily, while I am having a hard
time.Kindly advise me on what actions I shoul take. I would
particularly like to have an idea of the thought process to engage in
when given the programme to write.
Thanks for your time and consideration.

RG

Feb 20 '06 #1
160 4659
Hi!
I am not an expert in programming but I do like programming as it is
"mentally stimulating." The first thing that I will tell you is don't
compare yourselves with others, once you start doing that, you are
going to feel worse.
Try seeing programming like a game. First, try and build your own
solution from a mathematical perspective, without taking the
constraints of C programming. I wont say that it will be easy, but give
it a shot. Try to write an alogorithm, I dont use them much, but it
does help to understand the direction in which the program flows. So
once you have your solution try writing it in your own words. And then
in the end use the syntax of the programming language and try it out in
your editor.
I dont how much this will help you. I am a freshman myself, and I am
just telling you the way I see it. Just take it as a challenge and I am
sure you will be fine.
Take care and Good Luck.
Priyam

Feb 20 '06 #2
RG wrote:
This semester I have started a course in C programming. I was moving
along fine until I reached to the topic of loops (feeling embarrassed
among you elite programmers). My prof. would post program questions
and the solutions online. For practice I would try to do the problems.
I would reach to a certain point in the code, for example as far as
error trapping, but when the loop arrives, like knowing whether to use
for, while do, how to properly use the increment and decrements, and
counters,
one cause of your problems may be that there is no definitive right or
wrong answer. Anything you can do with a while() you can do with a
for(),
and the functionality of a for() can be duplicated with a while() and a

couple of other statements. C's for() is very powerful (some might
argue,
too powerful). I tend to classify loops into two main types.

- a fixed known number of iterations (times around the loop), for these
I
use for()
- an iterate until some condition is met

// loop n times
for (i = 0; i < n; i++)
do_something ();

I'd just learn this as a piece of boilerplate code to drop in when you
need a
known number of iterations.

// loop while some condition holds
while (condition)
do_something();

You can pretty much do anything with those two.
There is also a while at the end:-

do {
do_something()
}
while (condition);

but it practice this is far less useful. note it always does at least
one iteration.

A C idiom for a loop that goes "forever"

for (;;) // never stops
{
do_something();
if (exit_condition )
break;
do_something_el se();
}

this can sometimes be useful, I'd avoid it until you are comfortable
with the first two forms.

Where C can be confusing is when the "cleverness " of the for() is
exploited.

// walk a linked list
for (i = head; i != NULL; i = i->next)
flop_node (i->data);

If this confuses you, use a while()

for (i = 1, j = 1; i < k, j < l; i++, j + 2)
something_scary (i, j);

if this confuses you... chop the program listing into little pieces and
feed it
to the author of the code :-)

Keep It Simple is the first lore of programming
I am just not proficient in it and the class is moving ahead.
Eventually i would have to look at the solution and wondering to
myself, the reason i could not think of it. What ticks me off is that
other kids are getting this stuff easily, while I am having a hard
time.Kindly advise me on what actions I shoul take. I would
particularly like to have an idea of the thought process to engage in
when given the programme to write.

--
Nick Keighley

The fscanf equivalent of fgets is so simple
that it can be used inline whenever needed:-
char s[NN + 1] = "", c;
int rc = fscanf(fp, "%NN[^\n]%1[\n]", s, &c);
if (rc == 1) fscanf("%*[^\n]%*c);
if (rc == 0) getc(fp);

Feb 20 '06 #3
RG wrote:
Greetings friends,

This semester I have started a course in C programming. I was moving
along fine until I reached to the topic of loops (feeling embarrassed
among you elite programmers). My prof. would post program questions
and the solutions online. For practice I would try to do the problems.
I would reach to a certain point in the code, for example as far as
error trapping, but when the loop arrives, like knowing whether to use
for, while do, how to properly use the increment and decrements, and
counters,I am just not proficient in it and the class is moving ahead.
Eventually i would have to look at the solution and wondering to
myself, the reason i could not think of it. What ticks me off is that
other kids are getting this stuff easily, while I am having a hard
time.Kindly advise me on what actions I shoul take. I would
particularly like to have an idea of the thought process to engage in
when given the programme to write.
Thanks for your time and consideration.


Please, provide an example of something you don't grasp. The while loop
is probably the easiest loop construct to understand.

Example:

/* Show the contents of the array a of length len. */
k = 0;
while (k < len) {
printf("%d\n", a[k]);
k++;
}
/* Just by looking at the loop guard, we know that k >= len here
(actually k == len). */
August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
Feb 20 '06 #4

"RG" <su************ @hotmail.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
Greetings friends,

This semester I have started a course in C programming. I was moving
along fine until I reached to the topic of loops <snip> like knowing whether to use
for, while do, how to properly use the increment and decrements, and
counters, <snip> I would
particularly like to have an idea of the thought process to engage in
when given the programme to write.


This is my thought process on loops:
1) while - never use
2) for - use as much as possible
3) do while - only use when necessary

I'll explain. The while loop only checks for a single statement being true.
It is very simple. So simple in fact that it really is useless. It
duplicates functionality that exists in a for loop. Therefore, why use it?
Many C programmers don't. while(1) becomes for(;;). while (a<b) becomes
for(;a<b;) .The for loop is a very powerful loop construct that can
implement almost any loop. The only time you don't want to use a for loop
is when the block of code in the loop needs to execute at least once. When
the code 'needs to execute at least once,' you use the do while loop. The
last flow control constructs are procedures, functions, and switches.
Procedures and functions aren't usually used for looping. The switch
statement is useful any time the data or a counter you've created doesn't or
won't increment or decrement by one.

The next step is to 'see' how a for loop works when it is written as a while
loop:

i=0;
while(i<10)
{
/* continue starts here for while() */
/* ...code body... */
/* continue starts here for for() */
i++;
}

That is the same as this for loop:

for (i=0; i<10; i++)
{
/* ...code body... */
}

The continue statement in a while continues execution at the top of the
while. But, for the for loop, execution continues before the third
expression (i++).
Rod Pemberton
Feb 20 '06 #5

RG wrote:
Greetings friends,

This semester I have started a course in C programming. I was moving
along fine until I reached to the topic of loops (feeling embarrassed
among you elite programmers). My prof. would post program questions
and the solutions online. For practice I would try to do the problems.
I would reach to a certain point in the code, for example as far as
error trapping, but when the loop arrives, like knowing whether to use
for, while do, how to properly use the increment and decrements, and
counters,I am just not proficient in it and the class is moving ahead.
Eventually i would have to look at the solution and wondering to
myself, the reason i could not think of it. What ticks me off is that
other kids are getting this stuff easily, while I am having a hard
time.Kindly advise me on what actions I shoul take. I would
particularly like to have an idea of the thought process to engage in
when given the programme to write.
Thanks for your time and consideration.

RG


Go to www.bloodshed.net and download their free C/C++ compiler.
Then use it to test out your code to learn how to program in C/C++.

Bill Hanna

Feb 20 '06 #6
"Rod Pemberton" <do*********@so rry.bitbucket.c mm> writes:
"RG" <su************ @hotmail.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
Greetings friends,

This semester I have started a course in C programming. I was moving
along fine until I reached to the topic of loops <snip>
like knowing whether to use
for, while do, how to properly use the increment and decrements, and
counters,

<snip>
I would
particularly like to have an idea of the thought process to engage in
when given the programme to write.


This is my thought process on loops:
1) while - never use
2) for - use as much as possible
3) do while - only use when necessary

I'll explain. The while loop only checks for a single statement being true.


Condition, not statement.
It is very simple. So simple in fact that it really is useless. It
duplicates functionality that exists in a for loop. Therefore, why use it?


That's bad advice. Use while() whenever it's the most natural
construct to use. Arbitrarily transforming "while (condition)" into
"for (;condition;)" is foolish.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 20 '06 #7
bi*****@aol.com writes:
RG wrote:
Greetings friends,

This semester I have started a course in C programming. I was moving
along fine until I reached to the topic of loops (feeling embarrassed
among you elite programmers). My prof. would post program questions
and the solutions online. For practice I would try to do the problems.
I would reach to a certain point in the code, for example as far as
error trapping, but when the loop arrives, like knowing whether to use
for, while do, how to properly use the increment and decrements, and
counters,I am just not proficient in it and the class is moving ahead.
Eventually i would have to look at the solution and wondering to
myself, the reason i could not think of it. What ticks me off is that
other kids are getting this stuff easily, while I am having a hard
time.Kindly advise me on what actions I shoul take. I would
particularly like to have an idea of the thought process to engage in
when given the programme to write.
Thanks for your time and consideration.


Go to www.bloodshed.net and download their free C/C++ compiler.
Then use it to test out your code to learn how to program in C/C++.


How is that useful? Since the OP is taking a course in C programming,
it's safe to assume he already has access to a C compiler. If there's
something about the bloodshed compiler that makes it particularly
useful for beginners, you might want to say so.

BTW, what do you mean by "C/C++ compiler"? There is no "C/C++" language.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 20 '06 #8

"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Rod Pemberton" <do*********@so rry.bitbucket.c mm> writes:
"RG" <su************ @hotmail.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
Greetings friends,

This semester I have started a course in C programming. I was moving
along fine until I reached to the topic of loops <snip>
like knowing whether to use
for, while do, how to properly use the increment and decrements, and
counters,

<snip>
I would
particularly like to have an idea of the thought process to engage in
when given the programme to write.


This is my thought process on loops:
1) while - never use
2) for - use as much as possible
3) do while - only use when necessary

I'll explain. The while loop only checks for a single statement being true.
Condition, not statement.
Expression, not condition? I doubt the OP is familiar with any language or
definitions of ISO/ANSI specifications, so please don't criticize me for
phrasing to his level of understanding.
It is very simple. So simple in fact that it really is useless. It
duplicates functionality that exists in a for loop. Therefore, why use

it?
That's bad advice. Use while() whenever it's the most natural
construct to use. Arbitrarily transforming "while (condition)" into
"for (;condition;)" is foolish.


Not using the braces of a compound-statement for while's and if-else's when
there is a single statement is also the most natural construct to use. But,
of course, as we've seen many times here, that leads to later problems.
People forget to add the braces when a statement is converted to a
compound-statement. The same holds true for the while statement. A while
will frequently be converted to a for sometime in the future. At which
point, the programmer is likely to introduce an error. Why not just do it
manner which reduces the chance that an error, now or in the future, is
introduced? I've seen you suggest this on many occasions. I find it odd
that you'd take a contrary position now.

Rod Pemberton
Feb 20 '06 #9
Keith Thompson wrote:
"Rod Pemberton" <do*********@so rry.bitbucket.c mm> writes:

[about the while-statement]
It is very simple. So simple in fact that it really is useless. It
duplicates functionality that exists in a for loop. Therefore, why use it?


That's bad advice. Use while() whenever it's the most natural
construct to use. Arbitrarily transforming "while (condition)" into
"for (;condition;)" is foolish.


I agree. Moreover, the advantage of the "while" statement over the C
"for" statement is its clear semantics. But hey, how many C programmers
care about that ;-).
August

--
"C is what happened when Algol and Fortran got drunk and spent the
weekend together." -- Scott More (from the thread "Java or C++" in
comp.programmin g 2005.10.19)
Feb 20 '06 #10

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

Similar topics

22
2071
by: Stan | last post by:
Hey everyone, I've got a computer science class and we're working on C++. I am struggling with nested loops and have a really simple assignment, but I can't get it to run the loop. I need to run a loop that has someone guess at a number 5 times and if they get 45, it tells them how many guesses they needed. It also has to tell them if they leave the range of guesses from 0 to 100. I need help. Can somebody help me please!!!
4
2002
by: Rowan | last post by:
Hi there, it's me again. I am having trouble with a view again. I am trying to do a calculation, but there are some checks that need to be taken into consideration. Maybe a view is not the right way to deal with this. I don't know. This is the beginning of my query. SELECT coalesce(f.filenumber, i.filenumber) as filenumber, i.InvoiceNumber, i.InvoiceValue, il.lineid, MPF = .21 * (il.UnitCost * il.UnitQty + il.AddMMV - il.MinusMMV -...
26
6750
by: dagger | last post by:
Hi there. I'm using C under FreeBSD with the gcc compiler and am having a bit of trouble using the calloc and realloc calls. As an example the code snippet: #include <stdio.h> int main() { char *ptr;
4
5593
by: Angus Comber | last post by:
Hello I have received a lot of help on my little project here. Many thanks. I have a struct with a string and a long member. I have worked out how to qsort the struct on both members. I can do a bsearch on the long member (nKey) but I am struggling to do a search using the string member. The code I am running appears below. It doesn't crash or anything. It is just that when I do the last bsearch using "192.168.1.3" I SHOULD find...
5
4056
by: | last post by:
I am really struggling to conceptually understand classes in C# especially the use of the strange keywords 'static' 'void' and 'override'.... Is there a 'idiots' way of understanding these concepts broadly before even touching a line of code.... Thanks Jason
2
1268
by: mjeaves | last post by:
Hello there, Hope someone can help. I have data arriving in a table, a Cron job is triggered when the data arrives. ---- table_1 id (unique)
97
5484
by: Master Programmer | last post by:
An friend insider told me that VB is to be killled off within 18 months. I guess this makes sence now that C# is here. I believe it and am actualy surprised they ever even included it in VS 2003 in the first place. Anyone else heard about this development? The Master
0
1037
by: Steve | last post by:
I'm struggling to get proper runtime and designtime dpi values. In the forms .designer.vb I've added the following code after InitializeComponent() Me.AutoScaleDimensions = New System.Drawing.SizeF(96.0F, 96.0F) Me.AutoScaleMode = Windows.Forms.AutoScaleMode.Dpi And in my code behind the form, I've added,
11
22797
by: briankind | last post by:
Hello i have these radio buttons and i wish to have the corresponding textboxes to be visible when the radiobutton is selected. any Help? snippets. thanks thanks in adv Bry
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8542
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2760
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1740
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.