Fundamentals of Algorithms & Computer Problem Solving

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING

LESSON OUTCOMES

  • Differentiate between:

    • Program: A set of instructions that directs the computer to accomplish specific tasks.

    • Programming Language: A formal language consisting of a set of instructions that can be used to produce various kinds of output, which the computer can interpret. Examples include C++ and Visual BASIC.

    • Programming: The process of designing, writing, and implementing computer programs using programming languages; also known as software engineering.

    • Programmer: An individual who writes and implements programs to make computers perform specific tasks.

  • Explain the Program Development Life Cycle (PDLC): A series of steps for designing and developing programs.


INTRODUCTION TO PROGRAMMING

  • A Computer:

    • It is defined as an electronic device that operates under the control of instructions stored in its own memory.

    • Capable of:

    • Accepting data.

    • Processing data according to specified rules.

    • Producing results.

    • Storing results for future use.

PROGRAM
  • A program consists of statements written in a programming language to accomplish tasks.

  • Programming has evolved since its introduction in 1945 with various programming languages emerging over time.


PROGRAM DEVELOPMENT LIFE CYCLE (PDLC)

  1. Problem Analysis (Definition):

    • Also known as the definition phase where the problem is defined for a clear understanding of requirements.

      • Questions to consider for complete problem specification:

      • What are the input data?

      • What are the output (desired) data?

      • What formula is to be used?

      • What assumptions or constraints can be made?

      • What is the expected output screen?

  2. Algorithm Design:

    • This phase translates the specifications from the analysis into an algorithm.

    • An Algorithm: A step-by-step sequence of precise instructions that must terminate, detailing how data is processed to produce desired outputs.

    • Requirements of an algorithm:

      • Input and output: Must handle zero or more inputs and produce at least one output.

      • Unambiguous: Each step must be clear regarding its intention and execution.

      • Correct and efficient: The algorithm should solve the problem correctly and efficiently.

      • Finite: It must execute instructions and terminate in finite time.

  3. Algorithm Implementation (Coding):

    • The algorithm is translated into a specific programming language (e.g., C++) during this phase, involving coding, which comprises editing, compiling, and debugging.

      • Coding: The direct conversion of algorithm instructions into a programming language code.

  4. Program Testing & Debugging:

    • Involves testing the completed program to ensure it produces expected outputs.

    • Different testing data may be employed for verification of problem-solving capability.

  5. Program Maintenance & Documentation:

    • Programs may require updates to accommodate new requirements, necessitating modifications or additions to the code.

    • Proper documentation is vital for future development and ongoing maintenance efforts.


EXAMPLE OF PROBLEM

STEP 1: Problem Analysis
  • Input:

    • Input Variable:

    • number_1, number_2

  • Output:

    • Output Variables:

    • sum, average

  • Process/Formula:

    • sum = number_1 + number_2

    • average = sum / 2

STEP 2: Algorithm Design
(i) Pseudocode Design
Calculate the sum and average
Begin  
1. Get input  
   Read number_1  
   Read number_2  
2. Calculate sum  
   sum = number_1 + number_2  
3. Calculate average  
   average = sum / 2  
4. Display output  
   Print sum  
   Print average
End
(ii) Flowchart Design
Begin  
   Read number_1, number_2  
   sum = number_1 + number_2  
   average = sum / 2  
   Print sum  
   Print average  
End
STEP 3: Algorithm Implementation (Coding)
Simple Sequential C++ Program
#include <iostream.h>
int main() {
   int number_1, number_2; // Input variables  
   int sum, average; // Output variables  
   std::cout << "Enter first number";
   std::cin >> number_1;
   std::cout << "Enter second number";
   std::cin >> number_2;
   sum = number_1 + number_2; // Calculate sum  
   average = sum / 2; // Calculate average  
   std::cout << "The sum of " << number_1 << " and " << number_2 << " is " << sum;  
   std::cout << "The average of " << number_1 << " and " << number_2 << " is " << average;
   return 0;
}
Simple Modular C++ Program
#include <iostream>
using namespace std;
// Function Prototypes  
void get_Input();  
void calculate_Sum();  
void calculate_Average();  
void display_Output();

int main() {
    get_Input();
    calculate_Sum();
    calculate_Average();  
    display_Output();
    return 0;
}

void get_Input() {
   cout << "Enter first number ";
   cin >> number_1;
   cout << "Enter second number ";
   cin >> number_2;
}

void calculate_Sum() {
   sum = number_1 + number_2;
}

void calculate_Average() {
   average = sum / 2;
}

void display_Output() {
   cout << "The sum of " << number_1 << " and " << number_2 << " is " << sum;
   cout << "The average of " << number_1 << " and " << number_2 << " is " << average;
}

REVIEW QUESTIONS

  • What is a computer program?

  • Differentiate between programming language, programming, and programmer.

  • Briefly explain the tasks involved in the Program Development Life Cycle (PDLC).

  • Define what are Input, Process, and Output?

  • Differentiate between pseudocode and flowchart.