copyright © Michael B. Feldman, All Rights Reserved

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

Chap-7 Robust_Input. A Package for Robust Input Read input values by calls to procedure Get overloading “Get” procedure for Integer and Float values There.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Chapter 3 Assignment and Interactive Input
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
ITEC 320 Procedural Programming Dr. Ray Lecture 1.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
C Programming Lecture 4 : Variables , Data Types
1 CS 101 Lecture 2. 2 Input from the Keyboard Here is a program to accept input from the keyboard and put into into two variables. #include main(){ cout.
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.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 2: Basic C program structure Data in C: Data types,
CPSC- 120 Principle of Computer Science I Computer = Hardware + Software.
Slide: 1 Copyright © AdaCore Subprograms Presented by Quentin Ochem university.adacore.com.
Chapter 7 Other Loop forms, Procedures; Exception Handling.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chap 7- Control Structures : The WHILE Statement.
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.
A FIRST BOOK OF C++ CHAPTER 3 ASSIGNMENT AND INTERACTIVE INPUT.
Useful Ada Information Help for programming assignment.
System Structures b Package b Abstract Data Type.
CSC Programming I Lecture 6 September 4, 2002.
Chap 5 Control Structure Boolean Expression and the IF Statement.
Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration.
Review With packagename ; Use packagename ; Procedure procedurename Is declaration of variables & constants Begin statements of program End procedurename.
Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com.
Chapter 3 The General Structure of Ada Programs. General Form of an Ada Program With package1; With package2;... With packagen; Procedure pname IS - -
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Introduction to Python Sajal Desai. What is Python? Versitile, simple, high level language No linking and compilation “By the way, the language is named.
structured statically typed imperative wide-spectrum object-oriented high-level computer programming language extended from Pascal and other languages.
小型系統 心情 vs. 古典樂 心情 vs. 古典樂 浪漫求籤系統 美食導航系統 季潔亭雅鈺熒岱芸 美食導航系統 楊氏音樂模擬大會考人瑋 若維 芷萱 伽倩 楊氏音樂模擬大會考 麥當勞熱量計算系統 火星文困擾你嗎 ? 火星文困擾你嗎 ? 歌詞知多少 - 挑戰你的腦容量英琪 日馨 青雪 鈺娟.
WITH Ada.Text_IO; PROCEDURE Three_Days IS | Finds yesterday and tomorrow, given today.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
Chapter 15 - C++ As A "Better C"
CSE 220 – C Programming C Fundamentals.
Subtype Enumeration type
H:\>| WITH Ada.Text_IO; PROCEDURE Hello_Initials IS
Arrays: Checkboxes and Textareas
Introduction to Computer Science / Procedural – 67130
Arrays Declarations CSCI N305
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.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Escape Sequences What if we wanted to print the quote character?
Imperative Programming
Introduction to C++ Programming
Arrays Kingdom of Saudi Arabia
Lecture 12 Oct 16, 02.
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Computing Fundamentals
Suppose I want to add all the even integers from 1 to 100 (inclusive)
Functions continued.
Python Basics with Jupyter Notebook
The structure of programming
Arrays Arrays A few types Structures of related data items
C:\> sort_3_numbers.exe |
SE1H421 Procedural Programming LECTURE 3 Variables (2)
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Imperative Programming
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

copyright © Michael B. Feldman, All Rights Reserved Lecture 4 – Programs copyright © Michael B. Feldman, All Rights Reserved

Hello.adb with Ada.Text_IO; procedure Hello is ----------------------------------------------------------- --| A first very small, obligatory program --| Author: Michael B. Feldman; last revised September 1999 ----------------------------------------------------------- begin -- Hello Ada.Text_IO.Put_Line (Item => "This is the obligatory Hello program!"); end Hello;

Hello_Terse.adb with Ada.Text_IO; use Ada.Text_IO; procedure Hello_Terse is ----------------------------------------------------- --| A terse version of the Hello program --| Michael B. Feldman; last revised Sept. 1999 ----------------------------------------------------- begin Put_Line ("This is the obligatory Hello program!"); end Hello_Terse; -- we didn’t compile this one -- it works, but we won’t use USE clauses.

Hello_Error.adb -- with Ada.Text_IO; -- use Ada.Text_IO; procedure Hello_Error is ----------------------------------------------------- --| What happens if we omit the with and use clauses? --| Michael B. Feldman; last revised Oct. 2008 ----------------------------------------------------- begin Put_Line ("This is the obligatory Hello program!"); end Hello_Error; -- this one won’t compile

Display_Integer_Error.adb with Ada.Text_IO; with ada.integer_text_io; use Ada.Text_IO; --use ada.integer_text_io; procedure Display_Integer_Error is Number1: Integer; Number2: Integer; begin Number1 := 3; ada.integer_text_io.Put_Line(Number1); end Display_Integer_Error; -- this one has an erroneous parameter for Put_Line -- we discussed it a lot -- Number2 never read and never assigned (warning occurs) -- we commented out the USE clause and added the fully qualified reference

Display_Integer.adb with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; procedure Display_Integer is Number1: Integer; Number2: Integer; begin Number1 := 3; Put(Number1); New_Line; end Display_Integer; -- this works but we will remove USE clauses and -- substitute fully qualified names

Simple_Addition.adb with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; procedure Simple_Addition is Number1: Integer; Number2: Integer; begin Number1 := 3; Number2 := -5; Put(Number1); Put(Number2); Put(Number1+Number2); New_Line; end Simple_Addition; -- In this course when you do a computation, store the result before -- printing it. -- In this course we won’t use USE clauses

Simple_Addition_Better.adb with Ada.Text_IO; with Ada.Integer_Text_IO; procedure Simple_Addition_Better is Number1: Integer; Number2: Integer; Number3: integer; begin Number1 := 3; Number2 := -5; Ada.Integer_Text_IO.Put(Number1); Ada.Integer_Text_IO.Put(Number2); number3 := Number1+Number2; Ada.Integer_Text_IO.Put(Number3); ada.text_io.New_Line; end Simple_Addition_better;

Show_Titles.adb with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; procedure Show_Titles is Number1: Integer; Number2: Integer; begin Number1 := 3; Number2 := -5; Put("Number 1 = "); Put(Number1, 5); New_Line; Put("Number 2 = "); Put(Number2, 5); New_Line; Put("Sum of the two numbers = "); Put(Number1+Number2, 1); New_Line; New_Line; end Show_Titles; -- style problems: has use clauses -- has multiple statements on a line -- outputs the result of a computation without storing it

Show_Titles_better.adb with Ada.Text_IO; with Ada.Integer_Text_IO; procedure Show_Titles_better is Number1: Integer; Number2: Integer; Temp : integer; begin Number1 := 3; Number2 := -5; Ada.Text_IO.Put("Number 1 = "); Ada.Integer_Text_IO.Put(Item => Number1,width=> 5); -- named parameters Ada.Text_IO.New_Line; Ada.Text_IO.Put("Number 2 = "); Ada.Integer_Text_IO.Put(Number2, 5); -- positional parameters ada.text_io.new_line; Ada.Text_IO.Put("Sum of the two numbers = "); Temp = Number1 + Number2; Ada.Integer_Text_IO.Put(Temp, width=> 1); -- 1 positional, 1 named Ada.Text_IO.New_Line(2); ada.integer_text_io.put (width =>5, item => number1); -- parameter order doesn't matter with named parameters end Show_Titles_better; -- conforms to course style guidelines -- when have positional and named parameters, named must follow positional ones

Named_Parameters_Better.adb with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; procedure Named_Parameters_Better is Number1: Integer; Number2: Integer; begin Number1 := 3; Number2 := -5; Put(Item => "Number 1 = "); Put(Item => Number1, Width => 5); New_Line; Put(Item => "Number 2 = "); Put(Item => Number2, Width => 5); New_Line; Put(Item => "Sum of the two numbers = "); Put(Item => Number1+Number2, Width => 1); New_Line; end Named_Parameters_Better; -- still has use clauses -- still doesn’t store computed result before printing it -- BUT each statement is on a separate line

Show_Float_Without_Use_Clauses.adb with Ada.Text_IO; with Ada.Float_Text_IO; procedure Show_Float_Without_Use_Clauses is Number1: Float; Number2: Float; begin Number1 := 3.5; Number2 := -5.27; ada.text_io.Put(Item => "Number 1 = "); ada.float_text_io.Put(Item => Number1); ada.text_io.New_Line; ada.text_io.Put(Item => "Number 2 = "); ada.float_text_io.Put(Item => Number2, Fore => 5, Aft => 4, Exp => 0); ada.text_io.New_Line; ada.text_io.Put(Item => "Sum of the two numbers = "); ada.float_text_io.Put(Item => Number1+Number2, Fore => 4, Aft => 1); ada.text_io.New_Line; end Show_Float_Without_Use_Clauses;

Conditional_Without_Use_Clauses.adb with Ada.Text_IO; with Ada.Integer_Text_IO; procedure Conditional_Without_Use_Clauses is Number1: Integer; Number2: Integer; LargerNum: Integer; begin Ada.text_io.Put("Please enter integer Number1 >"); Ada.Integer_text_io.Get(Number1); Ada.text_io.Put("Please enter integer Number2 >"); Ada.Integer_text_io.Get(Number2); if Number1 >= Number2 then LargerNum := Number1; else LargerNum := Number2; end if; Ada.text_io.Put("Number 1 ="); Ada.Integer_text_io.Put(Number1); Ada.text_io.New_Line; Ada.text_io.Put("Number 2 ="); Ada.Integer_text_io.Put(Number2); Ada.text_io.New_Line; Ada.text_io.Put("The larger of the two numbers ="); Ada.Integer_text_io.put(LargerNum); Ada.text_io.New_Line(2); end Conditional_Without_Use_Clauses;

Nested_Conditional_Better.adb with Ada.Text_IO; with Ada.Integer_Text_IO; procedure Nested_Conditional_Better is Number1, Number2, Number3: Integer; begin Ada.text_io.Put("Please enter 3 integers >"); Ada.Integer_Text_IO.Get(Number1); Ada.Integer_Text_IO.Get(Number2); Ada.Integer_Text_IO.Get(Number3); Ada.text_io.Put("The largest of the three numbers = "); if Number1 >= Number2 then if Number1 >= Number3 then Ada.Integer_Text_IO.Put(Number1); else Ada.Integer_Text_IO.Put(Number2); end if; else if Number2 >= Number3 then Ada.Integer_Text_IO.Put(Number2); else Ada.Integer_Text_IO.Put(Number3); end if; end if; Ada.text_io.New_Line; end Nested_Conditional_Better;

Multiconditional_Better.adb with Ada.Text_IO; with Ada.Integer_Text_IO; procedure Multiconditional_Better is Number1: Integer; begin Ada.Text_IO.Put("Enter an integer between 1 and 3 inclusive > "); Ada.Integer_Text_io.Get(Number1); if Number1 = 1 then Ada.Text_IO.Put("I'm in case 1"); Ada.Text_IO.New_Line; elsif Number1 = 2 then Ada.Text_IO.Put("I'm in case 2"); Ada.Text_IO.New_Line; elsif Number1 = 3 then Ada.Text_IO.Put("I'm in case 3"); Ada.Text_IO.New_Line; end if; end Multiconditional_Better;

Show_Base.adb with ada.integer_text_io; with ada.text_io; procedure show_base is number : integer; begin number := 63; ada.integer_text_io.put (number); ada.text_io.new_line; ada.integer_text_io.put (number, 5, base => 2); ada.text_io.new_line; ada.integer_text_io.put (number, 5, base => 8); ada.text_io.new_line; ada.integer_text_io.put (number, 5, base => 16); ada.text_io.new_line; end show_base;

Show_attributes.adb with ada.integer_text_io; with ada.text_io; procedure show_attributes is type myArray is array (-3..10) of integer; type yourArray is array (5..30) of integer; type hisArray is array (100..105) of integer; myA : myArray; myY : yourArray; myH : hisArray; lower_bound, upper_bound,temp : integer; begin -- myArray lower_bound := myA'first; upper_bound := myA'last; ada.Integer_text_io.put (lower_bound); ada.Integer_text_io.put (upper_bound); ada.text_io.new_line; -- yourArray lower_bound := myY'first; upper_bound := myY'last; ada.Integer_text_io.put (lower_bound); ada.Integer_text_io.put (upper_bound); yada.text_io.new_line; -- hisArray lower_bound := myH'first; upper_bound := myH'last; ada.Integer_text_io.put (lower_bound); ada.Integer_text_io.put (upper_bound); ada.text_io.new_line; end show_attributes;

More_Show_Attributes.adb with ada.text_io; with ada.Integer_text_io; procedure More_Show_Attributes is temp: integer; begin -- showing proper use of 'pred temp := integer'last; ada.Integer_text_io.put (temp); temp := integer'pred(integer'last); ada.Integer_text_io.put (temp); -- showing proper use of 'succ ada.text_io.new_line; temp := integer'first; ada.Integer_text_io.put (temp); temp := integer'succ(integer'first); ada.Integer_text_io.put (temp); end More_Show_Attributes;