IPC144 Introduction to Programming Using C Week 1 – Lesson 2

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 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
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CMT Programming Software Applications
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Computer Science 1620 Programming & Problem Solving.
Introduction to C Programming
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
Basic Input/Output and Variables Ethan Cerami New York
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Agenda What is Computer Programming? The Programming Process
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Introduction to Python
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Creating your first C++ program
Programming With C.
Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++
Input, Output, and Processing
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
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.
Agenda Computer Languages How to Write a Simple C Program
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
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.
Last week: We talked about: History of C Compiler for C programming
Chapter 1: Introduction to computers and C++ Programming
Completing the Problem-Solving Process
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.
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Creating your first C program
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
Introduction to C Programming
Chapter 1 c++ structure C++ Input / Output
Getting Started With Coding
Presentation transcript:

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

Agenda Basic Programming Steps (Simple Example): Read and understand the question Develop an algorithm Code the program in C Compile the program Run, Test and Debug the program C programming Elements: main() function variables and data types output: printf (output format specifiers)

Programming Steps Gather required information (inputs, processing, outputs)‏ Determine the algorithm – sketch out, in English, how a human can solve the problem Write (ie “code”) the program (called the source code)‏ Compile the source code. Fix any syntax errors and recompile to create an executable file. Run executable file with test input(s) and check for correct output(s). If necessary, debug by finding logical errors (bugs) and correct source code.

C Programming Basics Every C program must contain a function called main() The name “main” indicates that it is the main part of the program and is executed first. main() is followed by the set of braces { and } which contains the contents of the main function.

C Program Example Here is the format of a basic C program: #include <stdio.h> main() { … statements of C program … } Most C statements end with a ; just as English sentences end with a period.

C Programming - printf A statement used to display formatted output on the screen is called printf (the “f” indicates that you can format the the output). You can use special characters in a printf statement to format output such as a tab (\t) or newline (\n). Refer to the IPC144 notes (page 9) for additional special characters, eg: \” \\ Text that you want to display is surrounded by parentheses (round brackets) and contained in double-quotes. For example: printf (“Hello!”);

C Programming - printf Note: The printf statement is actually a function that was created to make it easy for programmers to display output. To make certain that you can compile your program using printf on any computer, you should include a library that contains standard input and output function information called stdio.h This is done by adding a directive at the beginning of the program before main() as follows: #include <stdio.h> Note: No semicolon!

Practice From what we have learned so far, let’s try the first hello.c program

Practice How can you actually put your program into the computer? You need to connect to Seneca’s matrix computer First, read “Getting Started” on the IPC144 website Eg. In MS Windows: START -> RUN -> telnet matrix.senecac.on.ca After entering your userid and password a Unix prompt will be displayed. You then enter Unix commands Eg: >who which displays all users currently using matrix

Practice First you need to use a text editor to create your program (referred to as source code). Use a text editor such as nled, pico, vi, nano, etc… to enter plain text (no bold, formatting, etc). Eg: nled hello.c All C programming source code files should be saved with the file extension: lower-case .c eg assignment2.c or hello.c

Practice Compiling a C program: The C program must be translated into an executable file, also known as a binary file, machine-language file, or object file. To compile your source code file named hello.c type in the cc command (to mean compile a C program): >cc hello.c If there are syntax errors then details including as line number will be displayed If there are no syntax errors, then an executable file called a.out is created To run the program enter a.out at the prompt Eg: >a.out

Practice You can also provide a name for the executable file by using the –o option with the cc command: eg: cc hello.c –o hello The executable file is now contained in the file named hello, so to run it enter: hello and you should see something like this on the screen: Hello, my name is Fred

Variables It would be nice if a computer program could do something more interesting than display your name! Variables allow data to be stored in some location in the computer’s memory. To have data stored in the computer’s memory, a storage area must first be created. This is called declaring a variable. It is important to indicate the variable’s name and also its data type (eg: integer, decimal, character, etc…)

Variables Rules for variable names: Only use letters, digits, or underscores (eg: customer_age, number1, gstRate, etc…) Cannot begin with a digit (eg, invalid names: 2be, 1stcustomer, 4wheel) Cannot use keywords such as int, for, if because they have special meanings

Variables Use descriptive names for variables. You can combine multiple words to form a variable name. You could use underscores to separate the words but the convention is to use lowercase for the first word and then capitalize the first letter of each of the following word(s) For example: customerAge promptUserGuess generalSalesTaxRate

Integer Data Type First we will study the integer data type in this lesson, and will study other data types in later lessons. An integer data type is a whole number that may be positive or negative. On the matrix computer the integer data type has a limited range: approx. 2109 ( -231 to 231-1, or -2147483648 to 2147483647) because the number, including its sign, is stored in 32 bits

Variables At the beginning of the main() program, you should first declare your variables and indicate their data type. int is used to represent an integer data type. Here is a declaration of two integer variables int length, width; Note: same data type variables can be listed using commas Note: the declaration must end with a semi-colon ;

Variables There are many ways to have a variable “hold data” in the variable such as assigning a value, or prompting the user to enter a value, even read data from a file! A value can be assigned to a variable by using the assignment operator which is represented by the equals sign (however don’t say “equals” when you see the “=“ sign – say “is assigned” instead!) eg. int length, width; length = 3; width = 5; Note: The value of a variable can can also be assigned (“initialized”) when declaring the variable eg. int length=3, width=5;

Variables The printf statement is used to display values of variables (also in a specified format). If you want to display the variable’s value in the printf statement, you must use a “format specifier” that represents the variable’s data-type. You use the format specifier %d for int data-type variables. For example: int area = 23; printf(“Value of area is %d\n”, area); Variable name(s) (in order) appear at end of printf Format specifier for an integer data-type where value of area will be inserted

Practice From what we have learned so far, you should be able to start understanding Program #2, rectangle.c, which calculates the area and perimeter of a rectangle

Practice!! TASK #1 TASK #2 TASK #3 Attend the lab, and learn how to login to matrix then create, compile, run, and print out a simple C program. TASK #2 Take the answers for questions #1 and #2 in the Exercise Handout, create a source-code file, then compile, run and verify your program! Not due, but for practice! TASK #3 Work on question #3 by yourself to plan, create, compile and run that program. Remember: Practice is the key to success!!