Like Tree1Likes
  • 1 Post By grumpy

Trouble understanding the concepts behind the code

This is a discussion on Trouble understanding the concepts behind the code within the C Programming forums, part of the General Programming Boards category; For the assignment, I have to add upon the previous by including a function to detect that the characters inputted ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    6

    Question Trouble understanding the concepts behind the code

    For the assignment, I have to add upon the previous by including a function to detect that the characters inputted are appropriate. They can't be letters or symbols, can only be numbers, one decimal, and only two places from the decimal.

    Before I start writing code and make it really obvious I don't know what I'm doing, I'm trying to get the concepts through my head with pseudocode. I can't even get the pseudocode down because I have trouble getting the concepts.

    I know in order to check characters, there needs to be conversion going on-- but why? Is it to check values? To store data? I don't understand.

    This is where I'm stuck. A classmate told me the code has to check for fractions to make sure the decimal isn't over 100 but beyond that it went into one ear and out the other. I can't write pseudocode if I can't get the basic concepts. Can anyone help me out?

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    My assignment involves entering values for pay and wages. After calculating deductions, a function returns the values as net pay. However, I need to understand the how and why behind checking each character entered.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,288
    Quote Originally Posted by curlygeek View Post
    I know in order to check characters, there needs to be conversion going on-- but why? Is it to check values? To store data? I don't understand.
    If you enter the characters "56" at the keyboard when prompted to enter an integer, the keyboard sends two characters with values 53 (the '5') and 54 (the '6'). (Assuming your operating system or compiler works with the ASCII character set, which most do but not all do). To interpret those two characters as meaning a value 56, it is necessary to interpret the meaning of both characters, and then interpret the pair of them as meaning 56.

    Your description is not entirely accurate, because there are many possible ways of interpreting the pair of consecutive characters '5' and '6' as the value 56. Some of the techniques for doing that involve conversion (which means translate a value of type X to a value of type Y) and some don't. Some methods of interpreting data may even involve multiple conversions.

    In the example I described, there are often I/O functions to interpret a set of characters as a value. For example, scanf()'s %d format tells scanf() to interpret coming data as if it represented an integral value.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    Quote Originally Posted by grumpy View Post
    If you enter the characters "56" at the keyboard when prompted to enter an integer, the keyboard sends two characters with values 53 (the '5') and 54 (the '6'). (Assuming your operating system or compiler works with the ASCII character set, which most do but not all do). To interpret those two characters as meaning a value 56, it is necessary to interpret the meaning of both characters, and then interpret the pair of them as meaning 56.

    Your description is not entirely accurate, because there are many possible ways of interpreting the pair of consecutive characters '5' and '6' as the value 56. Some of the techniques for doing that involve conversion (which means translate a value of type X to a value of type Y) and some don't. Some methods of interpreting data may even involve multiple conversions.

    In the example I described, there are often I/O functions to interpret a set of characters as a value. For example, scanf()'s %d format tells scanf() to interpret coming data as if it represented an integral value.
    The classmate who had showed me his code explained it to me this way, but he used one method of doing so.

    Why do the characters get interpreted as 53 and 54? The compiler Bloodshed uses ASCII from what I recall.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,288
    All characters need distinct values, in order to tell them apart.

    There are various specifications that assign values to particular characters (the letters in the alphabet, digits, punctuation characters, various types of whitespace, symbols, etc). ASCII (American National Code for Information Interchange) is just one of the specifications. It is one of the older ones, but still in common use.

    As to why digits don't map exactly to their value, it is quite common in several programming languages that a character with value zero represents "end of string" (where a string is a sequence of characters that collectively have some meaning, such as "hello") or "no data in this character". It would not be a good thing to give the digit '0' a value of zero .... For example, that would make it impossible to represent the value 100 in a string. The digit '0' therefore has a non-zero value in most specifications. That affects other digits as well since, with most character sets (usually digits map to consecutive values, just not a set starting with zero).

    There are other character sets too, such as Unicode, EBCDIC, and others. All of them are just a specification mapping particular characters (human readable or not) into numeric values.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    Okay, in a step by step process, how does this look exactly? I think if someone explained the process to me in steps I'd understand better. Say for example, what a user inputted was 44.75.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,288
    Try writing down on paper how you would convert a string containing the characters "44.75" into the value (or values) you want.

    Imagine you are writing step-by-step instructions to do that, which will be followed by a pedantic ignoramus who will do what you tell him, no more no less, and will electrocute you if he gets confused.

    You'll learn more by working out the steps for yourself, than you will if someone simply gives you the steps.
    rags_to_riches likes this.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    Okay, the sarcasm is not needed considering all I was asking for was an example.

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by curlygeek View Post
    Okay, the sarcasm is not needed considering all I was asking for was an example.
    Read the post again. There is absolutely no trace of sarcasm whatsoever.

    grumpy is describing how to come up with the algorithm yourself, attempting to teach you how to fish instead of simply cramming a trout down your gullet.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    48
    One thing that I think you're not understanding - could be wrong, is this:

    If you say define a variable as char, and then assign it a number, like this:

    char num = 26; then if I'm not mistaken it is actually a letter because of the ascII table.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,288
    Quote Originally Posted by curlygeek View Post
    Okay, the sarcasm is not needed considering all I was asking for was an example.
    I was not being sarcastic. You are better off formulating your own answer to your question, rather than having someone else do it for you.

    I gave a pointer to how you might do that.

    Quote Originally Posted by jsuite View Post
    If you say define a variable as char, and then assign it a number, like this:

    char num = 26; then if I'm not mistaken it is actually a letter because of the ascII table.
    You are very much mistaken. ASCII 26 is a control character, known as the "substitute character" (I won't go into the reason for that name). It is the code generated by low level keyboard drivers when the user hits CTRL-Z (holds down the CTRL key and the Z key at the same time). It is also used under some older disk operating systems (like MS-DOS) to represent end of file.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble understanding code to calculate sum of n numbers
    By jackloring in forum C Programming
    Replies: 8
    Last Post: 11-13-2010, 03:33 AM
  2. Replies: 5
    Last Post: 08-31-2004, 06:41 PM
  3. Replies: 11
    Last Post: 08-30-2004, 03:56 PM
  4. trouble understanding collision detection code
    By silk.odyssey in forum Game Programming
    Replies: 5
    Last Post: 06-16-2004, 02:27 PM
  5. Implementing Mathematical Concepts in Code
    By MisterWonderful in forum Tech Board
    Replies: 6
    Last Post: 03-08-2004, 06:44 AM

Tags for this Thread


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