1 / 19

CMPT 102 Introduction to Scientific Computer Programming

CMPT 102 Introduction to Scientific Computer Programming. Examples of loops and nested loops. 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

Download Presentation

CMPT 102 Introduction to Scientific Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMPT 102Introduction to Scientific Computer Programming Examples of loops and nested loops

  2. 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

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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)

  8. 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.

  9. 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.12t4 + 12 t3 - 380t2 + 4100t + 220 Velocity • v(t) = -0.48t3 - 36t2 + 760t + 4100

  10. 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)

  11. 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

  12. 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 (Time Height 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 • If 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

  13. Left to do • Modify the sample program so that the input values are checked for consistency and correctness

  14. 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.

  15. 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

  16. 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); }

  17. 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 232. • For what value of n does n! exceed 232. (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)

  18. 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); }

  19. combination function double combination(int n, int r ) { if(r <= n && n >=0) { return( factorial(n) / (factorial(r) * factorial(n-r)) ); } else { return( -1 ); } }j

More Related