Some Basics for Problem Analysis

Slides:



Advertisements
Similar presentations
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Advertisements

1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
CS 117 Spring 2002 Using Files Hanly: Chapter 9, some in Chapter 4 Freidman-Koffman: Chapter 8, iomanip in Chapter 5.
CMSC 104, Version 8/061L15Switch.ppt The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
CMSC 104, Version 9/011 The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7,
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
1 ENERGY 211 / CME 211 Lecture 7 October 6, 2008.
Files A collection of related data treated as a unit. Two types Text
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Dynamic Allocation in C
C Formatted Input/Output
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Strings CSCI 112: Programming in C.
I/O Streams File I/O 2-D array review
Chapter 2 - Introduction to C Programming
Introduction to C CSE 2031 Fall /3/ :33 AM.
CS1371 Introduction to Computing for Engineers
Lab 7: File Input/Output Graham Northup
Getting Started with C.
CS1010 Discussion Group 11 Week 7 – Two dimensional arrays.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 2 - Introduction to C Programming
File Input/Output.
Chapter 2 part #3 C++ Input / Output
Programming in C Input / Output.
INPUT STATEMENTS GC 201.
Some Basics for Problem Analysis and Solutions
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Standard Input/Output Streams
Standard Input/Output Streams
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Lesson #5 Repetition and Loops.
Working with Data Files
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
CS 1430: Programming in C++ No time to cover HiC.
Input Validation CSCE 121 J. Michael Moore
The switch Statement Topics Multiple Selection switch Statement
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
File Input and Output.
Differences between Java and C
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Dynamic Allocation in C
Programming in C Input / Output.
Fundamental Programming
COP 3330 Object-oriented Programming in C++
Chapter 2 part #3 C++ Input / Output
Unit 3: Variables in Java
Module 12 Input and Output
Introduction to C EECS May 2019.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
The switch Statement Topics Multiple Selection switch Statement
CS31 Discussion 1D Fall18: week 2
Introduction to C Programming
EECE.2160 ECE Application Programming
Input Validation CSCE 121 Based on slides created by Carlos Soto.
Testing & Security Dr. X.
Input/Output Streams, Part 1
Presentation transcript:

Some Basics for Problem Analysis

Problem Statements Read through a problem to identify the important information needed. What is really being asked for? Ignore the things not relevant to the problem itself. Example: carrots problem Read through details of input/output Range of input values Details about formatting output Examine sample input/output Might clarify how particular situations should be handled Might give details about formatting

Input – end of input One-input case only Read in a number giving size of input Flag – read until a specific value is encountered Sometimes input end is not specified In this case, assume repeat until “End Of File” (EOF) C++: check cin.eof(), if true, are at EOF. Usually, must first try to read past EOF before EOF is true! // cin.eof() = false cin >> a; // But nothing read in! cin.eof() = true

Sizes needed Examine sizes specified in input for arrays If a maximum size is known, it can be easier to allocate fixed memory, rather than dynamically allocate Be careful about unnecessary extra allocation/deallocation

Sizes of numbers Keep in mind the size of numbers you can represent: These are guarantees in C/C++, though some systems have more: Integer: 32 bit (int or short): -2^15+1 to 2^15-1 = -32767 to 32767 Integer: 32 bit (unsigned short/int): 0 to 2^32-1 = 0 to 65,535 Integer: 64 bit (long int): -2^31+1 to 2^31-1: −2,147,483,648 to +2,147,483,647 (i.e. +/- 2x10^9) Integer: 64 bit (unsigned long) 0 to 2^64-1: 0 to 4,294,967,295 (4x10^9) Integer:128 bit (long long or Java long): −9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 (i.e. +/- 9x10^18) If you will exceed that, need an arbitrary precision library Keep in mind that the order of operations can affect this!

Reading in input lines Sometimes need to read a whole line to process it e.g. read as a string, including spaces, then process that string Especially if the format is not exactly known Can then use stringstream If reading fails with input stream: cin is false Need to reset cin: cin.clear() to resume reading

Reading in input lines Remember that reading a line will possibly pull a newline from the previous line: // assume input line is 10 cin >> a // reads the 10 into a, but not the line break at the end getline(cin, s) // reads an empty string in (since no string before line break) getline(cin, s) // NOW it reads the next line in!

Output Remember to flush output Formatting output See section 1.2.4 cin/cout flush the other cout generally flushes when a newline is written can explicitly cout.flush() Formatting output String streams can be useful to buffer output for writing C-style printf is often easier to format spacing when needed And scanf is often better for input than cin! See section 1.2.4

Some Basic Testing (we will return to more) The sample input/output is NOT sufficient But is, obviously, necessary Try doubling the test case – can help catch some initialization errors Identify corner cases The extremes of ranges of input – both max and min Identify the variations in cases 1, 2, 3, 4 in a row. Plan to handle the large cases It’s not always convenient to write large test cases on the fly But, your code will be tested on the largest cases Example: bus numbers; server

Assignment (on your own) Go through Exercise 1.2.3 Try it on your own Then, check your answer Work to make sure you understand the implementations And ideally can code/reproduce them on your own