180 likes | 196 Views
Introduction to Programming Using C Week 2 – Lesson 1 (Pages 12 to 18 in IPC144 Textbook). IPC144. Agenda. Additional C programming Elements Take-up REALITY CHECK (Question #3) Commenting & Indentation Additional Numeric Data Types ( longs, floats, doubles )
E N D
Introduction to Programming Using C Week 2 – Lesson 1 (Pages 12 to 18 in IPC144 Textbook) IPC144
Agenda Additional C programming Elements • Take-up REALITY CHECK (Question #3) • Commenting & Indentation • Additional Numeric Data Types(longs, floats, doubles) • Arithmetic Operators(+, -, *, /, order of operations) • Prompting Input from user (scanf( ) statement) • Examples
Take-up Homework • REALITY CHECK • Question #3
Previous Lesson • In the previous lesson, we created, compiled, and ran a simple C program using integers. #include<stdio>main(){ int num1 = 1, num2 = 2; printf(“The sum is: %d\n”, num1 + num2); } Note: statements are indentedto make main( ) section easier to read…
Commenting your Program • The programming community encourages “sharing of programming code” (eg. via the Internet). This concept is referred to as Open Source (although this does not apply to your IPC144 assignments!) • Since other programmers may read or modify your C program in the future, you should provide comments to help inform the reader of your code. • You can include plain text in your program by surrounding your between the /* and */ special characters. This instructs the compiler to ignore these comments when the program is transformed into machine language.eg. /* This is a comment intended to inform the reader of your C program */
Additional Numeric Data-types • Recall from the previous lesson, Variables allow data to be stored in some space in the computer’s memory. • In order to have data stored in the computer’s memory, a storage space must first be created. This process is called defining a variable, and it is important not only to define the variable, but declare the data-type of the variable (eg. number, character, character strings, etc…)
Additional Numeric Data-types • An integer data type is a whole number that may be positive or negative. The integer data type may have a limited range (-32768 to +32767). • We declare the variable at the beginning of the C program by using the int statement. We can also assign a value to that integer variable at the same time (if required)For example: int num1 = 1 , num2 = 2 , num3 = 25; • We can print-out the value of the integer variable by using a format specifier (%d) in the printf( ) statement. This format specifier is used to indicate the data-type of the variable and determine where that value will be inserted into our display.Eg: printf (“num1 is :%d, num2 is: %d, num3 is: %d\n”, num1, num2, num3);
Additional Numeric Data-types Other Numeric Data Types: Q: Which data-type to use? A: It depends on what you want to do. doubles are good to use if working with decimals, but use ints if you are using whole numbers.The distinction between int/long and float/double are based on efficiency. We will use int and double data-types in this course…
Practice • From what we have learned as of now, let’s try the REALITY CHECK handout, Question #1 • Using the handout, plan and then code your program
Arithmetic Operators There is an “Order of Arithmetic Operator” Arithmetical calculations with Round Brackets get calculated first, followed by Exponentiation, then followed by Multiplication/Division, then followed by Addition/Subtraction. (The abbreviation BEDMAS can be used to help remember that order…)
Arithmetic Operators • In addition to “Order of Arithmetic Operators”, programmers must also be careful to understand what happens when performing calculations between different data-types • For example: Division of an int and a double…If you divide an int by another int, the result will be an integer, but if you divide an int by a double (or the other way around), the result will be a double.
Inputting Numbers • In order to make your C program more flexible, your program should be able to obtain input from a user that runs you program. • The scanf( ) statement is used to wait until the user enters input and presses the ENTER key. The scanf () statement usually followed the printf() statement to prompt the user to enter input… • The scanf( )statement requires two pieces of information: Thedata-typeof the variable, and the name of the variable that was already declared in a previous statement
Inputting Numbers For Example: Memory(RAM) Sets up integer variablecalled “age” somewhere in the Computer’s Memory (RAM) • int age; • printf (“Enter your age (in years): ”); • scanf (“%d”, &age); • Printf (“\nYou are %d years old\n\n”, age); 25 Stores user input into memory location for age variable. The & symbol is used to indicate the RAM memory address where age variable is located…
Practice • From what we have learned as of now, let’s try the REALITY CHECK handout, Question #2 • Using the handout, plan and then code your program
Walk-Thrus • In addition to being able to create a C program from a “word problem”, you will be required to be given a piece of code, and write the intended output as if it has been compiled and run. • There are many reasons why you should learn to perform walk-thrus • 50% of your tests and final exams consist of walkthrus • Performing walk-thrus help “test to see” if you understand material • Carefully performing steps in the program (like a computer does) give you a “tool” to help troubleshoot or solve a program that is not working. A walk-thru allows you to see the program “from the perspective of the computer running that program”. • Refer to the slide show of a simple walk-thru demonstration…
Practice • From what we have learned as of now, let’s try the REALITY CHECK handout, Question #3 (Walk-thru)
Practice • From what we have learned as of now, let’s try the REALITY CHECK handout, Question #4 (Walk Thru)
Homework • TASK #1 • Complete lab #1 since it is due at the beginning of this week’s lab! • TASK #2 • Study for a quiz to be held in this week’s lab based on material taught last week • TASK #3 • Complete and code the solutions for today’s REALITY CHECK QUESTIONS, as well as “hide” and repeat the walk-thru questions • TASK #4 *** Highly Recommended *** • Read ahead in IPC144 Programming Notes pages 19 to 23, and 26 to 30.