Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
Another thing I didn't know was your while (input).

This is related to the ranges to integers code I have used in several of your examples.

C doesn't implement a separate true/false (a.k.a. Boolean) data type. It uses integers for that. 0 is false and any other value is true.

So
Code:
while(input)
is short hand for and completely equivalent to
Code:
while(input!=0)

There are MANY ways to skin this cat. The temporary variable "Result" pianojoe used is one way. Below is another based on that code that gives the exact same output without it.

Code:
#include <stdio.h>

int main (void) {
	int Input;
	
	printf ("Enter a number: ") ;
	scanf ("%d", &Input) ;
        printf("Result: ");
	while(Input) {
		printf("%d", (Input % 10));
		Input = Input / 10;
	}
	printf("\n");
	
	return 0;
}

Just for fun, I'll develop the multi loop version that was my first thought.

EDIT: Here's another way to do it that presumes knowing how many digits are in the number is useful as I originally assumed. Sometimes your assumptions need to be revisited/thrown out.

Code:
#include <stdio.h>

int main (void) {
  int input,copy,reverse = 0;
  int done = 0, digits = 0, place = 1, newplace = 1;
  int count;
  int digit;

  printf ("Enter a number: ") ;
  scanf ("%d", &input) ;
  if (input>0) {
    copy = input;
    // Figure out how many digits in the input number and build max placeholder           
    while(copy) {
      copy /= 10;
      digits++;
      place *= 10;
    }
    place /= 10;
    // Reverse the input pulling number off from most-significant to least                
    copy = input;
    for (count = 1; count <= digits ; count++) {
      digit = (copy/place);
      reverse += digit*newplace;
      copy -= digit*place;
      newplace *= 10;
      place /= 10;
    }
    printf ("Reversed number: %d\n", reverse);
  }
  else
    printf ("ERROR: Please enter a positive integer\n");

  return 0;
}

B
 
Last edited:
This is related to the ranges to integers code I have used in several of your examples.

C doesn't implement a separate true/false (a.k.a. Boolean) data type. It uses integers for that. 0 is false and any other value is true.

So
Code:
while(input)
is short hand for and completely equivalent to
Code:
while(input!=0)

There are MANY ways to skin this cat. The temporary variable "Result" pianojoe used is one way. Below is another based on that code that gives the exact same output without it.

Code:
#include <stdio.h>

int main (void) {
	int Input;
	
	printf ("Enter a number: ") ;
	scanf ("%d", &Input) ;
        printf("Result: ");
	while(Input) {
		printf("%d", (Input % 10));
		Input = Input / 10;
	}
	printf("\n");
	
	return 0;
}

Just for fun, I'll develop the multi loop version that was my first thought.

EDIT: Here's another way to do it that presumes knowing how many digits are in the number is useful as I originally assumed. Sometimes your assumptions need to be revisited/thrown out.

Code:
#include <stdio.h>

int main (void) {
  int input,copy,reverse = 0;
  int done = 0, digits = 0, place = 1, newplace = 1;
  int count;
  int digit;

  printf ("Enter a number: ") ;
  scanf ("%d", &input) ;
  if (input>0) {
    copy = input;
    // Figure out how many digits in the input number and build max placeholder           
    while(copy) {
      copy /= 10;
      digits++;
      place *= 10;
    }
    place /= 10;
    // Reverse the input pulling number off from most-significant to least                
    copy = input;
    for (count = 1; count <= digits ; count++) {
      digit = (copy/place);
      reverse += digit*newplace;
      copy -= digit*place;
      newplace *= 10;
      place /= 10;
    }
    printf ("Reversed number: %d\n", reverse);
  }
  else
    printf ("ERROR: Please enter a positive integer\n");

  return 0;
}

B



Thanks for your input balamw. You did that 2nd one "just for fun?" That one is a bit too complicated for me at this point. However, your other one is probably closer to what I could come up with if I could come up with it. Plainjoe is next. I studied his code during lunch. I'm trying to go through the steps again of what I was trying to accomplish, and see if I can see where it would lead to his/your code.

I forgot to think of the while (as true or false). I was just thinking of it simply on a conditional basis, while (this =that),or while this value > that value) In other words, thinking of it as it reads. If x is bigger than y, do stuff. I wasn't thinking in terms of 0,1 false and true. I didn't know that a number was equal and could be used in place of != 0. I've written != 0 before, I'm going to go back and look at that old code to see what number I would use in place of != 0.

I'm going to write some notes out, then post to see if they lead me in the right direction.


Okay, so here goes.
What am I trying to do?
1) I want to take out the last digit of userNumber with %
2) Then I want to divide userNumber /10 and return the new number to the loop.
3) Then I want to take out the last digit of userNumber with %
4) Then I want to divide userNumber /10 and return the new number to the loop.
I want to continue this pattern until the userNumber is no longer > 0.

User enters 348
I do 348 % 10 takes out the 8 <--------------------8
then I do 348 / 10 removes the 8, return 34 to loop.
I do 34 % 10 takes out the 4 <--------------------4
then I do 34 / 10 removes the 4, return 3 to the loop.
I do 3 % 10 takes out the 3 <--------------------3
then I do 3 / 10 values is less than 0, loop terminates, I have my 843 above.

Am I exactly right in this evaluation? Is this exactly what I should have come up with last night? At this point, would I possibly write some pseudo code?
Let me know, I want to go through the correct steps, make sure I'm on track.

FYI, one of the reasons I struggled with this idea last night is because I thought that once I did 348 % 10, that taking out that 8 was the same thing as removing it. So the way I thought last night was that if I did....

348 % 10 takes out the 8, sends only 34 to be divided by 10 in the next line
34 / 10 leaves me with only 3 (doesn't work)

Then last night I thought about doing the
348 % 10 before the loop started, it will get the 8 out for me
then put 34 in the loop to be remm'ed by 10, so 34 % 10, gets the 4 out for me
then divide 34/10, leaves me with 3, send 3 back to the loop.
3 % 10 takes out the 3...........843. Would that have worked too?

rough example, something like.....I don't know, just curious here....

(userNumber % 10) //this takes out the first number I need then sends it on..
(userNumber / 10 ) // sends the new number into the loop
while (userNumber != 0)
userNumber % 10 //takes out last number again
userNumber / 10 //sends the "new" number back up into the loop.


Lastly, I've almost forgotten about the book, but it did ask me to use a do loop to complete this exercise. Do balamw or plainjoe codes qualify as "do" loops? I only see a while in there.
 
Last edited:
Thanks for your input balamw. You did that 2nd one "just for fun?" That one is a bit too complicated for me at this point. However, your other one is probably closer to what I could come up with if I could come up with it. Plainjoe is next. I studied his code during lunch. I'm trying to go through the steps again of what I was trying to accomplish, and see if I can see where it would lead to his/your code.

I forgot to think of the while (as true or false). I was just thinking of it simply on a conditional basis, while (this =that),or while this value > that value) In other words, thinking of it as it reads. If x is bigger than y, do stuff. I wasn't thinking in terms of 0,1 false and true. I didn't know that a number was equal and could be used in place of != 0. I've written != 0 before, I'm going to go back and look at that old code to see what number I would use in place of != 0.

I'm going to write some notes out, then post to see if they lead me in the right direction.

Trust your instincts more on always using a conditional operator. Say != 0. that's more specific. If there were booleans in C, testing them would be sensible. There are not. No one is going to be confused by you being too clear with your intentions.

Regarding your question in an earlier post about how you'd know you need a second variable... If you need to keep track of some state, you need a variable to hold it. 0 is a pretty good place to start in most cases, especially if you are adding something up. The first * 10 is a little tough, but we just get lucky that 0 * 10 is 0, so the algoritm doesn't need to be modified for a special case the first time through the loop. You could have also gone with the route chown33 suggested and balmw demonstrated, printing a character at a time. That requires no intermediate variable. I always like to have my result stored in case I need it again for a subsequent calculation, etc. That doesn't mean printing character-wise is wrong. There's rarely a single solution to rule them all.

-Lee
 
You did that 2nd one "just for fun?" That one is a bit too complicated for me at this point.
Yeah, it was a test of two assumptions that in this case are misleading.
  1. Knowing how many digits you have up front is useful/necessary
  2. Taking the original number apart from most-significant to least-significant is effective
I forgot to think of the while (as true or false). I was just thinking of it simply on a conditional basis, while (this =that),or while this value > that value) In other words, thinking of it as it reads.
You're using your own shorthand here too. Your statement should read "while this value > that value) is true." As Lee says you need to go with what is comfortable and makes sense to you. If that means having your conditionals in loops be "!= 0", so be it.

I'm going to write some notes out, then post to see if they lead me in the right direction.

I think your notes are already too "refined" and close to pseudo code.

I'd start with the next level down from "reverse the number" which is something like
Code:
take the number apart into digits 
reorder the digits

The pianojoe and first version of the code I posted use the same "take the number apart into digits" approach, but differ on the reorder the digits.

The second version takes a different approach to the take the number apart code and splits that into two bits.
Code:
get the number of digits
get the numbers from left to right knowing the number of digits

NOTE: If you use the approach of building a new integer, you can get into a situation where (for large numbers) the reversed number is too large to fit in an int and it will cause overflow issues. This should be tested for in "production" code.

I always like to have my result stored in case I need it again for a subsequent calculation, etc. That doesn't mean printing character-wise is wrong. There's rarely a single solution to rule them all.

Ain't that the truth. You never know when you might need a number again for a subsequent step/expansion. So err on the side of keeping stuff around. That's why I chose to use the second "copy=input" in my toy code rather than mangling the input itself as pianojoe and I both did in the first one.

B
 
I try to keep thinking of this as just a language. I think that keeps it in perspective and simple for me. And as with any language, it takes using it over and over and over to master it. So I'll just keep using it, making mistakes as I go, eventually they will be less, until no more.




Edit: By the way did anybody answer about their examples being a "do" loop? I see both use while. Is that considered the same thing since do and while go together?

Edit: Am I calculating this correctly? User enters 25, n=25
for (i = 2; i * i <= n; i += 2) i is 2, (2 *2)=4, 4<=25; 4 adds 2 to itself and becomes 6. i is now 6? Is this correct?
 
Last edited:
Okay, so here goes.
What am I trying to do?
1) I want to take out the last digit of userNumber with %
2) Then I want to divide userNumber /10 and return the new number to the loop.
3) Then I want to take out the last digit of userNumber with %
4) Then I want to divide userNumber /10 and return the new number to the loop.
I want to continue this pattern until the userNumber is no longer > 0.

User enters 348
I do 348 % 10 takes out the 8 <--------------------8
then I do 348 / 10 removes the 8, return 34 to loop.
I do 34 % 10 takes out the 4 <--------------------4
then I do 34 / 10 removes the 4, return 3 to the loop.
I do 3 % 10 takes out the 3 <--------------------3
then I do 3 / 10 values is less than 0, loop terminates, I have my 843 above.

Am I exactly right in this evaluation? Is this exactly what I should have come up with last night? At this point, would I possibly write some pseudo code?
Let me know, I want to go through the correct steps, make sure I'm on track.
You're not exactly right. Error hilited in red.

You need to be very specific about what "return the new number to the loop" means. If it means "store the result in a variable", then you need to show it (storing into a variable is the assignment operation, =). You also need to name exactly which variable receives the result. Storing things in the wrong variables is a common bug.


It's unclear what "take out" means. 348 % 10 calculates a result of 8, but it doesn't modify the 348. If a variable held that value, then the variable would still be 348 after the calculation. Example:
Code:
int exampleNum;  // declare a variable named 'exampleNum'
exampleNum = 348;  // assign 348 to the variable exampleNum
printf( "%d", exampleNum % 10 );  // doesn't change exampleNum
printf( "%d", exampleNum );  // evidence of unchanged exampleNum
Very few operators actually modify the variable. Assignment (=) does, but it only modifies the left-hand variable, not any right-hand variables. ++ and -- also modify the variable. Nothing else does.


Based on the above about what modifies variables and what doesn't, analyze how variables are modified in the following:
(userNumber % 10) //this takes out the first number I need then sends it on..
(userNumber / 10 ) // sends the new number into the loop
while (userNumber != 0)
userNumber % 10 //takes out last number again
userNumber / 10 //sends the "new" number back up into the loop.
If that's pseudo-code, it's not showing any operations that modify variables, because it's not showing any assignments (='s). There is no "sending on" of anything that isn't a variable. If you do a calculation and don't assign the result to a variable, then the result of the calculation goes nowhere. If you're assuming that this will somehow happen without you writing code to perform assignment of results, then you should stop making that assumption, and stop writing pseudo-code that has no assignments.


More comments later. Gotta run right now.
 
Bill, wlh99, and ender land. Thanks for your moral support as well. Yes this is the most challenging thing I've learned. Nice to be reassured that it is, or at least was difficult for many of you at one time, that your not all genius savants!
My pleasure, Scott.

Just another tip, if you don't mind. Please try to avoid a trap that keeps catching me. Too often, I focus on syntactic, language-specific detail when I need to think instead about the problem I want to solve. That's why I need to spend lots of design time before I write any program lines.

I should have done plenty of designing before I wrote my C program that mimics Unix's wc command. Unfortunately, I wrote it at the keyboard without pseudocode. So that embarrassing program probably would have been much easier to understand if I thought much longer about how to count words, characters, and lines. I long to write elegant programs. But the sooner I write code, the less elegant it'll be.
 
Very few operators actually modify the variable. Assignment (=) does, but it only modifies the left-hand variable, not any right-hand variables. ++ and -- also modify the variable. Nothing else does.

Come on, what about all of the fun assignment plus operation operators:
+=
-=
*=
/=
%=
^=

They all modify their left operand.

=)

-Lee
 
There must me someone among you that feels the same way, like "why waste our time with this guy, he can't figure it out." In reality I don't think I'm an idiot. I understand economics and international trade theory.

Well, your coding problems are simple for me, but I know nothing about international trade theory and very little about economics. It's all about each person's individual knowledge, and props to you for taking on such a challenge by yourself. (I certainly would be intimidated to go onto a forum of economics experts and start asking questions... !)

I agree with the others that you just need to be patient and keep working at it. You're definitely going in the right direction by working on it on paper first. As I've said before (perhaps not in one of your threads, though, I can't remember) the real trick about computers is learning to think like a computer, and mapping out what needs to be done into a series of step by step operations. Once you've broken down the steps and generalized the solution, you can start writing code.

I think your challenge is that you're doing both steps at once, so you're getting frustrated by both the "writing out the algorithm" and "writing lines of code that compile properly" steps.

Definitely keep at it, but take breaks and don't let yourself get too frustrated. I do this stuff for a living and every once in a while I get stumped too. Sometimes the answer will come to me all of a sudden, much later. Some of my best epiphanies come while I'm in the shower :) That's always an exciting feeling. I remember rushing into work one Monday morning at 6:00am because I had been totally stumped by something the previous week and the answer came to me over the weekend. Sure enough, by 8:00am I had the problem solved, before my coworkers had even arrived at work!

You have to like (or at least tolerate) solving logic puzzles and math problems in order to get really good at programming.
 
Last edited:
Come on, what about all of the fun assignment plus operation operators:
+=
-=
*=
/=
%=
^=

They all modify their left operand.

=)

-Lee

Also, ++ and --. (I know those were already listed, but what about the ones that modify their right operand?)

++i
--i
 
Edit: Am I calculating this correctly? User enters 25, n=25
for (i = 2; i * i <= n; i += 2) i is 2, (2 *2)=4, 4<=25; 4 adds 2 to itself and becomes 6. i is now 6? Is this correct?
Is this a new problem?

Yes, for n=25 the loop should run for i==2 and i==4 and end because i==6.

B
 
Then last night I thought about doing the
348 % 10 before the loop started, it will get the 8 out for me
then put 34 in the loop to be remm'ed by 10, so 34 % 10, gets the 4 out for me
then divide 34/10, leaves me with 3, send 3 back to the loop.
3 % 10 takes out the 3...........843. Would that have worked too?

Why bother? You're repeating things outside the loop that are the same as things inside the loop. Repetition or duplication is a sign that something is wrong in the design.


(userNumber % 10) //this takes out the first number I need then sends it on..
(userNumber / 10 ) // sends the new number into the loop
while (userNumber != 0)
userNumber % 10 //takes out last number again
userNumber / 10 //sends the "new" number back up into the loop.
As pseudo-code, this has a big problem: no actions. There are no verbs or other action-words that tell what is actually done with the calculated values.

Go back and look at the step-by-step breakdowns I wrote in earlier posts. It's no accident that each step starts with an action verb: set, print, set, continue, divide, repeat, etc.

When I see pseudo-code like the above, it reads like this to me:
Code:
(userNumber % 10)  // Calculate userNumber%10, then do nothing with the result.
(userNumber / 10 ) // Calculate userNumber/10, then do nothing with the result.
while (userNumber != 0)
    userNumber % 10      // Calculate userNumber%10, then do nothing with the result.
     userNumber / 10      // Calculate userNumber/10, then do nothing with the result.

If the calculation is intended to be stored back in the same variable, like this:
Code:
(userNumber % 10)  // Calculate userNumber%10, then store it back in userNumber.
then that's an error in the algorithm design. Storing the low digit back into userNumber will overwrite the value needed for the next step.

If the calculation is intended to be assigned to a different varable, like this:
Code:
(userNumber % 10)  // Calculate userNumber%10, then store it in some variable.
then that's a different design error: you haven't identified which variable to store the result into.

If the calculation result is intended to be printed, like this:
Code:
(userNumber % 10)  // Calculate userNumber%10, then print it.
then that's a serious ambiguity in the design description language, because it looks exactly the same as this:
Code:
(userNumber / 10)  // Calculate userNumber/10, then do something unspecified with it.
where something unspecified happens to the result of the calculation, or maybe nothing happens to the result. Can't tell what is meant without a verb.

If some calculations are intended to be printed, and others are intended to be stored, then you have to use verbs to indicate what action is taking place. Here's an example with verbs and additional details added:
Code:
Print (userNumber % 10).
Store (userNumber / 10 ) in userNumber variable.
while (userNumber != 0)
    Print userNumber % 10.
    Store (userNumber / 10 ) in userNumber variable.
Now we have some comprehensible pseudo-code that states what happens. It has verbs, nouns, and modifiers (calculations).

We can also plainly see that the two statements before the loop are identical in every way to the two statements in the body of the loop. Once again, repetition is often a sign of a design error.

Change it so it's a loop with the test at the end:
Code:
do the following:
    Print userNumber % 10.
    Store (userNumber / 10 ) in userNumber variable.
 while (userNumber != 0).
Compare to what I wrote in my first post in this thread:
... loop doing the following:
1. print num % 10 (i.e. least significant digit).
2. divide num by 10.
3. repeat until num is 0.
In particular, notice the verbs (emphasis added).

I wrote "divide num by 10" which starts with the verb "divide". Used as a verb, "divide" is a shorter way of saying these two sub-steps.
Code:
a. Calculate num divided by 10.
b. Store the result back into num.
If you had to, you could break it down like this every time. Of course, if it wasn't intended to store the result back into the same variable, you'd have to write that out: Calculate something; store the result somewhere else.

I didn't write "num / 10" or "num divided by 10" because that contains no verb, so there's no stated action. "Calculate" is a verb, but something must be done with the result or the calculation serves no purpose.
 
I like functions. And it is faster that so many printfs (hey, each picosecond counts)

Code:
#include <stdio.h>

int reverseNum (int num);

int main()
{
   int		number;
	
   printf("Give me a number: ");
   scanf("%d", &number);
   printf("The reverse is %d\n", reverseNum (number));
   return 0;
}

int reverseNum (int num)
{
   int		result = 0;
   while (num) {
	result = result * 10 + num % 10;
	num /= 10;		
}
	
   return result;
}

Next exercise: write a new function that can handle very BIG numbers (numbers that are bigger that a long int).
 
I like functions. And it is faster that so many printfs (hey, each picosecond counts)

Code:
#include <stdio.h>

int reverseNum (int num);

int main()
{
   int		number;
	
   printf("Give me a number: ");
   scanf("%d", &number);
   printf("The reverse is %d\n", reverseNum (number));
   return 0;
}

int reverseNum (int num)
{
   int		result = 0;
   while (num) {
	result = result * 10 + num % 10;
	num /= 10;		
}
	
   return result;
}

Next exercise: write a new function that can handle very BIG numbers (numbers that are bigger that a long int).

I don't disagree, but the OP is working through a book and hasn't hit functions (or arrays, needed for your next step) yet.

-Lee
 
Edit: Am I calculating this correctly? User enters 25, n=25
for (i = 2; i * i <= n; i += 2) i is 2, (2 *2)=4, 4<=25; 4 adds 2 to itself and becomes 6. i is now 6? Is this correct?

No. If i is 2, then 2*2 is 4, and 4 <= 25. The analysis is correct up to that point. But the calculation of i*i does not alter i. So when the i += 2 begins execution, i is still 2, and i += 2 results in i becoming 4, not 6.

I was going to ignore this, but I think it shows a conceptual problem that I see running through all your analysis and coding. That problem is in understanding when variables change and more importantly, when they don't. The calculation i*i doesn't store a new value in i. It simply calculates a value using the current value of i. If the result of that calculation isn't stored anywhere (and in the posted code, it's not), then no variables change due to the calculation, and the calculated result is simply used to perform a comparison with n. The only time i changes is at the i += 2 expression.

I'm wondering if this misperception of variables changing when they participate in calculations is a result of "calculator thinking". A simple handheld calculator doesn't have explicit variables [1]. There is one implicit variable: the displayed value. So when you calculate products or sums or differences of things, the displayed value is the only value there is, and it does change as it participates in the calculation. That's just a guess, but it's common enough for most people that their only experience of calculation is with a handheld calculator, even if it's a simulated one running on a computer.


[1] Sophisticated calculators that do algebra and graphing, and have actual programming capabilities, do have actual explicit variables. And HP RPN calculators have always had the 4-level stack as well as numbered storage registers.
 
No. If i is 2, then 2*2 is 4, and 4 <= 25. The analysis is correct up to that point. But the calculation of i*i does not alter i. So when the i += 2 begins execution, i is still 2, and i += 2 results in i becoming 4, not 6.
I read that quite differently than you did, but see where you are coming from.

cybrscot, try tracing the behavior of loops by putting debug output in them, for example:

Code:
#include <stdio.h>

int main(void) {
  int i, n;
  printf ("Enter number n:\n");
  scanf ("%d",&n);
  for (i = 2; i * i <= n; i += 2) 
    printf ("%d %d\n",i,(i*i));
  printf ("%d %d\n",i,(i*i)); 
}

Code:
$ ./deleteme 
Enter number n:
25
2 4
4 16
6 36

So it does actually comes out at the end of the loop with (i == 6). Though maybe not for the reason cybrscot thinks. unfortunately 2*2 and 2+2 are the same.

B
 
Edit: Am I calculating this correctly? User enters 25, n=25
for (i = 2; i * i <= n; i += 2) i is 2, (2 *2)=4, 4<=25; 4 adds 2 to itself and becomes 6. i is now 6? Is this correct?


Not quite right, no. It will run with i==2 and i==4 as balamw says:
Yes, for n=25 the loop should run for i==2 and i==4 and end because i==6.
B


But I think you're a bit confused about when values get stored in variables. You're correct up to here:
i is 2, (2 *2)=4, 4<=25

But not this bit:
4 adds 2 to itself and becomes 6. i is now 6? Is this correct?

Just because you multiply i by itself with i*i, this does not affect the value stored in i. If i contains 2 and you calculate i*i, the result of that will be 4, but unless you store it in i with i=i*i, i will still contain 2.

It would be more like this:

i is 2, (2 *2)=4, 4<=25 is TRUE, execute loop for 1st time with i==2.

i is 2, add 2 to it, i becomes 4.
(4*4) = 16, 16<=25 is TRUE, execute loop for 2nd time with i==4.

i is 4, add 2 to it, i becomes 6.
(6*6) = 36, 36<=25 is FALSE, don't execute loop.

Loop is finished.

Edit: I didn't notice chown33 and balamw's latest posts before writing this, oops.
 
Last edited:
My pleasure, Scott.

Just another tip, if you don't mind. Please try to avoid a trap that keeps catching me. Too often, I focus on syntactic, language-specific detail when I need to think instead about the problem I want to solve. That's why I need to spend lots of design time before I write any program lines.

I should have done plenty of designing before I wrote my C program that mimics Unix's wc command. Unfortunately, I wrote it at the keyboard without pseudocode. So that embarrassing program probably would have been much easier to understand if I thought much longer about how to count words, characters, and lines. I long to write elegant programs. But the sooner I write code, the less elegant it'll be.

QFT.

As another newer object-oriented programmer (I have programmed quite a bit over the past 5 years, as well as Matlab) and have been working with C++ now for just about a year, this is SO true.

It is so tempting to just start coding without thinking "what will this look like." I wish I had realized this when I first started, because my first thought was "oh, I want to do x, y, z" and then started coding them. But after doing x and y, I realized z didn't quite work, so I redid x and then y didn't work, etc.

In VBA or Matlab it is much easier to do that simply because I was normally writing linearly based programs where it was not really a big deal to plan things out.
 
When I posted at this board's forum about religion, politics, and social issues, someone compared me to a dinosaur. In some respects, the description probably fits me. So please tell an old T. Rex what "QFT" means. At 6 feet tall and 166 pounds, I'm the tiniest full-grown T. Rex on record. :)

It is so tempting to just start coding without thinking "what will this look like." I wish I had realized this when I first started, because my first thought was "oh, I want to do x, y, z" and then started coding them. But after doing x and y, I realized z didn't quite work, so I redid x and then y didn't work, etc.
EL just put a finger on why I like what Paul Graham calls "bottom-up programming." In bottom-up programming, you write tiny, highly general, software tools that let you imbed a programming language in another language. The procedures extend the language you write them in. They're self-contained procedures that behave the same way wherever you put them. Since they're self-contained, they help you avoid bugs you can get when procedures interact with one another.

Sorry if I make bottom-up programming sound too much like object-oriented programming. Maybe some bottom-up programs are object-oriented, but bottom-up programs don't need to be object-oriented. Being a bottom-up program doesn't imply being an object-oriented program. I'll need to reread what Graham writes about "BUP."

Purely functional programming languages help a lot, too, partly because in a purely functional language no function can cause side-effects. In them, side-effects are impossible.
 
QFT = Quoted for truth.

And yeah, I think OOP and RISC both tend to drive towards BUP conceptually. I tend to write even my Matlab code this way these days. If my function is longer than about 50 lines it probably needs to be refactored.

I still find I need to do some top down design too.

B
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.