Variables User Input and Arithmetic Daniel Kohn University
Variables, User Input and Arithmetic Daniel Kohn University of Memphis Spring 2021
Labs start week! • Please show up to the lab you are assigned to in Gradewatch. • If you need to switch labs (for the semester) please find a student to swap with, then both of you email me (thus informing me that you both agree to the switch)
Recall Example 1 from Flowcharting/Pseudo Code • Problem Statement - Add two number together • High Level Algorithm • Stated above (add two numbers together) • More Detailed algorithm • • Get first number Get second number Add them together Show the answer
Flowchart
“Get 1 st Value (store in A)” and nd “Get 2 value (store in B)” • In C, before we can get or store a value in a variable we have to define that variable. • What is a Variable? • Just like in Algebra, a variable is a letter or word that represents a value that can change within the context of the mathematical problem (or in our case, within the program)
Rules for Naming Variables • The name can contain only letters, numbers, and the underscore symbol _. • The first character must be a letter or underscore. (It can’t be a number or symbol. ) • C is case-sensitive… job jo. B JOB are different variable names. • There are key words that can not be used because they have a specific meaning in C, such as: main, return Legal names: times 10 get_next_char _done Illegal name: 10 times $cost get-next-char
Naming Variables • When possible, use names that represent what is going to be stored in the variable. • If we are calculating area of a triangle: • We could use “a” for area, “b” for base and “h” for height • But it is better to use the full names: area, base, height. • This will make your code more READABLE and is good programing practice.
But wait there’s more…… • We also have to tell our programs what kind of data is to be stored in a variable. • We will use 3 different kinds of variables this semester: • Integers – A whole value that can be positive or negative (ie -1, 0, or 3) • Floating Point Numbers – a value that contains a fractional part and contains values before and after a decimal point. They can also be positive or negative. (ie 3. 14, -9. 81, 10. 435) • Characters – as the name implies, we can store characters within variables as well (ie, c, C, $).
Declaration Statements • When you declare a variable you are really telling the program: • what type of data will be in the variable, • what it will be called within the program • How it will be stored in memory (this is discussed in TECH 3233) • Therefore, each variable must be declared before the it can be used. • Examples: int height; float profit; char x; Variables will be declared at the top of main. Note: There are LIMITS to the values that can be stored in int and float type variables!
Initializing a Variable • Initializing a variable is to put something into it for the first time. This can be done a number of ways: • After it is declared, do a statement like: a = 32; • While you declare it: int a = 32; • As the ANSWER to a mathematical statement a = 2+3; or a=x+y; //when x and y are already initialized • Ask the user to type in a value (more on this in upcoming slides)
Initializing a Variable • NOTE: Until you initialize a variable, you do NOT know what is in it (could be zero, could be a random number) so make sure you always initialize a variable!
And now back to our program
printf placeholders • We saw last lecture that you can print a message by saying something like printf(“hello world nr”); But now we want to print a value stored in a variable? • To do that we need to use a placeholder and tell printf what to place into the placeholder: printf(“The answer is: %inr”, ans); • %i holds the place for an integer value • Ans is the value to put in the place holder
And now back to our program
But what if • But what if we want to output something like this: The answer to 5 + 7 is 12 • We can do that by putting in a place holder for each variable and then putting the variables IN THE ORDER THEY ARE TO BE PRINTED after the comma
And now back to our program
Common placeholders • %i – integer value • %f – floating point value • %c – character
Getting Input from a user • So far we have used a value stored in a variable, but now we will ask the user for the number. • To do this we need to use the scanf command. • Just like the “f” in printf stands for “formatted” so the “f” in scanf stands formatted.
Use of scanf • To get the first value (a) from the user you would use: scanf(“%i”, &a); • You MUST put a & in front of the variable (the reason why will become clear if/when we learn about pointers). • For now just accept that it MUST be included. • %i is a place holder similar to the printf, but you can use it to format how you wish the user to type in the input. • NOTE: what is in quotes will NOT be displayed to the user. • To “PROMPT” the user for input, you must use a separate printf statement.
And now back to our program
Watch out for those &’s • Demo (show what happens when they are left out) • No error on compile • Unexpected results
What happens when I add…. • I want to add 3. 14 + 6. 28 • Demo • Why didn’t it work? I told it to accept integer values, not floating point values…. What is the fix?
And now back to our program
Basic Arithmetic Operations + addition - subtraction * multiplication / division You can also use () as you would in math The variable to store the answer should be on the LEFT, calculation on the Right of the = sign.
Lab #1 • Will be a calculation using user input. • Assignment will be posted in the CHAT of the zoom at the start of class. • Please show up to the lab you are assigned to in Gradewatch. • If you need to switch labs (for the semester) please find a student to swap with, then both of you email me (thus informing me that you both agree to the switch)
- Slides: 25