0% found this document useful (0 votes)
8 views

CSC 201 Note - 1

Notes for computer programming

Uploaded by

fc5279413
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

CSC 201 Note - 1

Notes for computer programming

Uploaded by

fc5279413
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Introduction to computer programming.

Functional programming; Declarative


programming; Logic programming; Scripting languages. Introduction to object-
orientation as a technique for modelling computation. Introduction of a typical object-
oriented language, such as Java. Basic data types, variables, expressions,
assignment statements and operators. Basic objectoriented concepts: abstraction;
objects; classes; methods; parameter passing; encapsulation. Introduction to Strings
and string processing; Simple I/O; control structures; Arrays; Simple recursive
algorithms; inheritance; polymorphism.

Week 1: Introduction to Programming Concepts: Overview of


Programming Paradigms, Scripting Languages, and Problem-Solving in
Programming

1. Overview of Programming Paradigms

Programming paradigms are models or approaches to programming that guide how


problems are structured and solved. Here, we cover three key paradigms:

A. Functional Programming
Definition: A paradigm where computation is treated as the evaluation of mathematical
functions, avoiding changing state and mutable data.

Characteristics:
Emphasizes pure functions (functions that return the same output for the same input and
have no side effects).
Relies heavily on immutability (data that doesn’t change after creation).
Encourages function composition and higher-order functions (functions that can take
other functions as parameters or return them as results).

Examples of Functional Languages: Haskell, Lisp, and Erlang. JavaScript and Python
support functional programming concepts as well.

Use Cases: Often used in data processing, machine learning, and applications that benefit
from immutability (e.g., parallel processing).

B. Declarative Programming
Definition: A paradigm where the programmer specifies what the program should
accomplish rather than detailing how to achieve it.

Characteristics:
Focuses on expressing logic without describing the control flow.
Less code is typically required since specific implementation details are abstracted away.
Common in query languages and configuration management.

Examples of Declarative Languages: SQL for database queries, HTML for webpage
structure, and CSS for styling.

Use Cases: Frequently used in data manipulation and retrieval, configuration management,
and situations where outcomes are more important than procedural steps.
C. Logic Programming
Definition: A paradigm based on formal logic, where the programmer defines a set of rules,
facts, and relationships, and the program derives conclusions based on these.

Characteristics:
Uses facts and rules as a basis for reasoning.
Logic programs often rely on backtracking (trying different paths until a solution is found)
and unification (matching logic terms).

Examples of Logic Languages: Prolog and Datalog.

Use Cases: Well-suited for solving complex problems like constraint satisfaction
(scheduling, puzzles) and natural language processing.

2. Scripting Languages and Their Use Cases

Scripting languages are typically interpreted, lightweight, and designed to automate


tasks within larger programs or environments.

Characteristics of Scripting Languages:


Interpreted: They don’t need to be compiled, allowing faster development and testing.
Dynamic typing and automatic memory management, making them easy to use.
Often have simple syntax that is conducive to rapid development.

Examples of Scripting Languages:


Python: Used in web development, data science, and automation.
JavaScript: Primarily for web development, enabling dynamic content on web pages.
Ruby: Known for web development (particularly with Ruby on Rails).
Bash: A scripting language used in Unix/Linux for system administration and task
automation.

Common Use Cases:


Web Development: JavaScript is essential for client-side scripting on websites.
Automation: Python and Bash are commonly used for automating tasks like file
manipulation, data extraction, and system management.
Data Analysis and Machine Learning: Python has become popular for data analysis due
to libraries like pandas, NumPy, and TensorFlow.

3. Introduction to Problem-Solving in Programming

Programming is fundamentally about solving problems. To solve a problem with


code, you can follow a structured approach:

Problem-Solving Steps:

1. Understand the Problem: Clearly define the problem and identify requirements,
inputs, and outputs.
2. Plan the Solution: Break down the problem into smaller parts and outline a strategy.
This may involve pseudocode, flowcharts, or logic diagrams.
3. Implement the Solution: Write the code according to your plan. Keep it modular,
starting with a basic version and iterating from there.
4. Test and Debug: Check if your solution works with different inputs, and debug any
issues that arise.
5. Optimize and Improve: Once the solution works, consider ways to make it more
efficient or simpler.

Common Problem-Solving Techniques:


Divide and Conquer: Break down a large problem into smaller sub-problems (e.g.,
recursive algorithms).
Pattern Recognition: Recognize patterns in the problem that can simplify the solution.
Iterative Development: Start with a simple version of the solution and add more complexity
in stages.

Extra Activities

Activity 1: Group Discussion

Form groups to discuss scenarios where each programming paradigm (functional,


declarative, logic) would be useful. Students should provide examples and explain
why their chosen paradigm is suitable.

Activity 2: Hands-On Practice with Scripting Languages

Write a simple Python script to automate a task (e.g., reading a text file and counting
word occurrences). Alternatively, write a JavaScript program to manipulate an HTML
page (e.g., changing colors or adding elements dynamically).

Activity 3: Problem-Solving Workshop

 Provide students with a small problem (e.g., calculating the sum of an array or finding
the maximum number) and have them work through the problem-solving steps in
pairs.

Practice Questions

1. Short Answer Questions:

1. Describe the key characteristics of functional programming.


2. How does declarative programming differ from imperative programming?
3. Give an example of a use case where logic programming would be beneficial.

2. Scripting Language Exercises:

1. Write a Python script that takes a list of numbers and prints the squares of each
number.
2. Write a JavaScript function that changes the text color of a web page element when
a button is clicked.
3. Problem-Solving Questions:

1. You are given an array of numbers. Write an outline for a function that finds the
average of these numbers. Describe the problem-solving steps before writing the
code.
2. Suppose you need to create a schedule for several events. Which programming
paradigm (functional, declarative, or logic) might be most useful, and why?

You might also like