1 Lab Session-VI CSIT-120 Spring 2001 Let us look at C++ syntax rules in brief Exercise VI-A (Demo Required) Lab Assignment#4 Due May 1st, 2001 (No Lab.

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

CMPUT 101 Lab # 5 October 22, :00 – 17:00.
True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)
1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.
1 Session-I & II CSIT-121 Spring 2006 Session Targets Introducing the VC++.NET Solving problems on computer Programming in C++ Writing and Running Programs.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
1 Lab-1 CSIT-121 Fall 2004 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.
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.
Chapter 6: Functions.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
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.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Palindromes revisited Here's a simpler program for checking palindromes: int nums[100]; int i = 0, a; cin >> a; while(a > 0) { nums[i++] = a; cin >> a;
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
1 Special Programming Workshop CSIT-120 Fall 2000 Workshop Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Fundamental Programming: Fundamental Programming Introduction to C++
CPS120: Introduction to Computer Science Decision Making in Programs.
Previously Repetition Structures While, Do-While, For.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
1 CS161 Introduction to Computer Science Topic #9.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Control of flow We learned that default flow of instructions is sequential. Then, we learned how to control the flow using "if" and "switch." Now, we will.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Controlling Program Flow with Decision Structures.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 Functions Part 1 Prototypes Arguments Overloading Return values.
INTRODUCTION TO ROBOTICS Part 5: Programming
Basic concepts of C++ Presented by Prof. Satyajit De
User-Written Functions
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Lecture 4B More Repetition Richard Gesick
Introduction to Functions
Arrays, For loop While loop Do while loop
Conditions and Ifs BIS1523 – Lecture 8.
Introduction to C++ Programming
Additional Control Structures
Computing Fundamentals
Control Structures Part 1
Chapter 6: User-Defined Functions I
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
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.
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

1 Lab Session-VI CSIT-120 Spring 2001 Let us look at C++ syntax rules in brief Exercise VI-A (Demo Required) Lab Assignment#4 Due May 1st, 2001 (No Lab on April 24th, ….To Earthquake Zone) Lab session-VI deals with functions We will learn how to write independent functions that can be called from main() Students are given examples of functions

2 C++ Syntax Rules in Brief #include #…..other include files go here void main(void) { –Declarations of variables and constants –Assignment and control statements }

3 C++ Syntax Rules in Brief Make it a habit to put declarations in the beginning. For example, –int employeeID; –const int current-year=2000; –float deduction-amount; Once a data item has been declared, do not mention its data type in assignment statement int employeeID=23; isWRONG employeeID=23; isRIGHT

4 C++ Syntax Rules in Brief If you write a control statement, its conditions must be in parenthesis with no semicolon after the parenthesis For example, displaying employeeID if employeeID is 24; if (employeeID == 24) { cout<<“ID is “<<employeeID<<endl; } Please note “double-equal” in comparing, no semicolon after comparison statement and joining several displayable messages with <<

5 C++ Syntax Rules in Brief Different comparison statements if (employeeID != 24) { cout<<endl;} (if employeeID is not equal to 24, do a blank cout) if (employeeID >= 24) if (employeeID <=24) if (employeeID > 24) if (employeeID < 24) We could also use an else clause to capture the false results of comparison

6 C++ Syntax Rules in Brief For example, if employeeID is 24, display “ID is 24” else do a blank line display if (employeeID == 24) { cout<<“ID is “<<employeeID<<endl; } else {cout<<endl;} Use braces to show blocks of code even if only a single statement is there in if part or else part (for clarity purposes)

7 C++ Syntax Rules in Brief In assigning values, there can be only one destination shown on left of the single equal sign deductions = pay- (pay*tax-percentage); Please note the use of parenthesis to emphasize and make the expression very clear

8 Lab Exercise VI-A Develop a program that takes a number from the user and then enters a loop to keep displaying the result of successively multiplying the number by 2. The loop is terminated when the number exceeds half of INT_MAX (Do not forget to include the header file limits.h) Example: 2,4,8,16,32,64,…………..

9 Lab Assignment#4 Due 5/1 A classroom has the maximum capacity of 37 people, write a program that accepts the total number of students wishing to register for a course. This program checks for any violations of fire safety code. It asks the user to input the total number of students. If this number is more than the maximum allowed, it prints a warning and shows how many people will not get the course otherwise it shows the additional number of people who can be admitted to the course. Bonus 2 marks for making it a loop and asking again and again until the number of students is exactly equal to the capacity

10 Lab VI Continued We look at some C++ code segments Introducing functions We solve one programming problem

11 Basic Concepts What will be the output? Don’t run this code, just predict void main() { int num1,num2; num1=12345; num2=6789; cout<<num1<<num2<<endl; }

12 Basic Concepts What is wrong with this code segment? void main() { int num1, myloop; while (myloop<24) cout<<“Once in myloop, show no mercy”<<endl; }

13 Basic Concepts What is wrong with this code segment? Void main() { int mybirthdate; mybirthdate =2; while (mybirthdate<24); { cout<<“Enter my birth date (date only)”<<endl; cin>> mybirthdate; } cout<<“you got it…………\n”; }

14 Basic Concepts What is the error in this code segment? int cans; for(int cans=1; cans<=100; cans++) { cout<<“Each one has a refund of 5 cents\n”; cout<<“count until 100 cans are done”; cans--; }

15 Basic Concepts Fix this code segment int k; cout >k; if (k=5) cout<<“You typed 5”; else cout<<“Nothing else please”;

16 Functions Functions are “subroutines”, “procedures”, “modules” or “program units”. Functions perform a given task, usually one function is dedicated to one task One advantage of using functions is to avoid repeated coding for the same job

17 Functions Reduce Code Size For example, consider a program that prints text in a box, something like below **************************************** * * The Journey Through Central Spine * * **************************************** We can achieve this printout with a program containing 9 cout statements OR we can develop a function to print aesterik lines

18 Program with 9 cout Lines cout<<“****************************************”<<endl; cout<<“* *”<<endl; cout<<“* The Journey Through Central Spine *”<<endl; cout<<“* *”<<endl; cout<<“****************************************”<<endl; cout<<“****************************************”<<endl;.

19 Same Program with a Function Print_three_star_lines(); cout<<“* *”<<endl; cout<<“* The Journey Through Central Spine *”<<endl; cout<<“* *”<<endl; Print_three_star_lines(); Now the program is more compact and readable. Actual function definition will be developed after the main() function’s code DEMONSTRATION OF PROGRAM

20 Function Specific Actions If you plan to work with functions, make sure that you name your functions according to C++ naming conventions (letters, underscores, digits, no digits in the beginning) As demonstrated, function data type is specified before its name. If the function does not return anything, void is used Also note the DECLARATION before the actual function code, ending with a ;

21 Function Call Notice the function name is simply repeated when it is called from the main routine print_three_star_lines(); The empty parenthesis indicate the fact that no data is passed to this function. A semicolon is a must to terminate any C++ statement (except loop starters)

22 The Events When a function is called from the main routine, the following events take place Control (of the running program) is transferred to the function as well as any data that may be needed Function performs its task and at the end, transfers the control back to the calling routine

23 Why does the Function name appear so many times? Count the number of times the function name is repeated in our program The name appears each time for a different purpose Firstly we have to declare the function. Function declaration is called its prototype void print_three_star_lines();

24 Why does the Function name appear so many times? Next, we have to define the function,i.e. its actual source code void print_three_star_lines() {………….} Finally, we have to make the function call print_three_star_lines(); What do you know about main function when it reads void main(){……….}?

25 Lab Programming Exercise Write a program that accepts a digit entered by a user and then displays it in words. For example, user enters 8, program displays, “You Entered Eight” User enters 0, program displays “You Entered Zero” and so on………..