Thread: Teach a old dog new tricks?

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    6

    Teach a old dog new tricks?

    make a long story short I am almost 50 yrs old and just now getting my BA, in computer science , and right now I am in programming 1 aka C. and Ill be honest I am really struggling. I dont want to put the blame on the teacher, but let me say we just dont seem to have a good teaching / learning chemistry, so what I dont understand I need to teach myself. I am stuck on the logicial part of looping, I have a problem below that I can seem to figure out how to do it


    Write a program that utilizes looping to produce the following table of values

    A A2 A4 A6

    3 5 7 9
    6 8 10 12
    9 11 13 15
    12 14 16 18
    15 17 19 21


    here is the closest I have gotten to getting it right


    int count;

    printf("A A+2 A+4 A+ 6 !\n");
    for(count=3 ;count <= 100;count=count +2)
    printf("%d\t",count);

    I realize this is probably a very simple problem, and most of you could do it in your sleep, but im a little block headed at times and im having one of those times. I been using the tutorials on the website along with C for dummies and I havent found the right place to explain it to me, ( not that it isnt there I just havent found it) could someone point me in the right direction or tell me where to go find it or what I am doing wrong and leaving out

    I would greatly apprciate any help
    Last edited by olds-kool; 08-05-2010 at 08:36 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You spend a lot of time deprecating yourself. I honestly wonder why programmers in particular are hard to ask for things. You figured out a lot by yourself.

    I'm not sure how much you know about two dimensional arrays, but if we look at it that way it should be easy to explain! Given A(n, m), n can be a row (a line on the screen), m can be a column (a value in the row), and we can do this, provided it is translated to C:
    Code:
    for i = 0 to m:
       A(0, i) = 3 + 2 * i
       for j = 1 to n :
          A(j, i) = A(j - 1, i) + 3
    It turns out you can do this a couple of ways depending on perspective. I've shown you the way to fill in a whole column of table A first. If you want to fill in a whole row of table A first, then the math is a little different. You can do that.
    Last edited by whiteflags; 08-06-2010 at 12:32 AM. Reason: ARRRRGGG ...INDICES

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't know about these kids, nowadays!

    I didn't see the relationship that Whiteflags showed, so was very specific:

    1) In a table problem, I always use nested for loops to help organize the rows and columns, individually.

    The more variables the better - you can always consolidate later on.

    2) The first row is just WAY different, so it got it's own if(row==0) logic, including an if just for column==0.

    3) For the other iterations through the loop, I had another if(column==0) to set n (the number to be printed), to row * 3, then print the newline and n.

    Then for the column > 0 part of the above, n+=2.

    4) When you have to line up numbers, I really lean on %2d formatting - where the 2 is the width of the field that the integer will be printed into. Default is right-justified numbers if the field is bigger than the size of the number, which is usually best. (It can be changed to left justified if you want).

    5) always use code tags when posting code - otherwise the forum software changes into html text and it's a witch to look over then.

    click on the "#" icon at the top of the advanced editor window, and paste or type your code, between the given tags.

    Code:
    for(r=0; r<5; r++) {
      for(c=0, n=0;c<4; c++) {
        if(r==0) {
          if(c==0) {
               //first column and row
          }
          else {
            //first row after first column
    
          }
        } //end of first row logic
        if(c==0) {
            //first column after first row
        }
        else {
            //etc.
        }  
      }
    }
    Quite a bit better than:
    for(r=0; r<5; r++) {
    for(c=0, n=0;c<4; c++) {
    if(r==0) {
    if(c==0) {
    //first column and row
    }
    else {
    //first row after first column

    }
    } //end of first row logic
    if(c==0) {
    //first column after first row
    }
    else {
    //etc.
    }
    }
    }

    without the code tags!

    Once you see what n equals at the start of each row, you're off and running.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sigh. You didn't make a very good first impression by neglecting to read the stickies! << !! Posting Code? Read this First !! >>
    Now, scratching all that has been said, the only way you're going to get good at logic is to train yourself. Seeing code thrown at you will do little good.
    I'll give you some help on the way. First of all, write the logic on a paper. Use a flowchart. Write down the actual logic for the program! I'm sure you could do this in the real world without trouble. So decompose how you would do that in real life and put it on a piece of paper.
    Now, read your code, and write this on a piece of paper, too. A flowchart.
    Compare and contrast. Fix.
    If you have problems grasping the logic of the code, I suggest you use a debugger for stepping through it. For a visual debugger, there is always Visual Studio. There are more, of course.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm actually not sure how much flowcharts will help. Math does a lot of the heavy lifting in this program.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Math? What math? All I see is logic! Logic, logic, logic!
    I don't see an algorithm. And I don't see any equation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    6
    Thanks for the help , the grandkids leave in a couple hours and I'll put this into use. Just for the record, Im really not out to impress anyone, but i dont want you to mistake me for someone who doesnt follow rules, or read stickys,
    If you'll remember in my post i comment about being a block head at times. I did read the sticky about tags, and some where between reading and understanding my wires got severly crossed, a matter of fact it is a little emabarrassing for me to admit what I thought it meant ( almost makes me think my wife might be right about me)it all makes sense now,
    and boy, do I really feel stupid, but no worries all code will be posted with tags from now on.
    please be patient with me im 3 classes in and what I know I have gotten from diffrent sources, I need to get a good grasp on this by monday, so as long as it isnt a problem I would like to post my questions here. especially since 75% of what I have learned has come from material on this site

    one thing i havent found is a place where it explains the diffrence between
    a if, while, else, and for, statements and what sitituation that each one is best used for. can someone give me a link where i can read about the diffrences/

    thx again for your help

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    one thing i havent found is a place where it explains the diffrence between
    a if, while, else, and for, statements and what sitituation that each one is best used for. can someone give me a link where i can read about the diffrences/
    An if statement measures the size or quantity of a variable:
    Code:
    if(mpg > 25)
      printf("\n Good mileage");
    else if(mpg > 15)
      printf("\n Fair mileage");
    else
      printf("\n It's a fuel hog!");
    Do while and plain while() loops, are the same in almost all respects. They are used when you have to loop an unknown number of times, before the terminating condition is met. In a do while loop, the program will ALWAYS enter the loop, ONE time, at least. Whereas, in a plain while() loop, the test is made right at the top of the loop. So if the test fails, then NO entry into the body of the while() loop, is ever made.

    For loops are like while() loops, except they have a more structured number of iterations. Unless you do some re-assignment of i's value, in this example, you will only loop 10 times maximum, regardless of anything else:
    Code:
    for(i=0;i<10;i++)
      printf("I will print this 10 times, and no more");

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Also consider reading the site's material, mainly these tutorials:

    Cprogramming.com Tutorial: If Statements
    Cprogramming.com Tutorial: Loops

    In some ways Adak's explanations are simplified to the point of being wrong. If statements don't measure variables so much as they evaluate Boolean expressions. All loops should have a number of iterations, as well.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by olds-kool View Post
    Thanks for the help , the grandkids leave in a couple hours and I'll put this into use. Just for the record, Im really not out to impress anyone, but i dont want you to mistake me for someone who doesnt follow rules, or read stickys,
    If you'll remember in my post i comment about being a block head at times. I did read the sticky about tags, and some where between reading and understanding my wires got severly crossed, a matter of fact it is a little emabarrassing for me to admit what I thought it meant ( almost makes me think my wife might be right about me)it all makes sense now,
    and boy, do I really feel stupid, but no worries all code will be posted with tags from now on.
    I see.
    People who don't read stickies I don't like.
    People who read them but make mistakes are fine.
    So yeah, you're forgiven

    please be patient with me im 3 classes in and what I know I have gotten from diffrent sources, I need to get a good grasp on this by monday, so as long as it isnt a problem I would like to post my questions here. especially since 75% of what I have learned has come from material on this site
    Feel free to ask whatever comes to mind.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Aug 2010
    Posts
    6
    Adak, thanks for the explanation, if I understand you correctly, the IF will deal with variables and see if the input meets the requirments of the varaiable and determining what action to do if the statement is true or false(Boolean), the WHILE or the ELSE determines what the next step is if the requirments arent meet in the if statement, my next question is can you use a ELSE or While statement if you dont have an IF? and when do I want to use FOR statments?

    Elysia,
    after you mentioned, about pseudocode I did a little more research, I was thinking that pseudocode was when you wrote long programs, but I can see now that it is almost a must for me right now, reguardless how simple the program, but my problem is I understand how to write the pseudocode for a problem, but for that chart I posted i can begin to figure out how to write that,
    would you mind showing me how I would write pseudocode for that?

    I realize these questions are basic first day stuf,f unfortunetly the school I go to
    does classes diffrent that most, for a semester, I have 1-2 classes max. each class is 6 hrs a day, 2 days a week, and 5 weeks per semester, which is fine as long as I understand things, but getting stuck on one thing for a couple hours can snowball and put me way behind. thats why it is so important for me to figure out what I dont know and learn it before the next class, the majority of my class is 20-30 yrs younger than me, they never owned a 8-track and never knew life without computers, so when i have the time i need to learn what I can to keep up with the rest of the class, these forums can be a huge asset to me. So let me say it again I really appreciate your patience.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I showed you an example of pseudo code, instead of actually writing a program, in this thread.

    Code:
    for i = 0 to m:
       A(0, i) = 3 + 2 * i
       for j = 1 to n :
          A(j, i) = A(j - 1, i) + 3
    Two loops and some array accessing.

    Quote Originally Posted by Wikipedia
    Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of a programming language, but is intended for human reading rather than machine reading.
    It's supposed to help you so make sure it makes sense to you. There are no hard and fast rules as far as the definition goes, but try to be consistent. It doesn't have to be hard. Some people on here are so experienced with C that their psudo code is more like a C program that doesn't compile yet.

    Flow charts are less of a free for all. When you draw a flow chart (with flow chart software, such as Visio, I hope) there are shapes that all mean something in logical terms. It is particularly useful when you are working with complicated decision making and need a reference for program flow. When you're starting out, of course, everything is complicated. In professional environments, things only get bigger and involve more brains, or so I've heard.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Adak, thanks for the explanation, if I understand you correctly, the IF will deal with variables and see if the input meets the requirments of the varaiable and determining what action to do if the statement is true or false(Boolean), the WHILE or the ELSE determines what the next step is if the requirments arent meet in the if statement, my next question is can you use a ELSE or While statement if you dont have an IF? and when do I want to use FOR statments?
    If() statements measure the variable, against some value or variable. If your car gets > 25 mpg, then your car's mileage might be judged as "good":

    Code:
    if(mpg > 25) 
      printf("Mileage is good\n");
    else
      printf("Mileage is not so good");
    so the "else" part handles logic when the if() part is not being evaluated to true. The while() loops have nothing to do with if() or else statements, directly. They can be combined, and frequently are:

    Code:
    while(1) {  //starting an "endless" loop, so it needs break logic
      if(mpg > 40)  {
         printf("\nGreat mileage!);
         break;
      }                     //but it's bad logic
    }
    Now tell me what happens in this loop, if the car has less than or equal to 40 miles per gallon?

    if() statements may be used to test ANY variable, against either other variables, or (like in the mileage example), a given value.

    A typical while() loop is used to read in lines of text from a file. You never know if a file has no lines (an empty file), or lots of lines, so a while() loop is perfect for this. It just keeps looping until the file's contents have all been read:

    Code:
    while((fgets(bufferName, sizeof(bufferName), filePointerName)) != NULL) {
       //code to handle the data received from the file, goes here
    }
    Note that you will ALWAYS want to try and read AT LEAST ONE line of data from a file - even if it's empty. So it sounds like this would be a candidate for a do while() loop - that's really what the do while() loop is for, but a while() loop can make it even more clear and shorter, as well.

    Which is one reason why while() loops are more popular in C code, than do while() loops are.

    Code:
    do {
      fileOK=0;
      if((fgets(bufferName, sizeof(bufferName), filePointerName)) != NULL) {
        //code to handle the data from the file goes here
        fileOK=1; 
      }
    }while(fileOK);
    The above do while loop will work fine, but it just can't be quite as clear as the earlier while() loop.

    You could do the above file reading with a for loop, but it's not as intuitive to do so. Any while() loop or do while() loop, can be re-written as a for loop, (and the reverse is also true), but the resulting code is just not as clear and simple.

    Consider these two loops from Quicksort: (a famous sorting algorithm)

    Code:
    while(array[left] <= pivot) left++;
    while(array[right] >= pivot) right--;
    Here, the value of left may need to remain unchanged, IF the value of the array[left] is > the value of the pivot, on the very first comparison. Same for the value of right.. It's VERY clear just what is being done here, with both these simple while loops. You COULD make these loops work by using a for loop, but why would you want to?

    Make clarity a goal in your code, after accuracy (which is the #1 goal, always).

  14. #14
    Registered User
    Join Date
    Aug 2010
    Posts
    6
    you guys are awesome thanks a bunch . now let me ingest this and I am sure ill have more questions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginning Game Programming Type Books
    By bumfluff in forum Game Programming
    Replies: 36
    Last Post: 09-13-2006, 04:15 PM
  2. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  3. destroywindow() problem
    By algi in forum Windows Programming
    Replies: 6
    Last Post: 03-27-2005, 11:40 PM
  4. Refrences and a dog named skinbag.
    By CoderMatt in forum C++ Programming
    Replies: 4
    Last Post: 02-24-2003, 08:30 PM
  5. A Cat and a Dog
    By da_fall_guy in forum C++ Programming
    Replies: 10
    Last Post: 07-24-2002, 09:20 AM