By:
Abdullah Shoukat
ADMISSION # 040-31147
|
|
The following report covers my concept and interpretation of Unit 1 Programming (course autumn 2020).
CONTENTS
ALGORITHMS (P1) page 3
STEPS FROM CODING TO EXECUTION (M1) page 6
IMPLEMENTATION OF ALGORITHM (D1) page 8
Section 1: Algorithms:
P1) Algorithms:
Whenever a need for solving a problem arises, one forms up an idea or a plan to carry out his/her operation, the plan made is called an algorithm. Algorithm is basically the first step of making a program that defines how the problem given will be solved or choosing which path is to be taken to solve the problem easily.
Thus the proper definition off an algorithm can be:
“A detailed set of sequences made before writing a program is called an algorithm”- which may help the programmer directing his/her code.
An algorithm plays an important role in writing a program as the efficiency of a program is entirely dependent on how good the algorithm is.
Types of Algorithms:
There are many types of algorithms, some of them are:
1. Recursive Algorithm
2. Divide and Conquer Algorithm
3. Greedy Algorithm
4. Merge sort Algorithm
5. Bubble sort Algorithm
6. Backtracking Algorithm
7. Dynamic Programming Algorithm
However in this report I’ll be explaining the first three.
1.
Recursive Algorithm:
The word recursive means re-appearing. In recursive algorithms the function is executed again and again until the problem is solved. Recursive algorithms have a base case that means in certain conditions output maybe known to us, we may also know where the program will terminate and finally we may find the recursive call which will call or repeat the function (directly or indirectly) again and again.
For example
loops are a great example of recursive algorithms as they continue to execute themselves on a certain condition again and again until the problem is solved
Print number from 0-100
// Base case which is executed again and again until the solution is determined
2.
Divide and Conquer Algorithm
In divide and conquer algorithms the problem to be executed is broken down into smaller chunks so it may be easy to solve separately one by one, hence the name divide and conquer. Divide and conquer algorithms may prove very effective against higher and tougher problems where the program is divided into parts which doesn’t let the code to jumble up.
For example
in this program I had to execute all arithmetic operations with two numbers. Rather on typing the whole code at once under main(), I made separate functions for each operation. As a result I have a neat code for a calculator, which was also easy to write as I created a separate function for each operator.
3. Greedy Algorithms:
Greedy algorithms are the algorithms that solves a problem by making a most optimal choice at each stage with the hope of finding a global optimum. These algorithms are very easy to implement and have reasonable running complexity but however but the main drawback for this method is that there are very few cases where we end up with an optimum solution. In other words greedy algorithms. These algorithms are mostly applicable in making a program more effective
For example
if I want to give 70 Rs to the cashier but I only have note of 50 Rs , 20 Rs and 10 Rs, by a greedy algorithm firstly I’ll take out 50 the highest number then as we don’t need another 50 ill move on to the next highest number i.e. 20. Like this ill only handle two notes to the cashier, you can see I was choosing the most optimal solution on each step by choosing the highest most suitable number.
Outlining the process in building an application:
An application is made by keeping in view the following processes.
Definition and Requirement Analysis:
In this phase the purpose of the application being made is defined clearly. The problem to be solved is highlighted and a goal is set to achieve. This step also involves the defining of the resources that will be used to create the software, hence requirement analysis and gathering is also a part of this step.
Design:
this phase clearly defines how the purpose or goal of the program will be achieved. It is one of the most important steps and a software’s efficiency is completely dependent on this step. Basically an algorithms is made.
Development:
in this phase the algorithm made is implemented. The algorithm is written in a programming language to make the computer understand. In short code is generated and application is made.
Testing:
in this part the code generated in last step is tested for faults and errors are removed if there are any.
Implementation and Deployment:
After checking for errors the program is ready for use and is deployed where needed. The program is given to the end user and is installed in their systems.
Maintenance and upgrades:
after deployment further updates are checked that may create more ease for the customer and the program is upgraded with time to time to keep things up to date.
M1) Steps taken from writing a code to execution:
After the problem is defined and the requirements are taken note of we enter into the phase of writing an algorithm.
Writing an algorithm:
An algorithm is written while keeping the core of the problem in mind. The algorithm helps the coder to understand the logic behind the program and write the code knowing what to do by understanding the algorithm.
After the set of ideas are defined we can start developing the program. Following are the steps taken from writing a code to its execution
1.
Choosing the right language:
After you know how to solve your problem you should decide which programming language suits your problem, or which programming language will make it easier for you to execute your algorithm. Every programming language has its pro and cons,
for example
if you want to create a major software package or an operating system you might want to use C++ as majority of these types of programs are made in C++. Likewise JAVA is very common language in creating games. It is also dependent on the developer as which language is he an expertise of or on which language he has a good grip. Furthermore it is also dependent on the skill set of the developer that whether he is an experienced person or new in the field, if he is new it might be helpful for him to code in GW BASIC as it is a language for beginners and youngsters in this field.
2.
Using the right program to code:
Programming can sometimes be a tiring and a difficult job where the developer may find very difficult to even find a single bug. Luckily there are programs of a language also known as Integrated Development Environment (IDE) or code editors. If one can find the right code editor before writing a program it may be a great ease not only in de bugging but also it may have all the libraries while running the program.
Here you can clearly see that Visual studio 2013 clearly highlights the syntax error when I didn’t typed “;” after the statement.
3.
Stating Variables and Libraries:
Header files or libraries include the required instruction that help the system to read and execute the code. They are called before writing a code. Like in C++ #include lets the program to take and show input and output. Similarly it is wise to declare variables and their datatypes early in the program.
4.
Coding:
This step is the most important step in creating a program, it may also be the most difficult. Here programmers needs to code his algorithm into machine language in which the computer can understand the commands and execute. This step requires knowledge and syntax of the programming language. Then coders, keeping in track your algorithm write the code
Syntax
are the set of grammatical rules in a programming language, in other words a format which the compiler can understand.
Then run the code, if it runs successfully jump to step 6, if not check for bugs.
5.
Testing and Debugging
It is quiet common even for a good programmer to face bugs and errors after coding the program. Errors are the mistakes in the code of the program that cause a malfunction in execution of the program. The malfunction can be for example that the compiler can’t read the code in cases of
syntax errors. Syntax errors are cause by a grammatical mistake in the code which makes it difficult for the compiler to understand.
For example
In C++ every statement is ended by a “;” which tells the compiler that this command ends here. Another type of errors are the
runtime errors
in which errors occur while executing the program.
For example
inputting a decimal figure in an integer type variable. These types of errors occur when the environment given to program is not right or in other words like not giving a header file for execution.
The following image is the error bar I faced while having a syntax error in Microsoft Visual C++ 2013
6.
Implementation and Deployment
Implementation stage involves all the set of activities that you do to install the program and available to use on the user end. When the program is ready and created it is then passed on the customer for whom it was created. In some cases the user are trained to operate the program by the developers as they will be handing over the system to them. This step also involves advertising or marketing in some cases so the software is known of its abilities.
7.
Maintenance and Upgrades
Software maintenance is an ongoing process where further enhancements are made when needed or the software is kept up to date and optimized when the time passes by. Maintenance can also be repairing and modification of the software.
D1) Examination of Implementation of an Algorithm in C++
We know now what an algorithm is and what is coding, but in a practical view both need to be correlated as programming or coding is the most important step of creating a software and it isn’t possible without creating an algorithm. That algorithm defines the method of solving the problem which will eventually give us with a structure in mind to code.
This part of the report explains the implementation of algorithms
Scenario:
I am given a task to make a program that calculates 2 numbers for users, users may decide which arithmetic operation they want to perform in the calculation.
Seeing the problem I firstly understand and evaluate like explained earlier, after knowing what I need to create I will be focusing now to how to do it.
Making an Algorithm and Implementing:
As my program will be facing four different cases, I want to deal with each one separately, or other words I am using
divide and conquer
algorithm to deal my problem. I will make my algorithm in steps:
Step1:
the first thing I want my program will do is ask the users for two numbers.
Step 2:
they’ll input the numbers
Step 3:
then I want to ask my users to enter the symbol of the arithmetic operation they want use.
Step 4:
my next step will depend on their input as which operator they typed in.
Step 5:
Next I used a divide and rule format and made a separate case for each arithmetic operator.
Step 6:
my users input will decide which statement/case to follow next, each case is coded differently (using divide and conquer rule) as in case1 I commanded the computer to add the two numbers, in and in case two I coded to subtract and so on
Step 7:
after the calculation they will print out the answer.
Step 8:
End the program.
Coding
and implementation:
Step 1
Step 2
Step 3
Step 4
Step 5, 6 & 7
When I began coding with an end in mind, keeping in mind the algorithm steps I just had to write the algorithm in a programming language i.e. C++ in this case.
Activity 2 – Programming Paradigms
Introduction:
The word paradigm means a way of thinking. Programming paradigms refers to the different structural styles of coding that get your task done. Some of the programming paradigms include;
· Imperative/Procedural Programming
· Object Oriented Programming OOP
· Event Driven Programming
I will be explaining all three separately in this report.
Procedural Programming:
Procedural programming is also known as imperative programming. The word imperative means giving a command. Procedural programming is the type of programming that requires a top to bottom approach which keeps a program simple and strait forward. In these paradigms the main focus of the code is what are you doing and how are you doing. This paradigm uses statements to specify actions. It’s the most common and old type of programming that is and was used in assembly languages. It may also include some functions
Some examples of imperative programming languages:
· FORTRON
· COBOL
· C
· GW BASIC
Main Characteristic:
The whole program in procedural programming focuses on just commands in a flow coded by the user, to only get the job done. No care for using the code again for future purposes or upgrading in a different program etc.
Advantages:
· As this programming is the most basic and simple one where you just command for each action, it is easy to learn and provides a guideline to the new programmers.
· We are just concerned about get the job done, not about using the code again or passing it on to the next coder.
· Easily read by compilers and interpreters as it is simple.
· In these programming the chance of logical errors is quite low as it is easy to keep track of the process.
Disadvantages:
· No security can be imposed in these programing. Data is exposed and can be seen by anyone and everyone.
· The code cannot be used again for any other similar purpose.
· It gets difficult to program as the program gets bigger and bigger.
· Hard to expand the program.
· As it gets harder to code in large problems, hence it has limited capabilities.
Object Oriented Programming:
As your programs grow the will end up with a bunch of functions that all over the place. You might find yourself coping same lines of codes over and over again. You make a change in one function and then all functions change and it ruins the code. That’s what we call a spaghetti code. Object oriented programming solved this problem. In object oriented programming we combine a group of variables and functions into a unit. We call that unit an object. Object oriented programming or OOP for short is completely based on interactions and manipulations of these objects to complete these tasks, solve problems and get the job done.
Characteristics:
Program is divided into a subset called objects. New objects can easily be added whenever necessary which means, that new data and functions can also be added when necessary which is the major advantage of OOP over procedural programming. Another characteristic of OOP is that data hidden in an object cannot be accessed by the outside functions. OOP depends more on data rather than the method itself. Function that used to operate that data are combined together to form a data structure, and then these data structure work on the objects made.
Advantages:
· The most major advantage of OOP over procedural programming is Re-usability. It means re-using that code again and again even for different purposes makes it so easy and efficient.
· With setting privilege we are exposing limited amount of data and that maintains the secuirity in the program.
· With separate objects to work, our data is more organized.
· Upgrading a code is made much simpler in objective oriented programming
Disadvantages
· It isn’t the best programming technique for small codes, it can make small programs lengthy as compared to other paradigms
· In larger programs as the code becomes heavy a stronger system is required to run.
Event Driven Programming:
In event driven programming the flow of the program is determined by events such as mouse clicks and presses sensor outputs, or messages from other programs or threads. It is the type of programming that is mainly used in graphical. These types of programming are done to respond to user engagement in a graphical way. Some examples that are a result of event driven programming are:
· Sensor outputs
· User actions
· GUI
Characteristics
Everything on the screen provides a service to the user. Anything that appears on the screen even the program window is a control and each control can have blocks of codes attached that perform service to the user. These blocks are referred to as events. These are also time-driven meaning that an event will only occur from a user input such as clicking a button. One another key function of event driven programming is its trigger functions. In these the user enters a special sequence of codes that have a different outcome as the other.
Advantages
· In event driven programming alteration is quite easy and that gives us with a quite flexibility of the code
· The sustainability of event driven programming towards graphical user interface is its most major advantage over other programming paradigms.
· Even driven allows to introduce new objects easily and then directly edit them
Disadvantages
· Difficult to find errors.
· Need strong system to run for complex and high GUIs.
· Sometimes more lengthy and confusing
· Not recommended for standard programming
Application Source Code Evaluation:
Here you can see an application code for a calculator in different programming paradigms.
Procedural Programming Approach
#include
int main() {
char operator 1; double first, second;
printf("Enter an operator (+,- ): "); scanf("%c", &operator 1); scanf("%61f %1f", &first, &second);
printf("Enter two operands: ");
switch (operatori) { case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second); break;
case
printf("%.1lf %.1lf = %.11f", first, second, first-second); break;
case *:
printf("%.11f* %.11f = %.118", first, second, first second);
break;
case '/":
printf("%.11f/ %.11f = %.11f", first, second, first / second); // operator doesn't match any case constant
break;
default:
printf("Error! operator is not correct");
return 0;
}
In this code you can clearly see the main structure of the program in the main function. No classes are made and the program is kept as simple as possible with no intent for the future use. Furthermore no objects and classes are introduced. These types of programming are best for small tasks like these which is why this code is so smaller as compared to the other codes
Object Oriented Programming Approach:
#include
using namespace std;
class Calculator {
public:
void add(int i, int a);
void subtract(int i, int a);
void mult(int i, int a);
void divide(int i, int a);
void modulo(int i, int a); void menu(); void driver(); void driver2();
int a,x,y,result; char choice;
void Calculator::add(int i, int a) { result = i + a; cout
}
void Calculator::subtract(int i, int a) { result =i-a; cout
}
void Calculator::mult(int i, int a){
result = i a;
cout
}
void Calculator::divide(int i, int a) { result = i/ a; %3D cout
}
void Calculator::modulo(int i, int a){ result = 1 % a;
cout
}
void Calculator::menu() {
void Calculator menu() {
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
MENU
}
void Calculator::driver() {
cout
In these programs the data is well organized and oriented. Separate classes are made which can be called again when required. Data is also re-useable. Classes and objects are made for future use. But for even for a simple program the code is almost 2 pages long. It would be difficult for the programmer to code this way. It might be helpful for future purposes as compared to normal programming.