CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 2, Lecture 2.

Slides:



Advertisements
Similar presentations
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Advertisements

LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
Programming in Visual Basic
Introduction to C Programming
ITEC113 Algorithms and Programming Techniques
Computer Science 1620 Loops.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 2.
Chapter 2: Input, Processing, and Output
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Introduction to scripting
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
The Project AH Computing. Functional Requirements  What the product must do!  Examples attractive welcome screen all options available as clickable.
Computer Science 101 Introduction to Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
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.
Structured Programming Defn: This is an approach to program development that produces programs of high quality that are easy to test, debug, modify and.
Moodle (Course Management Systems). Assignments 1 Assignments are a refreshingly simple method for collecting student work. They are a simple and flexible.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
INTRODUCTION TO ALGORITHMS PROGRAMMING. Objectives Give a definition of the term algorithm Describe the various parts of the pseudocode algorithm or algorithm.
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
Computer Science 101 Introduction to Programming.
Main Memory Central Processor Unit Keyboard Input Device Secondary Memory Monitor Printer Output Devices.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
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.
Input, Output, and Processing
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 CS161 Introduction to Computer Science Topic #13.
Introduction to Programming with RAPTOR
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Introduction to Pascal The Basics of Program writing.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Programming, an introduction to Pascal
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 1.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Intermediate 2 Computing Unit 2 - Software Development.
The Hashemite University Computer Engineering Department
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Buffer Whenever a read or readln is executed and you type a line of data terminated by hitting ENTER, that line, along with the CR/LF end of line marker.
Introduction to Programming
Chapter 2 Introduction to C++ Programming
Chapter 7 Text Input/Output Objectives
Completing the Problem-Solving Process
Chapter 2: Input, Processing, and Output
Chapter 7 Text Input/Output Objectives
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
One-Dimensional Array Introduction Lesson xx
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 3: Introduction to Problem Solving and Control Statements
Introduction to Programming
Chapter 2: Input, Processing, and Output
Introduction to Programming
Presentation transcript:

CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 2, Lecture 2

Case Study 1 Converting Units of Measurements Problem Statement You work in a store that sells imported fabric, measured in square meters; The store customer wants to know the equivalent amount in square yards. You need to write a program that performs the conversion.

Case Study 1 Converting Units of Measurements Analysis: –Input: Fabric size in square meters (SqMeters). –Output: Equivalent amount in square yards (SqYards). –Relevant Formulas: The relationship between sq. meter & sq. yards (1 m 2 = y 2 )

Case Study 1 Converting Units of Measurements Design: –Algorithm: 1. Read the fabric size in m Convert size to yd 2 using the formula (1 m 2 = y 2 ). 3. Display fabric size in yd 2. –Refinement of step2: 2.1 Fabric size in yd 2 = * fabric size in m 2

Case Study 1 Converting Units of Measurements Implementation –Program header Give a logical name for your program –Program Name:ConvertSqMtoSqYd; The name of the source code file of the program can be different from the logical name –File name:CONVMYSQ.PAS –TurboPascal limits file names to 8 characters –Declaration section Memory cells used in your program Constant declaration: use constants for unchanged values Variable declaration: use variables for values that change –Program body Data processing takes place here

Case Study 1 Converting Units of Measurements Implementation (Using Turbo Pascal) program ConvertSqMtoSqYd; {Convert square meters to square yards } constMetersToYards = 1.96;{conversion constant} varSqMeters,{input - fabric size in meters} SqYards: real;{output - fabric size in yards} begin {Read the fabric size in square meters.} WriteLn ('Enter the fabric size in square meters >'); ReadLn(SqMeters); {Convert the fabric size to square yards.} SqYards := MetersToYards * SqMeters; {Display the fabric size in square yards. } WriteLn ('The fabric size in square yards is ', SqYards) end.

Case Study 1 Converting Units of Measurements Testing (Output Window): –A program run Enter the fabric size in square meters > 2.00 The fabric size in square yards is E+00

Case Study 2: Square & Cube Problem Statement –Write a program that calculates the square root, square, and cube of a number Is this statement clear & sufficient for analysis & design steps? –NO

Case Study 2: Square & Cube Modified Problem Statement Write a program that gets an integer number from the user, calculates the square root, square and cube of the number and then prints the number,the square root, the square, and the cube. If the number is negative, the square root should not be evaluated. An error message should be written instead. The output should be labeled with suitable headings.

Case Study 2: Square & Cube Analysis –Input Get a number from the user –Processing & formulas Calculate the square root, the square, & the cube –SquareRoot = sqrt(Number) –Square = Number x Number. –Cube = Number x Square. –Output Write labels for Number, Square root, Square, and Cube Write values of Number, Square root, Square, and Cube (This is one way of doing it…)

Case Study 2: Square & Cube Design (Algorithm) 1.Pseudocode for initialization Clear screen Write program purpose 2.Pseudocode for Input Read Number 3.Pseudocode for Processing SquareRoot = Sqrt(Number) Square = Number * Number Cube = Square * Number 4. Pseudocode for Output Write “Number”, “Sq. Root”,“Square”, “Cube” Write Number, SquareRoot, Square, Cube 5. Pseudocode for termination Write Goodbye message

Case Study 2: Square & Cube Implementation –Program header program SquareNCube; –Declaration section const HEAD = ‘Number Sq.Root Square Cube’; { Heading } var Number, { input- number being processed } Square, { output - square of the number } Cube : integer; { output- cube of the number } SquareRoot : real; {output- square root of the number} –Program body

Case Study 2: Square & Cube Implementation: First draft –Program Body Enclosed between begin & end. Write algorithm as comments begin{Program body} {1. Initialize} {2. Read Number } {3. Calculations / Evaluate formulas} {4. Output heading & values} {5. Terminate} end.

Case Study 2: Square & Cube Implementation: Second draft Add Pascal statements in corresponding places begin{Program body} {1. Initialize} Clrscr; WriteLn(‘This program calculates square rt., ‘); WriteLn(‘square & cube of a given number’); {2. Read Number } write (‘Enter a number ’); readln (Number); {3. Evaluate formulas} if (Number >= 0) then SquareRoot := sqrt(Number) else WriteLn(‘Square root can't accept negative values’); Square:= Number * Number; {evaluate square} Cube:= Square * Number; {evaluate cube} {4. Output heading & values} { writeln ( HEAD ); } writeln ( ‘Number Sq.Root Square Cube’); writeln ( Number, SquareRoot, Square, Cube ) {5. Terminate} WriteLn(‘Good bye’); ReadLn; end.

Case Study 2: Square & Cube Implementation: (Final Pascal Code) program SquareNCube; const{ Declaration part } HEAD = ‘N umber Sq.Root Square Cube’; { Heading } var Number, { input- number being processed } Square, { output - square of the number } Cube : integer; { output- cube of the number } SquareRoot : Real; {output- square root of the number} begin { Program body } { Initialize} Clrscr; WriteLn(‘This program calculates square rt., ‘); WriteLn(‘square & cube of a given number’); { Read Number } write (‘Enter a number’); readln (Number); { Evaluate formulas } if (Number >= 0) then SquareRoot := sqrt(Number) else WriteLn(‘Square root can't accept –ve values’); Square := Number * Number; { evaluate square } Cube := Square * Number; { evaluate cube } { Output heading & values } writeln ( HEAD ); writeln ( Number, SquareRoot:6:2,Square, Cube ); { Terminate} WriteLn(‘Good bye’); ReadLn; end. { end of program body }

Ideas from Students A program for keeping track of my grades in a course –Easy A program like those in graphic calculators that allows entering the data and will automatically display mean / average, the frequency, the median, the mode … –Easy, we’ll do a simple version of it A special dictionary that will enable me to write customized terms and the computer will automatically recognize the word I meant and transform it by itself. Ex: dvlp would stand for development. That would be very time saving while writing reports. –Not hard, but in this course we don’t learn everything we need A program for planning courses: what courses do I take next and what courses do I have left. –A planning and scheduling problem, can be arbitrarily hard A program for managing my time and schedule –If you know an algorithm for doing that, let me know….

A Limitation In this course, given the syllabus, we are limited to solving problems where you know beforehand how many inputs you will have or where you do not have to keep track of the values of individual inputs (unless you use external files to do so). Why? –Because we will look at only a limited set of ways of representing data in memory

Case Study 3: Keeping Track of Grades in a Course A program for keeping track of my grades in a course –Is this specified clearly enough?

Case Study 3: Revised Problem Statement A program that will let me enter and store the score that I receive for each assessment in the course, the maximum score that I could have obtained in that assessment, and the name of the assessment. When I run the program, it will retrieve and display the scores it already knows and give me the option to enter the new data. When I exit the program, all the data (old and new) will be stored to a file so that it can be retrieved next time the program is run.

Case Study 3: Begin Analysis A program that will let me enter and store the score that I receive for each assessment in the course, the maximum score that I could have obtained in that assessment, and the name of the assessment. When I run the program, it will retrieve and display the scores it already knows and give me the option to enter the new data. When I exit the program, all the data (old and new) will be stored to a file so that it can be retrieved next time the program is run.

Case Study 3: I/O Analysis Input: Existing data from file New data from keyboard Output: Existing data plus new data to file

Case Study 3: Processing Analysis Processing: –Display existing data Decide: about how to display the data. E.g. Table with headers: Assessment My Score Max Score HW# …. … …. –Prompt for new data: Data: Name of assessment, My score, Max score Decide: –prompt for data one by one or all 3 at a time? –prompt for data for a single assessment or multiple assessments? If multiple assessments, how do you indicate you are done? –Store old and new data: Decide: –Overwrite the old file? –Make a new version of the file? Decide: –Display all the data the program knows about before exiting?

Case Study 3: Revised Problem Statement A program that will let me enter and store the score that I receive for each assessment in the course, the maximum score that I could have obtained in that assessment, and the name of the assessment. When I run the program, it will: –Retrieve and display the scores it already knows in a table with suitable headers –Give me the option to enter data for one or more additional assessments or to exit –If I choose to enter data for an assessment it will prompt me for all three values on one line –After I enter the values it will again give me the option to enter new data or to exit. When I exit the program –all the data (old and new) will be stored to a new file so that it can be retrieved next time the program is run. –all the data it has for me will be shown. Are you starting to think that you should use a spreadsheet instead?