Some Basics for Problem Analysis and Solutions

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.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
CS101 Computer Programming I Chapter 4 Extra Examples.
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.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
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.
© 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
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
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.
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
C Formatted Input/Output
Lesson #5 Repetition and Loops.
I/O Streams File I/O 2-D array review
Chapter 2 - Introduction to C Programming
Lesson #5 Repetition and Loops.
CS1371 Introduction to Computing for Engineers
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.
Control Structures - Repetition
Control Structures Lecture 7.
Problem Solving: Brute Force Approaches
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Standard Input/Output Streams
Some Basics for Problem Analysis
Lecture 5A File processing Richard Gesick.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Lesson #5 Repetition and Loops.
Working with Data Files
CS 1430: Programming in C++ No time to cover HiC.
Chapter 2 - Introduction to C Programming
Input Validation CSCE 121 J. Michael Moore
CSCE 489- Problem Solving Programming Strategies Spring 2018
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
File Input and Output.
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 2 - Introduction to C Programming
Differences between Java and C
Introduction to Data Structure
Fundamental Programming
Chapter 2 - Introduction to C Programming
Chapter 2 part #3 C++ Input / Output
Lesson #5 Repetition and Loops.
Unit 3: Variables in Java
Introduction to C EECS May 2019.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
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.
Input/Output Streams, Part 1
Switch Case Structures
Presentation transcript:

Some Basics for Problem Analysis and Solutions

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 (several common ways of handling) 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 Consider sizes needed for intermediate and final computation Sometimes a long long int is needed, not just an int! The sizes involved can affect the algorithm you need to use to solve

Sizes of numbers Keep in mind the size of numbers you can represent: These are guarantees in C/C++, though many 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, natrij

Some common pitfalls (easier to avoid) Initialize your variables int t; t++; Remove debugging code The extra prints that you might have used for checking/debugging Read the instructions Output in sorted order, special output in some cases, etc. Careful with punctuation, spelling Be sure it works on the sample code Use file redirection/input

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