Download presentation
Presentation is loading. Please wait.
1
Some Basics for Problem Analysis
2
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
3
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
4
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
5
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 = 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!
6
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
7
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!
8
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
9
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
10
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.