Title: How to Program
1How to Program
- The key to programming is to
- Read and understand the problem
- Understand what's asked for
- Think about any questions about the problem
- Draw a "Black Box"
- Think about how YOU would do it. Do some
examples on paper. - Divide-and-conquer
2How to Program
- Write a C function that determines, given an
array of characters and a count of the number of
characters in the array, whether the characters
in the array form a palindrome. Assume the
result might be used elsewhere in a program.
3How to Program
- Never develop a solution that is different than
the way you do it. - Develop a black box
- Use divide-and-conquer and iteration charts.
4How to Program
is x a palindrome?
x
char
bool
is_palindrome
length
int
bool is_palindrome (char x , int length)
- Assume the result might be used elsewhere in a
program.
given an array of characters
and a count of the number of characters in the
array
5How to Program
So, what do YOU do???? Do you re-write the string
backwards and compare the two strings? NO Do you
figure out where the middle of the string is and
keep moving until you hit the middle? NO What you
do is
6How to Program
aXtY???z123456bbbcbbb654123z???tYXa ? ?
??
7How to Program
put down your left and right fingers
check whether string is a palindrome
8How to Program
put down your left and right fingers
assume until further notice that it is a
palindrome
check whether it really is
9How to Program
put down your left and right fingers
assume until further notice that it is a
palindrome
move your fingers as long as they dont meet and
you still think its a palindrome
10How to Program
put down your left and right fingers
assume until further notice that it is a
palindrome
while your fingers havent met and you still
think its a palindrome
change your mind if you see a mismatch
11How to Program
put down your left and right fingers
assume until further notice that it is a
palindrome
while your fingers havent met and you still
think its a palindrome
do you see chars that dont match?
yes
no
its not a palindrome
12How to Program How to Program
define LEFTMOST_POSITION 0 define
RIGHTMOST_POSITION length - 1 define
HAS_NOT_MET lt define AND bool
is_palindrome (char x, int length) int
left_finger, right_finger bool
is_a_palindrome left_finger
LEFTMOST_POSITION right_finger
RIGHTMOST_POSITION is_a_palindrome
true for ( left_finger HAS_NOT_MET
right_finger AND is_a_palindrome
left_finger, right_finger--) if
(xleft_finger ! xright_finger) is_a_palind
rome false return is_a_palindrome
13How to Program
bool is_palindrome (char x, int length) int
j, k bool is_a_palindrome true for (j 0,
k length - 1 j lt k is_a_palindrome j,
k--) if (xj ! xk) is_a_palindrome
false return is_a_palindrome
14How to Program
You need to work on these things Thinking about
how you solve a given problem Designing a black
box Meeting problem specifications
exactly Divide-and-conquer
15Introduction
- A C programmer needs to develop a function (NOT
a main program) that is given - an array of integers, and
- a count of how many values are in the array,
- to determine whether or not any two consecutive
locations contain the same value. Assume that
the result might be used elsewhere in a program.
16Introduction
x
(int )
consecutive_pair_exists
(bool)
count
(int)
17Introduction
List 1 List 2 25 ? 10 ? 10 ? 50
? 25 0 10 10 10 90 40 80 50 7
0 20 80 20 90 20
18Introduction
Point your fingers at the first two elements of
the list. Move your fingers down the list until
you find two matching consecutive values or until
you fall off the end of the list If you havent
fallen off the end of the list, you found a
match!!!!
19Introduction
put your fingers on the first two elements
keep moving down as long as you havent fallen
off the end and the two values pointed at are not
equal
have we fallen off the end?
20Introduction
put your fingers on the first two elements
while you havent fallen off the end and the two
elements are not equal
move down the list
have we fallen off the end?
yes
no
the result is false
the result is true
21Introduction
k 0
while k lt count - 1 xk ! xk1
k
k count - 1
true
false
result false
result true
22Introduction
bool consecutive_pair_exists (int x , int
count) bool result int k while (k lt count
1 xk ! xk 1) k if (k count
1) result false else result
true return result
23Problem Solving
- Read and understand problem before proceeding
- Never develop a solution in some way different
than what you yourself would do - Think modularly (black box)
- Divide and conquer
24Programming Standards
- It is your responsibility to download, read,
understand AND always apply the programming
standards. - The standards include, but are not limited to,
the following main points - 1) Proper indentation
25Programming Standards
- 2) Proper use of uppercase and lowercase in the
definition of program identifiers - 3) Meaningful and readable program identifiers
- 4) Proper one-entry, one-exit structure. (No
gotos or continues and no breaks or returns from
inside loops, for example) - 5) Maximum of 30 lines for a function.
26Programming Standards
- 6) Proper header comment at the top of each
source or header file.
27Homework
- 1. If you havent done so already, send me an
e-mail with your name in the subject line. List
the programming courses you have taken, where and
when you took them. Tell me your major and
whether you are graduate or undergraduate. - 2. Write (compile and test) C programs that
tests the functions we have just developed.
28Another Problem
Develop a C program that reads a series of
integer values from cin and writes them to cout
in the reverse order from which they came in.
29Another Problem
Develop a C program that reads a series of
integer values from cin and writes them to cout
in the reverse order from which they came
in. We will not place a limit on how many
numbers will come in. We will keep reading
numbers until there are no more.
30Another Problem
We can not use an array, because that would limit
the number of values we can read. What we need
is some sort of structure that can hold as many
values as needed. Perhaps we can define a new
data type, which we will call a Vector, which
works just like an array, but can grow to
whatever size is needed during the execution of
the program.
31Data Abstraction
- We would like to be able to separate inour minds
the implementation of this newdata type and the
algorithm that will use it. - This data type should be a separate unitwithin
the program (encapsulation). - We should not be concerned about the details of
implementing this data typewhen developing our
program(information hiding).
32Program
include ltiostreamgt using namespace
std include vector.h int main() Vector
values int j, k for (k 0 cin gtgt
valuesk k) for (j k 1 j gt 0
j--) cout ltlt valuesj ltlt endl return
EXIT_SUCCESS
33Data Abstraction
- Note how we assumed a data type (Vector) that
would work like an array without having to worry
about its size. - Note how we were able to write our program
independently. - Now lets define a Vector
34Data Abstraction
- A Vector should be indexable we should be able
to access any element by xk. - We might also want to incorporate a count of the
number of values actually in the vector if
properly maintained, we wont need to maintain
this as a separate variable. - The allocation of space should be seamless
invisible to the application.
35Data Abstraction
We use the C class mechanism to define an
ADT class class-name public constructors
and destructors methods (application
interface) private internal elements for
implementing the ADT
36Data Abstraction
typedef int Value class Vector public Vect
or (const int initial_allocation) Vector()
Value operator (const int index) Value
operator (const int index) const bool
insert (const int index, const Value
value) private Value array int count,
allocated
37Working with Visual Studio C
To create a C project using the Visual C
compiler, note the following Create a Managed
Empty C Project. This will create a folder with
the same name as the project you created and is
located in whatever path you used. Outside of
Visual Studio, use Windows to copy whatever
pre-existing .h and .cpp files you need into that
newly created folder. Of course you dont have
to copy system files. In the Solution Explorer
frame in Visual Studio, under Source Files, add
each .cpp file to be used. Under Header Files
add each .h file you will use (no system header
files!)
38Working with Visual Studio C
You now have the beginnings of a Project. Use
the Visual Studio editor to create whatever
additional source and header files you need. Add
them to the project using the Solution Explorer
frame. You can now compile and debug your
program. Note If you need my assistance on a
project, create a zip file (compressed folder)
containing ONLY the .h and .cpp files of your
project. Zipping everything creates too big a
file. You should attach this to your e-mail.
39Homework Assignment
Using the IntVector ADT (modified to work with
doubles instead of ints), write a C program
that reads a series of double values from cin and
writes those values to cout in ascending order.
Think about doing an insertion sort, placing each
value into its appropriate spot as it comes in,
and making use of the given methods defined for
IntVector. Your new ADT files should be called
DoubleVector.h and DoubleVector.cpp! You can
e-mail me your work for assistance or for my
critique. This is optional.