CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory...

32
CS 240: Data CS 240: Data Structures Structures Thursday, June 7 Thursday, June 7 th th Programming Semantics, Programming Semantics, Software Life Cycle, Memory Software Life Cycle, Memory Allocation Allocation
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    213
  • download

    1

Transcript of CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory...

Page 1: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

CS 240: Data StructuresCS 240: Data Structures

Thursday, June 7Thursday, June 7thth

Programming Semantics, Software Life Programming Semantics, Software Life Cycle, Memory AllocationCycle, Memory Allocation

Page 2: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

DecompositionDecomposition

When you create a decomposition (and When you create a decomposition (and particularly when you create a particularly when you create a specification) it should be readable to the specification) it should be readable to the point that if someone else was looking at it point that if someone else was looking at it they could code it.they could code it.

Page 3: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

ConcernsConcerns

Using classes: We will start discussing this Using classes: We will start discussing this today and using them in the next lab and today and using them in the next lab and successive classes.successive classes.

There is no class on Tuesday, June 12There is no class on Tuesday, June 12thth. I . I will not have office hours on that day will not have office hours on that day either.either.

Exam: We will start talking about this on Exam: We will start talking about this on the 19the 19thth..

Page 4: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

ConcernsConcerns

Functions:Functions:We use these to solve our problemsWe use these to solve our problemsFunctions are created from our decomposition Functions are created from our decomposition

and specifications.and specifications.Functions have an input and an outputFunctions have an input and an output

These inputs and outputs may be constrainedThese inputs and outputs may be constrained What goes into a blender? What comes out?What goes into a blender? What comes out? What are the inputs into makepizza(…)?What are the inputs into makepizza(…)?

There may also be side effects:There may also be side effects: Blenders use electricityBlenders use electricity makepizza(…) destroys my supplies!makepizza(…) destroys my supplies!

Page 5: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

ConcernsConcerns

File Layout:File Layout: A program may consist of many, many files.A program may consist of many, many files.

.cpp, .h, .hpp, .rc, .ico, .jpg, .wav, .dll and so on..cpp, .h, .hpp, .rc, .ico, .jpg, .wav, .dll and so on. These represent the code as well as other resources These represent the code as well as other resources

needed by the programneeded by the program Our programs will be more limited in scope:Our programs will be more limited in scope:

Our main() will have its own .cpp file.Our main() will have its own .cpp file. This .cpp file will use #include to put together a larger projectThis .cpp file will use #include to put together a larger project

Each other file is broken up into two parts:Each other file is broken up into two parts: .h file for the function prototypes and #ifndef code.h file for the function prototypes and #ifndef code .cpp file the function definitions.cpp file the function definitions

Page 6: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

File Layout ExampleFile Layout Example

blackjack.cppblackjack.cpp Contains our main()Contains our main() Contains:Contains:

#include”card.h”#include”card.h” #include”deck.h”#include”deck.h” #include”player.h”#include”player.h” #include”game.h”#include”game.h”

The following files The following files organize our code:organize our code: Card -> card.h and Card -> card.h and

card.cppcard.cpp Deck -> deck.h and Deck -> deck.h and

deck.cppdeck.cpp Player -> player.h and Player -> player.h and

player.cppplayer.cpp Game -> game.h and Game -> game.h and

game.cppgame.cpp

Page 7: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

More on pointersMore on pointers

Pointers allow us to give data to functionsPointers allow us to give data to functionsThey also allow us to obtain additional They also allow us to obtain additional

memory resources while a program is memory resources while a program is running.running.

Page 8: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

Passing data:Passing data: Value model:Value model:

A copy of the data is given.A copy of the data is given. This is like burning a copy of a CD to give to a friend because This is like burning a copy of a CD to give to a friend because

they don’t have clean hands.they don’t have clean hands. This is the model that Java uses.This is the model that Java uses.

int myfunction(int first, int last, double appliedvalue, float int myfunction(int first, int last, double appliedvalue, float secondvalue);secondvalue);

Page 9: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

Passing data:Passing data:

Reference model:Reference model:This model allows us to share/give data to This model allows us to share/give data to

a function.a function.

Page 10: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

Reference passingReference passing

#include<iostream>#include<iostream>#include”swap.h”#include”swap.h”using namespace std;using namespace std;

int main()int main(){{

int number = 5;int number = 5;int secondnum = 10;int secondnum = 10;swap(&number,&secondnum);swap(&number,&secondnum);cout << number << endl;cout << number << endl;cout << secondnum << endl;cout << secondnum << endl;return 0;return 0;

}}

Output:Output:

101055

Page 11: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

Reference passingReference passing swap.hswap.h#ifndef SWAP_H#ifndef SWAP_H#define SWAP_H#define SWAP_H

//Swaps the values pointed to by //Swaps the values pointed to by the parameters.the parameters.

//input: two int pointers//input: two int pointers//output: none//output: none//side effects: the values pointed //side effects: the values pointed

to by the two parameters are to by the two parameters are swapped.swapped.

void swap(int *a, int *b);void swap(int *a, int *b);

#endif#endif

swap.cppswap.cpp#include”swap.h”#include”swap.h”

void swap(int *a, int *b)void swap(int *a, int *b){{

int temp = *a;int temp = *a;*a = *b;*a = *b;*b = temp;*b = temp;

}}

Page 12: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

Using arraysUsing arrays

Arrays are a representation of multiple Arrays are a representation of multiple data of the same type:data of the same type:

Either: int numbers[1000]; orEither: int numbers[1000]; or int number1,number2,number3…..int number1,number2,number3…..

An array is not just a cleaner scheme but An array is not just a cleaner scheme but much more modifiable.much more modifiable.

Also, easier to access (try setting each of Also, easier to access (try setting each of the numbers to 15).the numbers to 15).

Page 13: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

ArraysArrays

An array is implicitly a pointer (a static An array is implicitly a pointer (a static pointer).pointer).

Because it is static, we do not use it as we Because it is static, we do not use it as we do with pointers.do with pointers.

Generally, we use dynamic arrays which Generally, we use dynamic arrays which are created differently.are created differently.

Page 14: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

Dynamic ArraysDynamic Arrays

#include<iostream>#include<iostream>

using namespace std;using namespace std;

int main()int main(){{

int *numbers;int *numbers;int userinput;int userinput;cin >> userinput;cin >> userinput;numbers = new int[userinput];numbers = new int[userinput];cout << “Array created of size: “ << userinput <<endl;cout << “Array created of size: “ << userinput <<endl;delete [] numbers;delete [] numbers;

}}

Page 15: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

Dynamic ArraysDynamic Arrays

If the array the user asked for isn’t large If the array the user asked for isn’t large enough?enough?

Lets say the user inputs: 10Lets say the user inputs: 10And we change the code to take in int And we change the code to take in int

inputs until the user inputs -1.inputs until the user inputs -1.

Page 16: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

Dynamic ArraysDynamic Arrays#include<iostream>#include<iostream>

using namespace std;using namespace std;

int main()int main(){{

int *numbers;int *numbers;int userinput;int userinput;cin >> userinput;cin >> userinput;numbers = new int[userinput];numbers = new int[userinput];cout << “Array created of size: “ << userinput <<endl;cout << “Array created of size: “ << userinput <<endl;int location = 0;int location = 0;userinput = 0;userinput = 0;while(userinput != -1)while(userinput != -1){{

cin >> userinput;cin >> userinput;numbers[location] = userinput;numbers[location] = userinput;location++;location++;

}}delete [] numbers;delete [] numbers;

}}

Page 17: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

Dynamic ArraysDynamic Arrays

When the user inputs the 11When the user inputs the 11thth value, it value, it uses memory that isn’t ours. uses memory that isn’t ours.

It may not cause an error right away.It may not cause an error right away. In fact, on larger programs, we might just In fact, on larger programs, we might just

overwrite our own data for awhile.overwrite our own data for awhile.Dynamic arrays allows us to resize the Dynamic arrays allows us to resize the

array if we need to.array if we need to.

Page 18: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

#include<iostream>#include<iostream>

using namespace std;using namespace std;

int main()int main(){{

int *numbers;int *numbers;int userinput;int userinput;cin >> userinput;cin >> userinput;numbers = new int[userinput];numbers = new int[userinput];cout << “Array created of size: “ << userinput <<endl;cout << “Array created of size: “ << userinput <<endl;int location = 0;int location = 0;int maxsize = userinput;int maxsize = userinput;userinput = 0;userinput = 0;while(userinput != -1)while(userinput != -1){{

cin >> userinput;cin >> userinput;numbers[location] = userinput;numbers[location] = userinput;location++;location++;if(location>=maxsize)if(location>=maxsize){{

……....}}

}}delete [] numbers;delete [] numbers;

}}

int *temp = new int[maxsize*2];for(int i=0;i<maxsize;i++){

temp[i]=numbers[i];}maxsize *= 2;delete [] numbers;numbers = temp;

Page 19: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

Abstract Data TypesAbstract Data Types An abstract data type (ADT) is a collection of An abstract data type (ADT) is a collection of

data.data. It provides a representation for some more It provides a representation for some more

abstract object.abstract object.class computerclass computer{{

string name,processor_type,brand,video_card;string name,processor_type,brand,video_card;int processor_speed,ram_speed,ram_amount;int processor_speed,ram_speed,ram_amount;……..

};};

Page 20: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

ADTsADTs

This allows you to use the ADT instead of This allows you to use the ADT instead of defining each field in your codedefining each field in your code

Page 21: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

ADTsADTs

#include<iostream>#include<iostream>#include”computer.h”#include”computer.h”

using namespace std;using namespace std;

int main()int main(){{

computer mycomputer;computer mycomputer;mycomputer.ram_amount = 1024;mycomputer.ram_amount = 1024;computer *mycomputer_ptr = new computer;computer *mycomputer_ptr = new computer;mycomputer_ptr->ram_amount = 1024;mycomputer_ptr->ram_amount = 1024;cout << mycomputer << endl;cout << mycomputer << endl; //This doesn’t work!//This doesn’t work!return 0;return 0;

}}

Page 22: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

ADTsADTs

You could print out each You could print out each field/value/variable within computer.field/value/variable within computer.

However, that would be excessive.However, that would be excessive.Also, what if you want to change what you Also, what if you want to change what you

decided to print? You’d have to change it decided to print? You’d have to change it in every location.in every location.

computer can be given functions/methods computer can be given functions/methods that generate a printable output.that generate a printable output.

Page 23: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

ADTs - OutputADTs - Output Option 1:Option 1:

Create a function that uses cout directly:Create a function that uses cout directly: This is not preferred, it limits what can be done with the This is not preferred, it limits what can be done with the

output.output. Option 2:Option 2:

Overload “<<“, that way we can use cout on computer.Overload “<<“, that way we can use cout on computer. This is easier to use, but still limits what can be done This is easier to use, but still limits what can be done

with the output.with the output. Option 3:Option 3:

Create a function that returns a string of what we want Create a function that returns a string of what we want to output.to output.

We will use options 2 and 3 in this classWe will use options 2 and 3 in this class

Page 24: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

computer.hcomputer.h

#ifndef COMPUTER_H#ifndef COMPUTER_H#define COMPUTER_H#define COMPUTER_H

class computerclass computer{{

string name,processor_type,brand,video_card;string name,processor_type,brand,video_card;int processor_speed,ram_speed,ram_amount;int processor_speed,ram_speed,ram_amount;public:public:

string getoutput();string getoutput();};};

#endif#endif

Page 25: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

computer.cppcomputer.cpp

#include”computer.h”#include”computer.h”

string computer::getoutput()string computer::getoutput(){{

string retval = “”;string retval = “”;retval = “Computer: “ + name;retval = “Computer: “ + name;return retval;return retval;

}}

Page 26: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

Other ADT benefitsOther ADT benefits

An ADT allows us to group data together. An ADT allows us to group data together. This way everything we want to This way everything we want to manipulate is close together.manipulate is close together.

The variables/fields declared in the class The variables/fields declared in the class prototype are “global” to all the class prototype are “global” to all the class functions/methods.functions/methods.

Page 27: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

ADT constraintsADT constraints

Generally, you don’t give a user access to Generally, you don’t give a user access to your variables/fields.your variables/fields.

They could mess things up, so, mutators They could mess things up, so, mutators and accessors are created.and accessors are created.

Mutators mutate/change data.Mutators mutate/change data.Accessors access data.Accessors access data.ADTs may require constructors and ADTs may require constructors and

destructorsdestructors

Page 28: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

int main()int main(){{

computer mycomputer;computer mycomputer;mycomputer.set_ram_amount(1024);mycomputer.set_ram_amount(1024);computer *mycomputer_ptr = new computer;computer *mycomputer_ptr = new computer;mycomputer_ptr->set_ram_amount(1024);mycomputer_ptr->set_ram_amount(1024);cout << mycomputer.getoutput() << endl;cout << mycomputer.getoutput() << endl;cout << mycomputer.get_ram_amount() <<endl;cout << mycomputer.get_ram_amount() <<endl;return 0;return 0;

}}

Page 29: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

Constructors/DestructorsConstructors/Destructors

Constructors:Constructors:Allow you to provide initial valuesAllow you to provide initial valuesThese are needed for dynamic memory within These are needed for dynamic memory within

an ADTan ADTDestructors:Destructors:

If you have any dynamic memory in the ADT, If you have any dynamic memory in the ADT, this is where you deallocate it.this is where you deallocate it.

Page 30: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

computer.hcomputer.h#ifndef COMPUTER_H#ifndef COMPUTER_H#define COMPUTER_H#define COMPUTER_H

class computerclass computer{{

string name,processor_type,brand,video_card;string name,processor_type,brand,video_card;int processor_speed,ram_speed,ram_amount;int processor_speed,ram_speed,ram_amount;int *somedynamicmemory;int *somedynamicmemory;public:public:

string getoutput();string getoutput(); //provides output//provides outputcomputer();computer(); //constructor//constructor~computer();~computer(); //destructor//destructor

};};

#endif#endif

Page 31: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

computer.cppcomputer.cpp#include”computer.h”#include”computer.h”

string computer::getoutput()string computer::getoutput(){{

string retval = “”;string retval = “”;retval = “Computer: “ + name;retval = “Computer: “ + name;return retval;return retval;

}}

computer::computer()computer::computer(){{

somedynamicmemory = new int[100];somedynamicmemory = new int[100];brand = “No name brand, inc.”;brand = “No name brand, inc.”;processor_speed = 0;processor_speed = 0;

}}

computer::~computer()computer::~computer(){{

delete [] somedynamicmemory;delete [] somedynamicmemory;}}

Page 32: CS 240: Data Structures Thursday, June 7 th Programming Semantics, Software Life Cycle, Memory Allocation.

Class ProjectClass Project

We will go over the implementation of a card We will go over the implementation of a card game as a class.game as a class.

We will implement either:We will implement either: Triple Triad, Triple Triad, http://http://

en.wikipedia.org/wiki/Triple_Triad#Triple_Triaden.wikipedia.org/wiki/Triple_Triad#Triple_Triad Tetra Master Tetra Master

http://en.wikipedia.org/wiki/Triple_Triad#Tetra_Masterhttp://en.wikipedia.org/wiki/Triple_Triad#Tetra_Master There are some common features of each which There are some common features of each which

we can work on before we make a decision of we can work on before we make a decision of which game to implement.which game to implement.

We will take a vote on it next Thursday.We will take a vote on it next Thursday.