IPC144 Introduction to Programming Using C Week 2 – Lesson 1

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
EGR 2261 Unit 11 Pointers and Dynamic Variables
CSCE 206 Structured Programming in C
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Introduction to the C Language
Chapter 2 - Introduction to C Programming
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Revision Lecture
Chapter 1. Introduction to Computers and Programming
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Chapter 2 - Introduction to C Programming
Computational Thinking
By: Syed Shahrukh Haider
Computational Thinking
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Learning to Program in Python
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Chapter 2 - Introduction to C Programming
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Programming Funamental slides
1) C program development 2) Selection structure
IPC144 Introduction to Programming Using C Week 3 – Lesson 1
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
Introduction to C Topics Compilation Using the gcc Compiler
The ‘while’ loop ‘round and ‘round we go.
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 2 - Introduction to C Programming
Topics Designing a Program Input, Processing, and Output
Introduction to C Topics Compilation Using the gcc Compiler
IPC144 Introduction to Programming Using C Week 6 – Lesson 1
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
EECE.2160 ECE Application Programming
Chapter 2 - Introduction to C Programming
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Variables in C Topics Naming Variables Declaring Variables
EECE.2160 ECE Application Programming
The while Looping Structure
DATA TYPES There are four basic data types associated with variables:
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
Introduction to C Programming
The while Looping Structure
IPC144 Introduction to Programming Using C Week 2 – Lesson 2
Getting Started With Coding
Presentation transcript:

IPC144 Introduction to Programming Using C Week 2 – Lesson 1 (Pages 12 to 18 in IPC144 Textbook)

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 indented to 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: The data-type of the variable, and the name of the variable that was already declared in a previous statement

Inputting Numbers For Example: Memory (RAM) int age; Sets up integer variable called “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 TASK #2 TASK #3 TASK #4 *** Highly Recommended *** 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.