LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-29-2019, 05:33 AM   #106
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,266

Original Poster
Rep: Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691

I fear it wouldn't be easy for me to explain that RT

I'll try what you said about the spreadsheet and what GazL and others said about writing it down. There's also something else that I'll be trying as well that might help.

But thanks for explaining the above nonetheless.
 
Old 10-29-2019, 07:38 AM   #107
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,187
Blog Entries: 13

Rep: Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601
Quote:
Originally Posted by jsbjsb001 View Post
I fear it wouldn't be easy for me to explain that
Can I still please ask that you do try to explain, this specifically:
Quote:
Originally Posted by rtmistler View Post
Perhaps explain for me how "you" would think about single stepping through that for loop as it looks at and processes "line 1\n\0"
My reason being is that it would be helpful to learn how you would trace through that one loop line of logic, and it would give your fellow LQ members some insight into exactly how you are approaching these types of analyses. Otherwise things here stand to continue with each of us offering various techniques and not seeing that any message has been conveyed or interpreted as anticipated.
 
1 members found this post helpful.
Old 10-29-2019, 09:27 AM   #108
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,266

Original Poster
Rep: Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691
While I'm not really sure if you mean as in, "doing it in my head", or "writing it out" (be that in a spreadsheet or on paper); as much as I don't really like to assume too many things, I'll assume you mean the former (doing it in my head)...

Perhaps it's easier to say; it's not particularly hard to imagine that the while loop (the most outer loop in my original program) calls fgets() to upload a line from the file into the array, then the for loop scans through that same line looking for a hash, then depending on what it finds, does x,y or z (whatever the if statements within dictate), then the program repeats the process until it's finished reading all of the lines in the file. That's how I would think about it in my head, but trying to follow the for loop in detail just in my head with the if's in it, that's where I end up getting lost.

Even if I loaded the program up in gdb, and then set a breakpoint, then typing "n" to run statements one by one, it's still very difficult to try and keep track of where exactly it's at, and why it's at where it's at - even if I know what I intended and tried to do/write, what's on the line(s), etc. I know it sounds funny, I know, but it would still be the same story regardless of exactly what's on the line, be that your example line or anything else. It's ok when I look at your post #105, because I can clearly see the content of each array element, the value i has each time, and the result of the test each time. But gdb for one doesn't exactly show me that kind of detail, and even if I print the value of for example i, I tend to lose track of it - particularly where there's multiple lines in the file to process.

It's ok with other people's non-programming related technical problems/questions that I respond to here, because I can generally just focus on one possible cause/reason/etc at a time, without having to try and keep track of multiple values, variables, etc all at once. And even with multiple variables/details, it's still much easier because for lack of a better answer, I can to a large extent focus on what the program is supposed to do at a high or high-ish level, and if it isn't working as it should, think of what situation could be the cause of that, then think about the OP's situation/what they've described about their problem, etc. In any case, it's similar to "you can know what a kernel is and does, but it doesn't mean you can write one yourself, even if you can compile one yourself".

Not really sure how else to explain, so that's probably the best I can explain it.
 
Old 10-29-2019, 12:45 PM   #109
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,187
Blog Entries: 13

Rep: Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601
Quote:
Originally Posted by jsbjsb001 View Post
While I'm not really sure if you mean as in, "doing it in my head", or "writing it out" (be that in a spreadsheet or on paper); as much as I don't really like to assume too many things, I'll assume you mean the former (doing it in my head)...
Thanks. It does help. I think there are benefits with doing things in one's head, but a point of concern here is that thinking through the progress of an algorithm solely within one's mind can rapidly lead to a situation where you have too much to remember and consider. I know you are aware of this, and suggest that you consider various ways to reset things if or when you do get into the too highly detailed situation, so that you can find a clear way to get back to a known state of clear understanding.

Shorter comment: Great, you're doing it in your head. I'm telling you that you're missing stuff and that it's not working.
Quote:
Originally Posted by jsbjsb001 View Post
Perhaps it's easier to say; it's not particularly hard to imagine that the while loop (the most outer loop in my original program) calls fgets() to upload a line from the file into the array, then the for loop scans through that same line looking for a hash, then depending on what it finds, does x,y or z (whatever the if statements within dictate), then the program repeats the process until it's finished reading all of the lines in the file. That's how I would think about it in my head, but trying to follow the for loop in detail just in my head with the if's in it, that's where I end up getting lost.
I think here is a disconnect. What I was asking about was that one line, and one loop. You are talking about the entire processing algorithm. This is both OK, as well as a problem. My point is to be able to have the capability to identify a point where you may need to debug, or at least test and verify so that you know for sure it does what you need it to do, for all your test cases. That for() loop processes each line. The outer while() loop just introduces each line to the for() loop, or this should be the intention. Therefore the recommendation is to debug, refine, and test thoroughly the for() loop which processes each individual line. After which point you should then determine whether or not the while() loop is performing as needed.

By combining analysis of both loops together in your mind analysis, or in a paper analysis means that you are failing to segment the debug of things and keeping things too complicated at this time.

Shorter comment: Please debug ONLY that for() loop, because it seems clear to me that it can be written differently, such as the end test condition is not something I recommend using the way it is coded.
Quote:
Originally Posted by jsbjsb001 View Post
Even if I loaded the program up in gdb, and then set a breakpoint, then typing "n" to run statements one by one, it's still very difficult to try and keep track of where exactly it's at, and why it's at where it's at - even if I know what I intended and tried to do/write, what's on the line(s), etc. I know it sounds funny, I know, but it would still be the same story regardless of exactly what's on the line, be that your example line or anything else. It's ok when I look at your post #105, because I can clearly see the content of each array element, the value i has each time, and the result of the test each time. But gdb for one doesn't exactly show me that kind of detail, and even if I print the value of for example i, I tend to lose track of it - particularly where there's multiple lines in the file to process.
As far as the debugger goes, it can be tedious, no easy answer because it does take time. I've demonstrated that I use GDB through emacs in my blog about debugging. Using it in that manner, you can have a watch window. A watch window is where you can specify variables that you wish to "watch", so at each breakpoint and at each single step, the variables in that watch list/window, get updated. This provides convenience, but is not absolutely necessary.

One does not need to use a watch window, or to set variables up for being watched. Even if you use the command line GDB, if you declare a variable as being watched, I believe what happens when you step, or break, or maybe only if there are changes to a watched variable, the new prompt, plus output, includes the changes to the watched variables.

But not using the concept of watching at all is perfectly fine. One can just print 'p' from the debugger at each stopping point, as I demonstrated in an earlier post and also as shown in that blog. The up arrow helps, or just typing repetitively the same 4, 5, or 6 commands in secession, so that you can fill out your table in your spreadsheet.

The bottom line is that it is all organizational. At each step, verify the variables needed. Do not run or take another step until you've verified, and be very cautious about choosing to run. Instead single step, or set enough breakpoints so that when you hit library calls like printf(), you can step over them, or run and hit a breakpoint so that you do not miss the next iteration of your loop.

And on top of this, you can write things into a spreadsheet, notepaper, scrap paper.*** see below

I no longer do this level of detail, but only because previously I have done this detail, and have learned from those experiences.

You do not have those prior experiences, so the recommendation is that you do try to practice something like this tedious form of debugging.

While there are different ways to do this, single step, breakpoints, watch variables, or printing in the debugger, these tedious steps and the attention to diagnosing one area of code completely will help you as you progress to more complex designs.

Shorter comment: I KNOW it's pain in the backside and it takes a LOT of time and effort, but ... you're going to benefit from it.

*** Addendum: About writing things down. Recent place I worked, "the boss" would print out lots of stuff and bring it to me to ensure that I "saw" it, that I knew about it, it was just his way. Got it. Caused me to have a lot of leftover stuff, which I put in a pile on one of my shelves.

Guess what I largely did with that?

I'd flip over the printed junk, because it was all single sided, tree waste, and I'd use that as scratch note paper for design discussions, and detailed analysis.

So when I say "use a spreadsheet" is does not mean EasyCalc, Excel, or a program, it can be on a piece of paper, and that typically is how I do it.

When I talk about GDB and either watching variables or printing variables, those are notifications and information which you "see", but the storing of it, is manually writing it with a 0.5mm mechanical pencil (just my style) onto that scrap paper, in some form of table/organized fashion.

So I'd have a sheet where I analyzed, "line 1\n\0".
I'd have a sheet where I analyzed, "# this is a comment line\n\0"
I'd have a sheet where I analyzed, "This is a partial # comment line\r\n\0"
I'd have a sheet (maybe multiple ones) where I analyzed, "$%&@!()FGJUIOLEHJTGF*()FYH_(FUYE)GF_I*)(EYH(F0ut0puw80frrg-09uf08iyt-9wuf9-53\b\b\\b\b\b\t\t\t\t\r\n\0\r\r\r\0"

I'm never saying this is easy, a throw in the bucket, but I am trying to convey a path to follow, agreeing that it takes a lot of commitment, but also showing the benefits from it.
 
2 members found this post helpful.
Old 11-06-2019, 03:19 AM   #110
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 899

Rep: Reputation: 209Reputation: 209Reputation: 209
At some stage, one calls in a peer review and in this case the peers appear to be participants in this thread.

Since the participants in this thread are remote (to you), it helps to put it down in a step by step output (with the help of
Quote:
fprintf(text_file_contaning_errors_which_is_previously_fopened,"%d:%s\n",i,content[i]);
and so forth.

Do that with every suspect variable.

If you put it in a spreadsheet, you can sort, group etc.

OK
 
1 members found this post helpful.
Old 11-06-2019, 09:38 AM   #111
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,375

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
some people/organizations spend many hours on a project before putting it into production. This requires logical boards and flow charts all put on paper and/or chalkboards, or white boards. Before one letter of code it written into a file.
 
1 members found this post helpful.
Old 11-07-2019, 12:52 AM   #112
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,266

Original Poster
Rep: Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691
Thanks guys. Sorry I took a little while, but I've had other things that got in the way.

I tried debugging it with gdb, while printing the values of i and content[i] and writing it down in a spreadsheet in LibreOffice Calc, but gdb was only showing me the values of the first non-space character in the content array. I also tried using the printf astrogeek posted earlier on, but that wasn't showing me the value of all of the elements for the content array either. So it's hard to do it on paper if I can't see what the current value of the content array element is.

So I don't know how I'm supposed to figure it out now.
 
Old 11-07-2019, 07:50 AM   #113
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,187
Blog Entries: 13

Rep: Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601
Quote:
Originally Posted by jsbjsb001 View Post
but gdb was only showing me the values of the first non-space character in the content array.
GDB will show you the entire array.
It can show you the whole array or character by character.

Example code:
Code:
void main(void)
{
    char content[16] = "abcde\0";

    if(content[0] == 0) {
        content[15] = 0;
    }
}
Sample GDB session:
Code:
Reading symbols from sample...done.
(gdb) b 5
Breakpoint 1 at 0x1004010a3: file sample.c, line 5.
(gdb) r
Starting program: sample
"sample" hit Breakpoint 1, main () at sample.c:5
5           if(content[0] == 0) {
(gdb) p content
$1 = "abcde\000\000\000\000\000\000\000\000\000\000"
(gdb) p content[0]
$2 = 97 'a'
(gdb) p content[1]
$3 = 98 'b'
(gdb) p content[2]
$4 = 99 'c'
(gdb) p content[3]
$5 = 100 'd'
(gdb) p content[4]
$6 = 101 'e'
(gdb) p content[5]
$7 = 0 '\000'

### It can also show you the variables in HEX

(gdb) p/x content
$8 = {0x61, 0x62, 0x63, 0x64, 0x65, 0x0 <repeats 11 times>}
(gdb) p/x content[0]
$9 = 0x61
(gdb) p/x content[1]
$10 = 0x62
(gdb) p/x content[2]
$11 = 0x63
(gdb) p/x content[3]
$12 = 0x64
(gdb) p/x content[4]
$13 = 0x65
(gdb) p/x content[5]
$14 = 0x0
(gdb)
What do you see instead when you try this?
 
1 members found this post helpful.
Old 11-09-2019, 06:16 AM   #114
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,266

Original Poster
Rep: Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691
Thanks RT. I tried what you said above and while I can see what you mean. But trying to follow just the for loop in my head, or even trying to understand the steps it's taking to write it down (on paper or in a spreadsheet) is still virtually impossible. Even trying to use gdb to figure out the steps it's taking is still virtually impossible for me. Let alone trying to work it out like GazL posted in post #99. I'm sorry, but I'm honestly lost trying to figure out what GazL did in post #99 - I only know that's what's happening because GazL posted it before, whereas had they not posted that, I'd still be in the dark about all of that.

Quote:
Originally Posted by rtmistler View Post
...What do you see instead when you try this?
I done what you done above, and here's gdb's output for you;

Code:
Reading symbols from skip_line_if_comment...done.
(gdb) b 21
Breakpoint 1 at 0x400719: file skip_line_if_comment.c, line 21.
(gdb) run
Starting program: /home/james/devel/practice/skip_line_if_comment 

Breakpoint 1, main () at skip_line_if_comment.c:21
21                         for ( i = 0; content[i] != '\0'; i++ ) {
(gdb) p content
$1 = "No comment\n", '\000' <repeats 21 times>, "\001\000\000\000\000\000\000\000&\260be", '\000' <repeats 12 times>, "\307\372\366\367\377\177\000\000\200\352\377\367\377\177\000\000\060\340\377\377\377\177\000\000@\340\377\377\377\177\000\000\023\354\375\367\377\177\000\000\000\000\000\000\000\000\000\000\020\341\377\377\377\177", '\000' <repeats 26 times>, "\360\346\377\367\377\177\000\000\330\340\377\377\377\177\000\000\020\341\377\377\377\177\000\000\360\346\377\367\377\177\000\000\000\000\000\000\000\000\000\000\200\352\377\367\377\177", '\000' <repeats 26 times>...
(gdb) p content[0]
$2 = 78 'N'
(gdb) p content[1]
$3 = 111 'o'
(gdb) p content[2]
$4 = 32 ' '
(gdb) p content[3]
$5 = 99 'c'
(gdb) p content[4]
$6 = 111 'o'
(gdb) p content[5]
$7 = 109 'm'
(gdb) p content[6]
$8 = 109 'm'
(gdb) p content[7]
$9 = 101 'e'
(gdb) p content[8]
$10 = 110 'n'
(gdb) p content[9]
$11 = 116 't'
(gdb) p content[10]
$12 = 10 '\n'
(gdb) p content[11]
$13 = 0 '\000'
(gdb) p/x content
$14 = {0x4e, 0x6f, 0x20, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0xa, 
  0x0 <repeats 21 times>, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x26, 0xb0, 0x62, 0x65, 
  0x0 <repeats 12 times>, 0xc7, 0xfa, 0xf6, 0xf7, 0xff, 0x7f, 0x0, 0x0, 0x80, 0xea, 0xff, 
  0xf7, 0xff, 0x7f, 0x0, 0x0, 0x30, 0xe0, 0xff, 0xff, 0xff, 0x7f, 0x0, 0x0, 0x40, 0xe0, 0xff, 
  0xff, 0xff, 0x7f, 0x0, 0x0, 0x13, 0xec, 0xfd, 0xf7, 0xff, 0x7f, 0x0, 0x0, 0x0, 0x0, 0x0, 
  0x0, 0x0, 0x0, 0x0, 0x0, 0x10, 0xe1, 0xff, 0xff, 0xff, 0x7f, 0x0 <repeats 26 times>, 0xf0, 
  0xe6, 0xff, 0xf7, 0xff, 0x7f, 0x0, 0x0, 0xd8, 0xe0, 0xff, 0xff, 0xff, 0x7f, 0x0, 0x0, 0x10, 
  0xe1, 0xff, 0xff, 0xff, 0x7f, 0x0, 0x0, 0xf0, 0xe6, 0xff, 0xf7, 0xff, 0x7f, 0x0, 0x0, 0x0, 
  0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0xea, 0xff, 0xf7, 0xff, 0x7f, 
  0x0 <repeats 26 times>, 0xff, 0xff, 0xff, 0xff, 0x0 <repeats 12 times>, 0xc8, 0x12, 0xfd, 
  0xf7, 0xff, 0x7f, 0x0, 0x0, 0xf0, 0xe6, 0xff, 0xf7, 0xff, 0x7f, 0x0 <repeats 26 times>...}
(the above gdb output is from the same code as GazL quoted in post #99)
 
Old 11-09-2019, 07:05 AM   #115
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,286
Blog Entries: 18

Rep: Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047
Code:
char array[] = { 'a', 'b', 'c', '\n', '\0' };

int i = 0;

while ( array[i] != '\0' )
{
  putchar ( array[i] );
  i = i + 1;
}
James, can you follow that code in your head, or do you have the same problem as with understanding the 'for' loop? It seems you're having problems visualising the individual steps in a program and I'm trying to figure out what it is that's blocking your understanding.


You've told us before that you're a visual learner, perhaps this series on youtube might help. I've not watched it myself, but I recently watched the C++ tutorial series from the same guy and it was pretty good, so it might be worth a look.


I know it's pretty much a dead language at this point but I'm thinking you might actually benefit from learning a teaching language like BASIC, even if only to get you used to seeing through programs to the individual steps they take.


P.S. I'm a 'he'. I know it's all the rage now, and it's probably due to my age, but I find the use of 'they' as a gender neutral third person singular personal pronoun really jarring. Don't worry, I'm not offended, just saying. I also remember a time when "bad" meant bad, "gay" meant happy, and "sick" meant a call to the doctor! *sigh* Oh well...

Last edited by GazL; 11-09-2019 at 08:00 AM.
 
2 members found this post helpful.
Old 11-09-2019, 07:36 AM   #116
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,187
Blog Entries: 13

Rep: Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601
You used the identical technique to debug code from earlier.

The results of that debug session are correct.
Code:
return(ENOERROR);
 
Old 11-09-2019, 10:55 AM   #117
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,266

Original Poster
Rep: Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691
Thanks again guys.

Quote:
Originally Posted by GazL View Post
Code:
char array[] = { 'a', 'b', 'c', '\n', '\0' };

int i = 0;

while ( array[i] != '\0' )
{
  putchar ( array[i] );
  i = i + 1;
}
James, can you follow that code in your head, or do you have the same problem as with understanding the 'for' loop? It seems you're having problems visualising the individual steps in a program and I'm trying to figure out what it is that's blocking your understanding.
...
No. But yes, it's the same problem as before - trying to visualize each step in my head that is. Reading the code itself is ok, but trying to step through it, particularly in my head is another story.

I'll have a look at the video you linked though.
 
Old 11-09-2019, 11:30 AM   #118
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,375

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
for loop looked as a while loop.

Code:
for ( int i = 0; i<10;i++)

set value
int i = 0;
while ( i < 10 ) //check value
{
 process code
i++; //increment counter
}
set the start value  ,checks value, increments counter. 
for ( i = 0; "while" i < 10 ; i++)
the for loop does the same as a while loop. it only consolidates the three into one line.

for loop
Code:
for (set value ; condition ; increment) 
{
   process code
}
here go back to top and don't stop doing that until condition has been met.
if condition has been met, continue with next step.


example
Code:
#include <stdio.h>


int main(void)
{
int i=0;

for (i = 0; i <= 10; i++)
{
   printf("%d,", i);
}

printf("\nthis is next step\nFor loop final count is %d\n",i);

--i;

while (i >= 0)
{
 printf("%d,",i);
 i--;
}
printf("done\n");

return 0;
}
Code:
 userx@FreeBSD.edy:~
$ cc main.c

$ ./a.out
0,1,2,3,4,5,6,7,8,9,10,
this is next step
For loop final count is 11
10,9,8,7,6,5,4,3,2,1,0,done
where adding to for formatting purposes.
Code:
#include <stdio.h>


int main(void)
{
int i=0;

for (i = 0; i <= 10; i++)
{
        if (i < 10 )
           printf("%d,", i);
        else
           printf("%d.\n",i);
}

printf("this is next step\nFor loop final count is %d\n",i);

// de-increment by one to start at 10
--i; 

while (i >= 0)
{
  if ( i >  0)
     printf("%d,",i);
  else
    printf("%d.\n",i);
i--;
}
printf("done\n");

return 0;
}
gets
Code:
userx@FreeBSD.edy:~
$ cc main.c

$ ./a.out
0,1,2,3,4,5,6,7,8,9,10.
this is next step
For loop final count is 11
10,9,8,7,6,5,4,3,2,1,0.
done
removes the last , (coma and adds the period) . at the end.

Last edited by BW-userx; 11-09-2019 at 12:04 PM.
 
Old 11-09-2019, 11:41 AM   #119
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,286
Blog Entries: 18

Rep: Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047
okeydoke. 'for' syntax is a little awkward, but 'while' is pretty straight forward, which is why I wanted to check it wasn't that.

Maybe the vids will help it click with you. At this point I'm out of other ideas.
 
Old 11-10-2019, 07:16 AM   #120
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,266

Original Poster
Rep: Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691Reputation: 1691
That's ok GazL. Thanks a lot for all of your help, I very much appreciate your help and interest.

Like I said before it's not really the code itself/the "syntax", or even the concepts by themselves that's the problem(s). It's trying to follow it and predict what the program is likely to do next, and the next move the computer going to make - basically "thinking like a computer". That's the real problem - it basically goes back to what astrogeek was talking about before (trying to think in terms of understanding and particularly following an algorithm). While loops seem to be particularly tricky, even generally speaking (unless it's a very short and simple program that for example (but not necessarily limited to) just adds a few numbers together and prints the result) it's difficult.

Hopefully I'll get some time to watch that video either tonight or tomorrow sometime. And maybe Umlet another member suggested privately to me may help. In any case, and like you, I'm almost outta ideas on what I can do about it too.

Thanks again GazL.
 
  


Reply

Tags
fgets, strings


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Tabs or spaces? Spaces, obviously, but how many? LXer Syndicated Linux News 0 09-13-2018 10:50 AM
block special and character special files s_shenbaga Linux - Newbie 4 06-23-2015 03:16 AM
LXer: Special mention for Special purpose LXer Syndicated Linux News 0 04-22-2011 12:11 PM
renaming files with spaces and special characters. bowens44 Linux - Newbie 8 06-29-2009 07:52 PM
Spaces and escaped spaces pslacerda Linux - Newbie 13 12-20-2008 10:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration