Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
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
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Computer Science 1620 Loops.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
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
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Agenda  Perform Quiz#2 (20 Minutes)  Functions / Continued … –Functions - Definition –Types of Functions: Functions that do not accept or return a value.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Fundamental Programming: Fundamental Programming Introduction to C++
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Agenda Basic Logic Purpose if statement if / else statement
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
 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.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
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.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 6: User-Defined Functions I
Chapter 2 - Introduction to C Programming
ICS103 Programming in C Lecture 3: Introduction to C (2)
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 CS Your First C Programs
Week 9 – Lesson 1 Arrays – Character Strings
Chapter 2 - Introduction to C Programming
1) C program development 2) Selection structure
T. Jumana Abu Shmais – AOU - Riyadh
Chapter 2 - Introduction to C Programming
Chapter 3 – Introduction to C# Programming
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Chapter 2 - Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Introduction to C Programming
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions –Purpose –Function Structure: Prototype / Heading / Body / Function Calls

for loops  The for loop is used when the number of repetitions is already known.  The for loop can be convenient since all of the controls for the loop appear at the top of the for statement initialize Test condition commands Change (update)

for loop main ( ) { int i; printf (“\nMy 13 times table\n\n”); for ( i = 1; i <= 12; i++) printf(“%d times 13 = %d\n”, i, i * 13); printf (“\n”); } Initialize the variable to be used in the for loop There are 3 parts in the for Statement: (separated by semicolons “;”) Initialization – performed once, when for loops first starts. Test Condition - test to determine if loop is to be continued. Change or Update – evaluated at the end of each loop. Perform action within the loop. (use braces for code block)

In Class Exercise #1 Display the output from the following program: (show your rough work) #include main () { int num; printf(“\tn\tn cubed\n”); for (num = 1; num <= 6; num++) printf (“%5d %5d\n”, num, num*num*num); }

In Class Exercise #2 Write a program that prints a table with each line giving an integer, its square, and its cube. Ask the user to input the lower and upper limits for the table. (use a for loop)

Storing Characters  Another data type that variables can contain is a character.  The C keyword to declare a variable as a character data type is char variable_name;  The character data type actually stores a character as an integer (a numerical code) that represents certain characters. You will learn about data representation in your ICS124 course and the standards to represent characters on different computer systems. (eg. ASCII or EBCDIC)

Storing Characters To prove that characters are stored as integers, please view the following C program (available in ~msaul/ipc144/samples): #include main ( ) { char ch; printf (“Please enter a character: ”); scanf (“ %c”, &ch); printf (“The code for %c is %d.\n”, ch, ch); } SAMPLE OUTPUT: Please enter a character: C The code for C is 67. When scanning a character, after double quote, place space before %C” to have system ignore or skip “whitespace” characters such as spaces or tabs. This only has to be done when scanning characters.

Storing Characters We can improve our programs by allowing the user to select “y” for yes and “n” for no

In Class Exercise #3 Q What is the weakness with the following program? main () { char selection; double amount, delivery = 30.5; printf (“\nEnter the amount of your purchase: ”); scanf (“%lf”, &amount); printf (“\nDo you want item delivered? (Y/N): “); scanf (“ %c”, &selection); if ( selection == ‘Y’) amount = amount + delivery; printf (“\nThe total amount of the purchase is: $%.2lf\n\n”, amount); } A What if the user enters a lowercase “y”? The delivery amount will not be added to the total amount!

Character Conversion Functions A common method to prevent problems with case sensitivity is to use a function convert one type of case to another to prevent the confusion and errors associated with mixing up case sensitivity. toupper (variable) –Used to convert lowercase characters to uppercase characters. This function will not affect the character if it is already uppercase. tolower (variable) –Used to convert uppercase characters to lowercase characters. This function will not affect the character if it is already lowercase.

Solution to In Class Exercise #3 main () { char selection; double amount delivery = 30.5; printf (“\nEnter the amount of your purchase: ”); scanf (“%lf”, &amount); printf (“\nDo you want item delivered? (Y/N): ”); scanf (“ %c”, &selection); if ( toupper(selection) == ‘Y’) amount = amount + delivery; printf (“\nThe total amount of the purchase is: %.2lf\n\n”, amount); }

Advantages of Modularity  Use modules, that were previously created, in current program you are creating (saves time)  Split large program into modules and assign them to several programmers to have programs developed at a fast rate.  It may be easier to modify or replace a module of a program than trying understand and modify a large program without modules

Functions  So far, the programming examples have been fairly simple.  Since programs that we develop become larger and larger, it may become more difficult to understand all the elements in one large program  When designing large programs, it would be better to reduce elements of the program into smaller parts or modules.  The process of having programs consisting of separate parts or modules is referred to as modularity

Functions  Many computer programming languages (such as C) uses the word “function” to represent the term modules.  main() is actually a function that is the first and last function performed when the compiled C program is executed.The main function, while running, can execute or “call” other functions.  Functions should contain simple specific operations, so they can be used when creating other programs. –Examples include: prompt and scan data & perform error-checking, make calculates, display output, etc…

Functions (Visual Example) declare variables main function: Run get_input function Run print_result function Calculate result get_input function: print_result function: entire program Small C program

Elements of a Function  Function Prototype –If a function appears after the main function (preferred method of many programmers), then a function prototype is required to “declare” a function for main.  Function Heading –A function heading is place just above the function body for identification.  Function Body –The function body is enclosed in braces and contains commands (C code) to perform the function. You can think of the body of the function as a small C program  Function Calls –A function can be run or “called” within the main function or within another other functions.

Example #include void report_heading (void); main () { int i, num; printf ("\nEnter an integer for Times Table: "); scanf ("%d", &num); report_heading (); for (i = 1; i <= num; i++) printf ("%d times %d equals %d\n", num, i, num*i ); printf ("\n"); } void report_heading (void) { printf ("\nMy Times Table:\n\n"); }

In Class Exercise #4  Modify In Class Exercise #2 to use a function called table_header to print the title: My Little Awesome Times Table By: YOUR NAME  And use another function called table_footer to print the message at the end of the times table: That’s all Folks!!!  These functions should be executed or “called” within the main function or program.