© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Examples of loops and nested loops.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

Introduction to Computing Science and Programming I
Chapter 3 Reading a Set of Data. CIS 1.5 Introduction to Computing with C++3-2 Reading a Set of data A Simple Payroll Program Problem: Write a complete.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Control Structures for Loops.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
Computer Science 1620 Loops.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
CS150 Introduction to Computer Science 1
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Chapter 8 Arrays and Strings
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
© Janice Regan, CMPT 128, Feb CMPT 128: Introduction to Computing Science for Engineering Students Running Time Big O Notation.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
13&14-2 Know the forms of loop statements in C (while,do/while,for). Understanding how data conversion occurs. Read/Write data in files using Unix redirection.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
Chapter 8 Arrays and Strings
Input, Output, and Processing
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions Parameters passed by reference.
5 While-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Chapter 4 Introduction to Control Statements
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
 What Does This Program Do? ACSL.  Frequently one must use or modify sections of another programmer’s code.  It is essential to be able to read and.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Arrays Chapter 7.
Exceptions Exceptions are used to signal that an unexpected event has happened in a program C++ will generate exceptions for some errors in the program.
Chapter 4 – C Program Control
C++ Basic Input and Output (I/O)
Topics Designing a Program Input, Processing, and Output
User-Written Functions
Bill Tucker Austin Community College COSC 1315
Introduction to Programming
Variables, Expressions, and IO
Lecture 13 & 14.
Lecture 07 More Repetition Richard Gesick.
Lecture2.
Structured Programming (Top Down Step Refinement)
CMPT 102 Introduction to Scientific Computer Programming
Introduction to Java Applications
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
EECE.2160 ECE Application Programming
Unit 3: Variables in Java
EECE.2160 ECE Application Programming
Presentation transcript:

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Examples of loops and nested loops

© Janice Regan, CMPT 102, Sept Problem  Write a program to count the number of each of the following in a file containing text  The vowels a, e, i, o, u, y  All other characters (not vowels) except white space (tab, space, newline …)  The user will enter the filename, and must be able to count vowels in multiple files without restarting the program

© Janice Regan, CMPT 102, Sept Algorithm  Your program should  Prompt the user for the name of the file containing the text  Open the file and read the characters from the file. The file can contain any number of characters in any number of lines..  Count the number of each of the following vowels in the file a e i o u y  Count the number of other characters (excluding white space such as spaces and tabs) in the file  print the summary statistics  Prompt the user the see if the user wants to count the vowels in another file  If the user decides to count the vowels in another file return to 1. and continue, otherwise end the program execution.

© Janice Regan, CMPT 102, Sept Problem Statement  You will be tabulating the data resulting from a field count of a particular type of bird. Each observer has provided the number of birds counted, and the area in acres of the observation region in which those birds were counted. You must determine the total number of birds counted, and the number of birds per acre.

© Janice Regan, CMPT 102, Sept Problem Analysis  Make the following assumptions:  The coordinator of the experiment will be provided you with a file, surveydata.txt, containing the survey data after the survey is complete. The format of the data in that file is as follows  The number of observers, numberObs, is an integer on the first line of the file.  Following the first line in the file there is one line for each observer. On each of these lines is an integer, NumBirds, indicating the number of birds counted a double, AreaAcres, indicating the area of the observation region You may assume these two numbers are separated by a blank space.  There is data for more than one observer. (numberObs>1)  More information about the survey is included in the file following the lines of interest to you.

© Janice Regan, CMPT 102, Sept Designing an algorithm  Your C program should complete each of the following tasks:  Read an integer numberObs from the first line of file surveydata.txt  Use a for loop to Read the next line of data from file input.txt. The number of birds is nBirds, and the area in acres is areaAcres. Add the number of birds for this observer to the total number of birds, TNumBirds. Add the area for this observer to the total area, TArea.  Determine the number of birds per acre, birdsPerAcre.  Print the total number of birds and the number of birds per acre on separate lines. Each line should include an explanation of what the printed number represents.

© Janice Regan, CMPT 102, Sept Problem Statement  Altitude and velocity data has been collected for a weather balloon. Write a program to print a table of altitude in meters and velocity in meters per second as at 10 minute intervals for a two hour period starting 4 hours after the launch of the weather balloon. Also determine the balloons maximum altitude during the period covered by the table.  Based on problem 4.43 Etter(2005)

© Janice Regan, CMPT 102, Sept Problem Analysis  Background  Weather balloons are used to gather pressure and temperature information at various altitudes in the atmosphere. The balloon rises when the helium inside is less dense than the surrounding air. When the balloon is warmed by the sun the helium expands (becomes less dense) and the balloon rises. When the sun sets the balloon cools and the helium contracts (becomes more dense) and the balloon’s altitude decreases. At given intervals measurements of altitude, velocity, and temperature are made.

© Janice Regan, CMPT 102, Sept Problem analysis assumptions  Measurements were taken over the first 48 hours of the flight of a weather balloon.  The data gathered were approximated by polynomials ( a polynomial that produced a line that fit the data well was found) Altitude  alt(t) = -0.12t t t t Velocity  v(t) = -0.48t t t

© Janice Regan, CMPT 102, Sept Problem Analysis  What do we know  The polynomials  The interval between successive interpolated results (the value we would expect to see at that time based on our observed data which may be at times before and after the time of interest)  The start and end time of observations to be included in the table (should start no earlier than the launch of the balloon and end no later than 48 hours after the launch when out data ends)

© Janice Regan, CMPT 102, Sept Program Analysis  Inputs  Starting time  Time increment  Ending time  NOTE: polynomials will be hard coded in the program, this program will be useful only for this set of data  Outputs  A table showing the altitude and velocity at n minute intervals starting k minutes after launch and ending l minutes after launch  The maximum altitude reached by the balloon

© Janice Regan, CMPT 102, Sept Designing an Algorithm  Read Inputs (start time, end time, interval)  Verify data (check start time < end time, start time and end time both < 48 hrs, interval < 48 hrs …) this step is omitted in the sample program  Print a title for the table (Weather Balloon Data)  Print headings for the columns in the table (TimeHeight Velocity)  Initialize the maximum altitude to 0  Initialize time of maximum altitude to start time  For each time (each row in the table)  Evaluate the alt(t) to get the height  Evaluate the v(t) to get the velocity  Print a row in the table (time height velocity)  Check to see if the altitude is larger than the maximum altitude I f it is update the maximum altitude to be the altitude for the present time and update the time of maximum altitude to be the present time  Print the maximum altitude and the time that it occurred

© Janice Regan, CMPT 102, Sept Left to do  Modify the sample program so that the input values are checked for consistency and correctness

© Janice Regan, CMPT 102, Sept Problem Statement  Calculate how many ways you can choose r objects from n different objects.  You should build your program using reusable functions for factorial and combinations.

© Janice Regan, CMPT 102, Sept Analysis  Need to write two functions, begin by declaring the prototypes  int factorial( int n );  Calculates n! n*(n-1)*(n-2)*… *2*1  int combination( int n, int r );  Calculates how many ways r objects can be chosen from m objects  The number of combinations (the number of ways r objects can be chosen from m objects) is calculated using the formula. Write the Factorial function

© Janice Regan, CMPT 102, Sept Function to compute a factorial int factorial( int n) { /* declare local variables */ int i; int product; /* initialize local variables */ product = 1; for (i = n; i >1; i--) { product *= i; } return (product); }

© Janice Regan, CMPT 102, Sept Is this a useful factorial function?  In this program the return value is an integer  This makes sense since the value of n! is an integer  However, the largest integer that can be represented using an int is  For what value of n does n! exceed (13)  This function will only work for values up to 12  For a more generally useful factorial function we could make the return value a floating point number. (see alternate code provided)

© Janice Regan, CMPT 102, Sept Function to compute a factorial double factorial( int n) { /* declare local variables */ int i; double product; /* initialize local variables */ product = 1; for (i = n; i >1; i--) { product *= i; } return (product); }

© Janice Regan, CMPT 102, Sept combination function double combination(int n, int r ) { if(r =0) { return( factorial(n) / (factorial(r) * factorial(n-r)) ); } else { return( -1 ); } }j