COS120lec2_SDMAlgorithms1

advertisement
116094243
Page 1 of 4
COS120 Software Development Using C++ – Lecture 2
AUBG, COS dept
Lecture 2
Computers, Problem Solving and Programming (continued).
Applying the software development method. Introduction to linear
algorithms and branch algorithms. Typical examples.
H&K, Sec 1.5
F&K, Sec 1.6
Software Development Method, Software Life Cycle, Program Development Cycle






Specify the problem requirements;
Analyze the problem;
Design the algorithm to solve the
problem;
Implement the algorithm;
Test and verify the completed
program;
Maintain and update the program.

Systems Analysis;

Systems Design;

Systems
Implementation;

Systems Support.






Analyze;
Design;
Choose interface;
Code;
Test & debug;
Complete
documentation.
Three “notations” used to document algorithms: Flowchart, Pseudo code, Hierarchy chart
Attention: Analyze the problem and design the algorithm: It is helpful to underline phrases
in the problem statement that identify input and output. See the example below:
Problem: Compute and display the total cost of apples (output) given the number of
kilos of apples (input1) purchased and the cost per kilogram of apples (input2).
Problem input:
Problem output:
 quantity of apples purchased (in kg)
 cost per kilo of apples (in $ or in leva)
 total cost of apples (in $ or in leva)
After specifying inputs and outputs, develop list of formulas, giving relationship between
input and output.
Abstract form
Total cost = Unit cost * Number of units
Detailed, concrete form
Total cost of apples = Cost per kg * Kilos of apples
Applying SDM for solving problems based on linear algorithms and branch algorithms:
Typical linear algorithms:
Typical branch algorithms:
 Converting miles to kilometers;
 Root (solution) of a linear equation
bx + c = 0 (single solution, infinite
 Converting meters to feet and inches;
number of solutions, no solution);
 To ultiply and divide two numeric
 Roots (solution) of a quadratic
values.
equation ax2 + bx + c = 0 (two distinct
solutions, two identical solutions, no
real solutions).
See Source text of a sample C program
on back >
116094243
Page 2 of 4
C++ program using object oriented stream I/O
// MilesToKms1.cpp
// Algorithm converting distance in miles to kilometers
#include <iostream>
using namespace std;
void main()
{
double miles;
double kms;
// input
// output
// step 1 - get the distance (input)
cout << “\n Enter distance in miles:”;
cin >> miles;
// step 2 – convert the distance from miles to kilometers
kms = 1.609 * miles;
// step 3 - display the distance in kilometers (output)
cout << “\n\n
That equals “ << kms << “ kilometers\n";
}
// exit the main function
116094243
Page 3 of 4
Same C++ program using run-time I/O functions
// MilesToKms2.cpp
// Algorithm converting distance in miles to kilometers
#include <stdio.h>
int main()
{
double miles;
double kms;
// input
// output
// step 1 - get the distance (input)
printf("\n Enter distance in miles:");
scanf("%lf",&miles);
// step 2 - conversion miles to kilometers
kms = 1.609 * miles;
// step 3 - display the distance in kilometers (output)
printf("\n\n
That equals %lf kilometers\n",kms);
return 0;
}
// exit the main function
116094243
Page 4 of 4
Same C++ program as managed .NET application
// MilesToKms3.cpp
// Algorithm converting distance in miles to kilometers
#include "stdafx.h"
using namespace System;
int main()
{
double miles;
double kms;
String^ line;
// input
// output
// step 1 - get the distance (input)
Console::Write("\n Enter distance in miles:");
line = Console::ReadLine();
miles = Convert::ToDouble(line);
// step 2 - conversion miles to kilometers
kms = 1.609 * miles;
// step 3 - display the distance in kilometers (output)
Console::WriteLine("\n\n
That equals {0} kilometers\n",kms);
return 0;
}
Download