Lab 8 printf() modular arithmetic how to read assignments

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Fundamentals of Computer and programming in C Programming Languages, Programs and Programming Rohit Khokher.
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Chapter 04 (Part III) Control Statements: Part I.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Introduction to C Programming
Introduction to C Language
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.
Digesting a big problem ONE BYTE AT A TIME. Quiz YOU WILL WORK WITH YOUR PARTNER (LAB PARTNER NOT FULL TEAM). FOR SOLOISTS OR THOSE WHOSE PARTNER IS NOT.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
2.3 Output Formatting. Outputting Format Specify the number of spaces, “c”, used to print an integer value with specifier %cd, e.g., %3d, %4d. E.g. printf.
Topics Designing a Program Input, Processing, and Output
Algorithm: procedure in terms of
1-1 Logic and Syntax A computer program is a solution to a problem.
- Standard C Statements
Chapter 2 - Introduction to C Programming
CS1010 Discussion Group 11 Week 9 – Pointers.
CSC113: Computer Programming (Theory = 03, Lab = 01)
TMC 1414 Introduction to Programming
Chapter 2 - Introduction to C Programming
Lecture2.
C Formatted Input / Output Review and Lab Assignments
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
CSCE 206 Lab Structured Programming in C
CPU Design & Computer Arithmetic
Chapter 2 - Introduction to C Programming
A First Book of ANSI C Fourth Edition
Chapter 2 - Introduction to C Programming
CS2011 Introduction to Programming I Arrays (I)
Lecture3.
Making the PRINTF Look Better in G05 !!
CISC181 Introduction to Computer Science Dr
Seating “chart” Front Back 4 rows 5 rows 5 rows 4 rows 2 rows 2 rows
Topics Designing a Program Input, Processing, and Output
Introduction to C Programming
EPSII 59:006 Spring 2004.
Topics Designing a Program Input, Processing, and Output
Fundamental Programming
An Introduction to Programming with C++ Fifth Edition
Chapter 2 - Introduction to C Programming
CSCE 206 Lab Structured Programming in C
Introduction to C Programming
CS 1054 Introduction to Programming in Java
Presentation transcript:

Lab 8 printf() modular arithmetic how to read assignments what is the time-space tradeoff? discuss data cleanup algorithm give midterms back, discuss how grades are calculated last day to drop classes is Tues the October 15!

System.out.printf() Another way to print output. Probably the most powerful of the print statements due to its high configurability. Makes printing types as other types fairly easy. like System.out.print() you must specific newlines when you want them.

System.out.println("To print a float you specify %f in the printf"); System.out.println("and pass it an argument which it prints as a float"); System.out.println("Our floating point number f1 = 123.4567"); System.out.printf("System decimal places = %f \n", f1); System.out.printf(" one decimal places = %.1f \n", f1); System.out.printf(" two decimal places = %.2f \n", f1); System.out.printf(" three decimal places = %.3f \n", f1); System.out.printf(" ten decimal places = %.10f <- see floating point errror!\n", f1); Using the correct printf syntax allows you to customize your output WITHOUT having to modify your variable. Gives output: To print a float you specify %f in the printf and pass it an argument which it prints as a float System decimal places f1 = 123.456703 one decimal place f1 = 123.5 two decimal places f1 = 123.46 three decimal places f1 = 123.457 ten decimal places f1 = 123.4567031860 <- see floating point error!

Gives output: Double precision defaults to printing like float each type specifier has a different effect with f = 987654321.012346 with d = 987654321 with g = 9.87654e+08 with s d1 = 9.876543210123457E8 with e d1 = 9.876543e+08 with a d1 = 0x1.d6f345881948bp29 System.out.println("Double precision defaults to printing like float"); System.out.println("each type specifier has a different effect"); System.out.printf("with f = %f \n", d1); System.out.printf("with d = %d \n", (int)d1); System.out.printf("with g = %g \n", d1); System.out.printf("with s d1 = %s \n", d1); System.out.printf("with e d1 = %e \n", d1); System.out.printf("with a d1 = %a \n", d1);

Modular Arithmetic System of arithmetic where numbers wrap around, much the same way they do on a clock face. Excellent resource on modular arithmetic: http://www.math.cornell.edu/~morris/13 5/mod.pdf the modulo operator is ‘%’ and a quick example....

5 % 7 = 12 % 7 = 26 % 7 = 5 15 % 7 = 8 % 7 = 22 % 7 = 1

public class HelloWorld { public static void main(String[] args) { System.out.println("Absolute Answers to math section of test"); System.out.println(" 2 + 3 * 2 = " + (2 + 3 * 2) ); System.out.println(" 2.5 + 10 / 4 = " + (2.5 + 10 / 4) ); System.out.println(" 1 + 2 + \"GSU\" = " + (1 + 2 + "GSU") ); System.out.println(" \"CS\" + 3 + 7 = " + ("CS" + 3 + 7) ); System.out.println(" 5 % 12 * 3 / 2 = " + (5 % 12 * 3 / 2) ); } Absolute Answers to math section of test 2 + 3 * 2 = 8 2.5 + 10 / 4 = 4.5 1 + 2 + "GSU" = 3GSU "CS" + 3 + 7 = CS37 5 % 12 * 3 / 2 = 7 WHY? (java book section 2.7) Precedence and Associativity

How to read questions? “Write a Java program, Mowing.java, that reads in the length (in yards) and width (in yards) of a rectangular yard and the length (in yards) and width (in yards) of a rectangular house situated in the yard. Your program should compute the time required (in minutes rounded off to second decimal place) to cut the grass at the rate of 2.3 square meters per second. A sample run of the program is shown below.” What are we asking you to do?

Unpack the question: 1. Identify what the question is giving you. a. length and width are in yards b. velocity is in meters/second c. one area is inside the other area, so you will be subtracting it out. 2. Identify what the question is asking for. a. output is in minutes -this requires a conversion from yards to meters -this requires a conversion from seconds to minutes -this requires eliminating distance to arrive a time b. program reads data -this requires a means of reading input c. program must round output correctly You now have all the information you need to go about formulating an algorithm to solve the problem.

time-space tradeoff this was discussed in class wed/thurs in a nutshell, sometimes you can see a speed increase by using more space. ie: with the data cleanup algorithm discussed in class. instead of trying to do this in place with an O(n2) sorting algorithm, create an output array and use an O(n) scan algorithm instead.

data cleanup algorithm There are concise notes on this posted on the course website. (notes for 10/09/2013)