Homework #2: Functions and Arrays By J. H. Wang Mar. 20, 2012.

Slides:



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

Week 8 Arrays Part 2 String & Pointer
1 C++ Syntax and Semantics The Development Process.
Homework #2: Functions and Arrays
CS102--Object Oriented Programming Discussion 2: (programming strategy in java) – Two types of tasks – The use of arrays Copyright © 2008 Xiaoyan Li.
Hand Crafting your own program By Eric Davis for CS103.
MPI Program Structure Self Test with solution. Self Test 1.How would you modify "Hello World" so that only even-numbered processors print the greeting.
The Efficiency of Algorithms
Computer Science 1620 Programming & Problem Solving.
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
Chapter 8 Arrays and Strings
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Programming is instructing a computer to perform a task for you with the help of a programming language.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
1 Project 5: Median. 2 The median of a collection of numbers is the member for which there are an equal number less than or equal and greater than or.
An Introduction to Textual Programming
CS1101: Programming Methodology Aaron Tan.
High-Level Programming Languages: C++
Numbering Systems CS208.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Foundations of Computer Science Computing …it is all about Data Representation, Storage, Processing, and Communication of Data 10/4/20151CS 112 – Foundations.
Chapter 3 Section 1 Number Representation Modern cryptographic methods, unlike the classical methods we just learned, are computer based. Representation.
11 Chapter 5 METHODS CONT’D. 22 MORE ON PASSING ARGUMENTS TO A METHOD Passing an Object Reference as an Argument to a Method Objects are passed by reference.
First tutorial.
Warm-up Grab a die and roll it 10 times and record how many times you roll a 5. Repeat this 7 times and record results. This time roll the die until you.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
Homework #3: Classes and Constructors
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Comp 335 File Structures Hashing.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
6.2 Homework Questions.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Date processing and conversion Please use speaker notes for additional information!
Week 6.  Lab 1 and 2 results  Common mistakes in Style  Lab 1 common mistakes in Design  Lab 2 common mistakes in Design  Tips on PE preparation.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
1 Flight Times. 2 Problem Specification 3 Additional Specifications You may assume that the input is a valid 24 hour time. Output the time entered by.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Scis.regis.edu ● CS-361: Control Structures Week 6 Dr. Jesús Borrego Lead Faculty, COS Regis University 1.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Homework #1: C++ Basics, Flow of Control, and Function Basics By J. H. Wang Mar. 13, 2012.
Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.
Homework #4: Operator Overloading and Strings By J. H. Wang Apr. 17, 2009.
Homework #2: Functions and Arrays By J. H. Wang Mar. 24, 2014.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Homework #1: C++ Basics, Flow of Control, and Function Basics
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Strings Chapter 5.
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Homework #3: Classes and Constructors By J. H. Wang Apr. 14, 2014.
Homework #5: Pointers, Dynamic Arrays and Inheritance By J. H. Wang Jun. 5, 2009.
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.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
int [] scores = new int [10];
Homework 1 (due:April 8th) Deadline : April 8th 11:59pm Where to submit? eClass “ 과제방 ” ( How to submit? Create a folder. The name.
Homework #3: Classes and Constructors
Fun fun ’til daddy takes your C turbo away! A C program is organized into one or more modules (source files created by a programmer), within which are.
Homework #3: Classes and Constructors By J. H. Wang Apr. 24, 2015.
Lecture 2 What is a computational problem? What is an instance of a problem? What is an algorithm? How to guarantee that an algorithm is correct? What.
Introduction to programming in java Lecture 21 Arrays – Part 1.
EGR 2261 Unit 10 Two-dimensional Arrays
Homework 3 (due:May 27th) Deadline : May 27th 11:59pm
© 2016 Pearson Education, Ltd. All rights reserved.
Homework 3 (due:June 5th)
Starter Write a program that asks the user if it is raining today.
Introduction to pseudocode
Introduction to C++ Programming
Digital Electronics and Microprocessors
Presentation transcript:

Homework #2: Functions and Arrays By J. H. Wang Mar. 20, 2012

Programming Exercises 1.Write a C++ program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 P.M. The input is given as two integers. There should be at least three functions: one for input, one to do the conversion, and one for output. Record the A.M./P.M. information as a value of type char, ‘A’ for A.M. and ‘P’ for P.M. Thus, the function for doing the conversions will have a call-by-reference formal parameter of type char to record whether it is A.M. or P.M. (The function will have other parameters as well.) Include a loop that lets the user repeat this computation for new input values again and again until the user says he or she wants to end the program.

2.An array can be used to store large integers one digit at a time. For example, the integer 1234 could be stored in the array a by setting a[0] to 1, a[1] to 2, a[2] to 3, and a[3] to 4. However, for this exercise you might find it more useful to store the digits backward, that is, place 4 in a[0], 3 in a[1], 2 in a[2], and 1 in a[3]. [… to be continued on the next slide …]

[… continued from the previous slide …] In this exercise you will write a program that reads in two positive integers that are 20 or fewer digits in length and then outputs the sum of two integers. You program will read the digits as values of type char so that the number 1234 is read as the four characters ‘1’, ‘2’, ‘3’, and ‘4’. After they are read into the program, the characters are changed to values of type int. [… to be continued on the next slide …]

[… continued from the previous slide …] The digits will be read into a partially filled array, and you might find it useful to reverse the order of the elements in the array after the array is filled with data from the keyboard. Your program will perform the addition by implementing the usual paper-and-pencil addition algorithm. The result of the addition is stored in an array of size 20 and the result is then written to the screen. [… to be continued on the next slide …]

[… continued from the previous slide …] If the result is an integer with more than the maximum number of digits (that is, 20), then your program should issue a message saying that it has encountered “integer overflow.” You should be able to change the maximum length of integers by changing only one globally defined constant. Include a loop that allows the user to continue to do more additions until the user says the program should end.

3.The birthday paradox is that there is a surprisingly high probability that two people in the same room happen to share the same birthday. By birthday, we mean the same day of the year (ignoring leap years), but not the exact birthday including the birth year or time of day. Write a C++ program that approximates the probability that two people in the same room have the same birthday, for 2 to 50 people in the room. […to be continued on the next slide…]

[… continued from the previous slide …] The program should use simulation to approximate the answer. Over many trials (say, 5,000), randomly assign birthdays to everyone in the room. Count up the number of times at least two people have the same birthday, and then divide by the number of trials to get an estimated probability that two people share the same birthday for a given room size. […to be continued on the next slide…]

[… continued from the previous slide …] Your output should look like the following. It won’t be exactly the same due to the random numbers: For 2 people, the probability of two birthdays is about For 3 people, the probability of two birthdays is about For 4 people, the probability of two birthdays is about … For 49 people, the probability of two birthdays is about For 50 people, the probability of two birthdays is about 0.969

Homework Submission Due: 2 weeks (Apr. 3, 2012) Submission instructions: – – File names : Please name your file according to our homework. For example, HW1.zip or quiz2.rar

Questions or Comments?