Re: Assignment Help

  • From: "black ares" <matematicianu2003@xxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Tue, 11 Aug 2009 08:09:17 +0300

another thing
5. iam not so sure, but char item means a single character.
So if you are trying to put there a whole sentence it will fail.
you can use for a string representation or char item[100];
which means 100 chars or a char* item; which means a pointer to char data;

----- Original Message ----- From: "Marvin Hunkin" <startrekcafe@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Tuesday, August 11, 2009 7:24 AM
Subject: Assignment Help


hi.
doing an assignment about using cohesion and cuppling for functions.
now for some stupid reason.
here's how the program works.
you get a welcome screen with instructions on how to use the program.
you enter a item description.
then you enter an amount.
if the amount is $10 or greater it adds 10% tax to the amount of the item.
you also have a while loop to enter 0 to exit the program.
then you get the total amount spent.
now when i compile the program.
i get the welcome message.
now when i enter say bread.
i am using jaws 10.0.154u with windows vista sp2, and using g++ from
http://www.cygwin.org
jaws crashes and get the windows blue screen of death.
can any one help me out.
tried writing this a few other versions with file names.
but still get the same answer.
hard to visualise functions and perameters passing values, and then have it
cuppled and cohesion.
not an easy concept to grasp.
for a totally blind person learning c++.
did take some classes a few years ago, but forgot most of what i had
learned.
and stupid me did not back up.
actually did back up, but the cd died with all previous examples.
so that's gone.
so if any one can help me out.
will post the code and the session web page information.
so you can see how the instructor expects me what to do.
and asking for your help.
as my instructor is the only programming instructor for the state of
tasmania in the education in higher education and tertiary education.
so overworked, over stressed.
face to face students, possibly in multiple locations as well as online.
so maybe over 100 students.
and his computer system crashed the other day.
and not sure if he had backups.
if so, it could take a while.
so banging my head against a brick wall.
giving me a migrain.
and tried looking on the net for similar or same examples.
either way too complex or actual software for businesses.
so if any one can help me out.
maybe just trying too hard and maybe just a total idiot after all when it
comes to programming.
okay enough of the request.
if any one can help .
either post on list or off list.
not sure if atatchments are allowed on list.
cannot remember.
brain cells must be getting old in this 44 year old body.
from devonport, tasmania, australia.
cheers Marvin.

Code goes here for assignment:

/*
Program: Cash Register.
File:  Cash.cpp
Description: Cash Register Program.
Author:  Marvin Hunkin.
Revision: 1.00 05/08/2009 First release.
*/

// iostream is needed for the cout statement.
#include <iostream>
using namespace std;

//declare function declaration.
void input();
void tax();
void total();

int main()
{

//Call functions in main.
input();
tax();
total();
return 0;
} //close main function

//The input function goes here.
void input()
{
//Declare variables.
//this variable will hold the item being entered.
char item;
//this variable will hold the amount being entered.
float amount=0.0;
cout << "Welcome to Marvin's Cash Register Program\n";
cout << "To use this program please follow the instruction below:\n";
cout << "You enter a description from the keyboard.\n";
cout << "You then enter a amount for the item from the keyboard.\n";
 //Prompt for entering a item goes here.
cout << "Enter a item description and then press enter";
cin>> item;
//the prompt for entering an amount goes here.
cout << "Enter an amount and then press enter";
cin >> amount;
//While loop goes here.
while (amount!='0')
{
//Prompt for entering a item goes here.
cout << "Enter a item description and then press enter";
cin>> item;
//the prompt for entering an amount goes here.
cout << "Enter an amount and then press enter";
cin >> amount;
} // close while loop
} //close input() function

//the tax function goes here.
void tax()
{
//Declare variables.
//this variable will hold the tax amount.
float tax = 0.1;
//this variable will hold the total.
float total = 0.0;
//this variable will hold the amount.
float amount = 0.0;
//if block goes here.
if (amount >=10)
{
tax = total * tax;
  } //close if block
} //close tax function

//total function goes here.
void total()
{

//output goes here.
cout << "total $ " << total << endl;
} //close total function

Session 10 internet document begins here:


--------------------------------------------------------------------------------

Session 10
Cohesion and Coupling

--------------------------------------------------------------------------------

Objectives
The purpose of this session is to introduce you to the concept of cohesion
and coupling and what these concepts mean when writing a C++ program.



--------------------------------------------------------------------------------

Last session we looked at how to write a function. This session we are going
to look at how to make better functions. There are a number of objectives
when deciding what to put into a function and what not to put into a
function. Good function design states that functions should be loosely
coupled and be tightly cohesive.

Cohesion
Cohesion is the measure by which the statements within a function relate to each other. The statements in a function should only perform one task. Most
of the programs that we have written so far have only had one function and
that was the main function. In our examples so far, the main function has
displayed the title information, gathered user input, processed data and
displayed the output. To write good programs, each of our functions should
only perform one task each.

To demonstrate the concept lets use a practical example. We are going to
write a program that keeps track of a persons bank account. With a bank
account we do a number of tasks. We withdraw money, deposit and make a
balance enquiry. The following code does the job but suffers from a lack of
cohesion.


/*
* Program: Cohesion and Coupling example - 1.
* File:  cohesCoup1.cpp
* Description: This program is an exercise to demonstrate cohesion in a
program.
* Author:  Henry Bush
* Revision: 1.00 2/12/2003 First release.
*/

// iostream is needed for the cout statement.
#include <iostream>using namespace std;

int main()
{
float balance = 0.0;
float amount = 0.0;
char option;

cout << "Welcome to the Bank Account Manager\n\n";
cout << "Enter the account balance: $";
cin >> balance;

cout << "w = withdrawal, d = deposit, b = account balance, x = exit
program" << endl;
cout << "Type in one of the choices from the list above ";
cin >> option;
while(option != 'x')
{
 switch(option)
 {
  case 'b':
   cout << "\nThe account balance is: $" << balance << endl;
   break;

  case 'd':
   cout << "Type in the amount that is being deposited: $";
   cin >> amount;
   balance += amount;
   break;

  case 'w':
   cout << "Type in the amount that is being withdrawn: $";
   cin >> amount;
   balance -= amount;
   break;

  case 'x':
   exit(0);
   break;

  default :
   cout > option<< " is not a valid value try again  ";
   break;
 } // close switch block

 cout << "\nType in one of the choices (w, b, d, x) ";
 cin >> option;

}  // close while loop
}   // close main function


Lets look at the program and see how we can rewrite the program in a much
better manner. The first thing to do is to analyse the tasks that are
performed in the program. Obviously, there are the balance, deposit and
withdrawal tasks, but if we look closer at the program we can see that there
is some beginning programing stuff and a choice task. So if we add these
tasks up we then have 5 tasks and we can write the program with 5 functions. We can start off by writing the blank functions and declaring them as in the
following


/*
* Program: Cohesion and Coupling example - 1.
* File:  cohesCoup1.cpp
* Description: This program is an exercise to demonstrate cohesion in a
program.
* Author:  Henry Bush
* Revision: 1.00 2/12/2003 First release.
*/

// iostream is needed for the cout statement.
#include <iostream>using namespace std;
// Declare the functions.
void initialise();
void deposit();
void withdrawal();
void accountbalance();
void input();

int main()
{
} // close main function

void initialise()
{

} // close initialise function

void deposit()
{
} // close deposit function

void withdrawal()
{
} // close withdrawal function

void accountbalance()
{
} // close balance function

void input()
{
} // close input function


At this stage there are no parameters and the return type is void. Lets
start with adding the variables that we need for this program. They were
float balance, float amount and char option, we will add them above the main
function. The reason for this will be explained later this session.

The initialise function is where we are going to display the title, get user
input for the account balance and prime the while loop. Yes, I can already
here you saying that is three things, but for this example it will be OK to call all of this initialising the program. The function should look like the
following


void initialise()
{
cout << "Welcome to the Bank Account Manager\n\n";
cout << "Enter the account balance: $";
cin >> balance;
cout << "w = withdrawal, d = deposit, b = account balance, x = exit
program" << endl;
cout << "Type in one of the choices from the list above ";
cin >> option;
} // close initialise function


The Deposit function is where we make the deposits so add the code to make
it look like this


void deposit()
{
cout << "Type in the amount that is being deposited: $";
cin >> amount;
balance += amount;
} // close deposit function


And the withdrawal function will have code like this


void withdrawal()
{
cout << "Type in the amount that is being withdrawn: $";
cin >> amount;
balance -= amount;
} // close withdrawal function


The accountbalance function will display the account balance so it will look
like the following


void accountbalance()
{
cout << "\nThe account balance is: $" << balance << endl;
} // close balance function


The input function is where we will input what the users next choice is like
this


void input()
{
cout << "\nType in one of the choices (w, b, d, x) ";
cin >> option;
} // close input function


At this point we only have the main function left and this is where the most noticeable feature is. The main function now only controls the operation of
the program and lets all of the other functions do the detailed work. The
program will start off with some initialisation and then into a while loop
we will put a switch statement and a call to the input function. Modify your
main function so that it looks like the following


int main()
{
initialise();
while(option!='x')
{
 switch(option)
 {
  case 'b':
   accountbalance();
   break;

  case 'd':
   deposit();
   break;

  case 'w':
   withdrawal();
   break;

  case 'x':
   exit(0);
   break;

  default :
   cout << option<< " is not a valid value try again  ";
   break;
 } // close switch block
 input();
}  // close while loop
}   // close main function


One of the big things to note is that main has now become much smaller and
easier to read. Programs that are easy to read are also easier to maintain.
The program should now look like the following


/*
* Program: Cohesion and Coupling example - 1.
* File:  cohesCoup1.cpp
* Description: This program is an exercise to demonstrate cohesion in a
program.
* Author:  Henry Bush
* Revision: 1.00 2/12/2003 First release.
*/

// iostream is needed for the cout statement.
#include <iostream>using namespace std;
// Declare the functions.
void initialise();
void deposit();
void withdrawal();
void accountbalance();
void input();

float balance = 0.0;
float amount = 0.0;
char option;

int main()
{
initialise();
while(option!='x')
{
 switch(option)
 {
  case 'b':
   accountbalance();
   break;

  case 'd':
   deposit();
   break;

  case 'w':
   withdrawal();
   break;

  case 'x':
   exit(0);
   break;

  default :
   cout << option<< " is not a valid value try again  ";
   break;
 } // close switch block
 input();
}  // close while loop
}   // close main function

void initialise()
{
cout << "Welcome to the Bank Account Manager\n\n";
cout << "Enter the account balance: $";
cin >> balance;
cout << "w = withdrawal, d = deposit, b = account balance, x = exit
program" << endl;
cout << "Type in one of the choices from the list above ";
cin >> option;
} // close initialise function

void deposit()
{
cout << "Type in the amount that is being deposited: $";
cin >> amount;
balance += amount;
} // close deposit function

void withdrawal()
{
cout << "Type in the amount that is being withdrawn: $";
cin >> amount;
balance -= amount;
} // close withdrawal function

void accountbalance()
{
cout << "\nThe account balance is: $" << balance << endl;
} // close balance function

void input()
{
cout << "\nType in one of the choices (w, b, d, x) ";
cin >> option;
} // close input function


It is made of a number of small functions that only perform one task each.
This technique makes for programs that are easy to handle as they get
larger. With programs of this size it is hard to justify in your own mind
why you would bother, after all we have nearly doubled the number of lines
of code and have only created the same program. Once your programs reach
hundreds of lines of code you will see the benefits of this type of
technique.

Recapping when you write functions try to put into one function only the
code that performs one task. Sometimes you may need to put statements into a
function that perform more than one task. In these cases try to put tasks
that relate to each other. Another thing to try for is to have your
functions small. Try and have then less than one screen full of text. You
will not always be able to achieve these guides, but if you work towards
them, then you are on the right track.

Coupling.
Where cohesion relates to the function's internal behavior, coupling relates to how it interacts with the program that it exists in. Functions should be
loosely coupled. By that I mean that a function should not rely on
statements or variables from outside the function.

A well written function should be able to be copied into another program and
work without any changes to the function or the program. If we look at the
example that we have been working on, we can see that each function depends
on the variables that we declared at the beginning of the program. If we
took one of the functions out of this program and placed it into another, we would have to modify the function or the program to make them work together.
We are going to modify the program again to make the functions loosely
coupled.

Firstly we will move the variable declarations back into the main function.
When they were placed before the main function made them global variables.
Global variables are accessible to all the functions in a program. This is
handy but very dangerous. It is hard to see at this size of program how it
can be a problem. The more parts of a program that can gain access to a
variable, the greater the chance of being altered without realising the
consequences.

To avoid this we use a technique to control the access to variables. By
placing the variable declaration inside a function makes them what are
called local variables. That means that they are only accessible from within
the function. By doing so makes the programs more robust, but it does have
the down side of making the programmer have to think more about the way they
write their programs. The only way to get data into a function is through
the parameter list, and the only way to get data out of a function is
through the return type.

If we look at the initialise function first. We do not need to pass any
variables to the function but we do wish to return the balance entered. So
we make the return type a float. When a function returns a value, the
calling function must accept the value, as in the following



balance = initialise();


Inside this function though we also had the user enter the choice for the
next function. We will move this back to the main function. The deposit
function needs the balance value and we need to return the new balance. So
we need to pass the balance value to the function as a parameter. Within the
function we declare a new variable to hold the amount deposited. After
performing the calculation we need to return the new balance as in the
following


float deposit(float value)
{
float amount = 0.0;
cout << "Type in the amount that is being deposited: $";
cin >> amount;
value += amount;
return value;
} // close deposit function


The withdrawal function will be almost identical to the deposit function


float withdrawal(float value)
{
float amount = 0.0;
cout << "Type in the amount that is being withdrawn: $";
cin >> amount;
value -= amount;
return value;
} // close withdrawal function


The account balance function requires the balance value but does not need to
return anything as in the following


void accountbalance(float value)
{
cout << "\nThe account balance is: $" << value << endl;
} // close balance function


The last function is the input, this function will return a value to the
option variable and does not have to take any parameters.


char input()
{
char inputValue;
cout << "\nType in one of the choices (w, b, d, x) ";
cin >> inputValue;
return inputValue;
} // close input function


Put all together the program will look like this


/*
* Program: Cohesion and Coupling example - 1.
* File:  cohesCoup1.cpp
* Description: This program is an exercise to demonstrate cohesion in a
program.
* Author:  Henry Bush
* Revision: 1.00 2/12/2003 First release.
*/

// iostream is needed for the cout statement.
#include <iostream>using namespace std;
// Declare the functions.
float initialise();
float deposit(float value);
float withdrawal(float value);
void accountbalance(float value);
char input();


int main()
{
float balance = 0.0;

char option;

balance = initialise();
cout << "w = withdrawal, d = deposit, b = account balance, x = exit
program" << endl;
cout << "Type in one of the choices from the list above ";
cin >> option;
while(option!='x')
{
 switch(option)
 {
  case 'b':
   accountbalance(balance);
   break;

  case 'd':
   balance = deposit(balance);
   break;

  case 'w':
   balance = withdrawal(balance);
   break;

  case 'x':
   exit(0);
   break;

  default :
   cout << option<< " is not a valid value try again  ";
   break;
 } // close switch block
 option = input();
}  // close while loop
}   // close main function

float initialise()
{
float value = 0.0;
cout << "Welcome to the Bank Account Manager\n\n";
cout << "Enter the account balance: $";
cin >> value;
return value;
} // close initialise function

float deposit(float value)
{
float amount = 0.0;
cout << "Type in the amount that is being deposited: $";
cin >> amount;
value += amount;
return value;
} // close deposit function

float withdrawal(float value)
{
float amount = 0.0;
cout << "Type in the amount that is being withdrawn: $";
cin >> amount;
value -= amount;
return value;
} // close withdrawal function

void accountbalance(float value)
{
cout << "\nThe account balance is: $" << value << endl;
} // close balance function

char input()
{
char inputValue;
cout << "\nType in one of the choices (w, b, d, x) ";
cin >> inputValue;
return inputValue;
} // close input function


Make sure that you can understand what is happening here in the program.
Cohesion and coupling are important concepts to creating larger workable
programs.


--------------------------------------------------------------------------------
Session 10 Practical Exercise
The practical exercise for this session will be to create a cash register
program that you input the cost of an item, the number purchased and if a
sales tax applies to the item. If a tax does apply to the item, the tax
value will be ten percent of the items cost.



Example

Item Cost Tax Applies? Cost Plus Tax
Bread $2.00 No $2.00
Shirt $20 Yes $22.00
Total $24.00


In the example, the bread costs $2, it does not have sales tax on it, so its final price is $2. The shirt on the other hand, has a cost of $20, and does
have tax on it, so ten percent of 20 is 2, making the final price of the
shirt $22.00. The program will then calculate the cost of all the items.
This will be in a loop so that a number of different items can be inputed
and when finished the program it will produce a grand total.

If you can complete this exercise without any difficulties then you can
proceed to the next lesson. If you found that you had any problems with this
exercise then contact your instructor about the problems and attach any
working code so that we can discuss where your solutions lay.

After you successfully complete this exercise, remember to send your
completed code into your instructor for assessment.

--------------------------------------------------------------------------------


E-Mail: startrekcafe@xxxxxxxxx
Msn: startrekcafe@xxxxxxx
Skype: startrekcafe
Visit my Jaws Australia Group at http://groups.yahoo.com/groups/JawsOz/


__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: