Presentation is loading. Please wait.

Presentation is loading. Please wait.

Some Basics for Problem Analysis and Solutions

Similar presentations


Presentation on theme: "Some Basics for Problem Analysis and Solutions"— Presentation transcript:

1 Some Basics for Problem Analysis and Solutions

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 (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

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 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

5 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 = 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, natrij

10 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

11 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


Download ppt "Some Basics for Problem Analysis and Solutions"

Similar presentations


Ads by Google