[go: up one dir, main page]

0% found this document useful (0 votes)
47 views10 pages

Assignment 2 PF

The document is an assignment submission that answers several questions about programming fundamentals. It begins by listing the assignment details such as the name, subject, and date. It then provides detailed answers to each question. The questions cover topics like defining an algorithm and pseudocode, explaining keywords and identifiers in C++, and providing examples of each. Pseudocode is provided to demonstrate computing an overall grade based on different assessments. Various algorithm types are also enumerated.

Uploaded by

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

Assignment 2 PF

The document is an assignment submission that answers several questions about programming fundamentals. It begins by listing the assignment details such as the name, subject, and date. It then provides detailed answers to each question. The questions cover topics like defining an algorithm and pseudocode, explaining keywords and identifiers in C++, and providing examples of each. Pseudocode is provided to demonstrate computing an overall grade based on different assessments. Various algorithm types are also enumerated.

Uploaded by

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

Assignment Name Assignment 2 Submitted By

Subject Programming Fundamentals Muhammad Ehtisham Amjad


Submitted to Dr. Naseer Jan Dated
Program BSSE-II-B March 03, 2024

QUESTIONS
The questions listed below need to be answered:
 What is an algorithm? How does it differ from program control? Demonstrate the concept of
an algorithm using an example of feeding family.
 Define pseudocode and provide a pseudocode representation for computing the overall
grade in a subject, considering assignments, quizzes, exams, and labs.
 Explain the concepts of keywords and identifiers, and enumerate various keywords used in
C++.
 Explain the purpose of using namespace std, #include, cin>>, cout<<, int main(), return 0,
 etc.
ANSWERS
1. WHAT IS AN ALGORITHM? HOW DOES IT DIFFER FROM PROGRAM CONTROL?
DEMONSTRATE THE CONCEPT OF AN ALGORITHM USING AN EXAMPLE OF
FEEDING FAMILY.

1.1 WHAT IS AN ALGORITHM?


An algorithm is a procedure used for solving a problem or performing a computation.
Algorithms act as an exact list of instructions that conduct specified actions step by step in
either hardware- or software-based routines.
Algorithms are widely used throughout all areas of IT. In mathematics, computer
programming and computer science, an algorithm usually refers to a small procedure that
solves a recurrent problem. Algorithms are also used as specifications for performing data
processing and play a major role in automated systems.
An algorithm could be used for sorting sets of numbers or for more complicated tasks, such
as recommending user content on social media. Algorithms typically start with initial input
and instructions that describe a specific computation. When the computation is executed, the
process produces an output.
1.2 HOW DO ALGORITHMS WORK?
Algorithms work by following a set of instructions or rules to complete a task or solve a
problem. They can be expressed as natural languages, programming languages, pseudocode,
flowcharts and control tables. Natural language expressions are rare, as they are more
ambiguous. Programming languages are normally used for expressing algorithms executed by a
computer.
Algorithms use an initial input along with a set of instructions. The input is the initial data
needed to make decisions and can be represented in the form of numbers or words. The input
data gets put through a set of instructions, or computations, which can include arithmetic and
decision-making processes. The output is the last step in an algorithm and is normally expressed
as more data.

For example, a search algorithm takes a search query as input and runs it through a set of
instructions for searching through a database for relevant items to the query. Automation
software acts as another example of algorithms, as automation follows a set of rules to complete
tasks. Many algorithms make up automation software, and they all work to automate a given
process.

1.3 WHAT ARE DIFFERENT TYPES OF ALGORITHMS?


There are several types of algorithms, all designed to accomplish different tasks:
1.3.1 SEARCH ENGINE ALGORITHM:
This algorithm takes search strings of keywords and operators as input, searches its
associated database for relevant webpages and returns results.
1.3.2 ENCRYPTION ALGORITHM:
Encryption algorithm. This computing algorithm transforms data according to specified
actions to protect it. A symmetric key algorithm, such as the Data Encryption Standard, for
example, uses the same key to encrypt and decrypt data. If the algorithm is sufficiently
sophisticated, no one lacking the key can decrypt the data.
1.3.3 GREEDY ALGORITHM:
Greedy algorithm. This algorithm solves optimization problems by finding the locally
optimal solution, hoping it is the optimal solution at the global level. However, it does not
guarantee the most optimal solution.
1.3.4 RECURSIVE ALGORITHM:
Recursive algorithm. This algorithm calls itself repeatedly until it solves a problem.
Recursive algorithms call themselves with a smaller value every time a recursive function is
invoked.
1.3.5 BACKTRACKING ALGORITHM:
Backtracking algorithm. This algorithm finds a solution to a given problem in incremental
approaches and solves it one piece at a time.
1.3.6 DIVIDE AND CONQUER ALGORITHM:
Divide-and-conquer algorithm. This common algorithm is divided into two parts. One part
divides a problem into smaller subproblems. The second part solves these problems and then
combines them to produce a solution.
1.3.7 DYNAMIC PROGRAMMING ALGORITHM
Dynamic programming algorithm. This algorithm solves problems by dividing them into
subproblems. The results are then stored to be applied to future corresponding problems.
1.3.8 BRUTE FORCE ALGORITHM:
Brute-force algorithm. This algorithm iterates all possible solutions to a problem blindly,
searching for one or more solutions to a function.
1.3.9 SORTING ALGORITHM:
Sorting algorithm. Sorting algorithms are used to rearrange data structures based on a
comparison operator, which is used to decide a new order for data.
1.3.10 HASHING ALGORITHM:
Hashing algorithm. This algorithm takes data and converts it into a uniform message with a
hashing.
1.3.11 RANDOMIZED ALGORITHM:
Randomized algorithm. This algorithm reduces running times and time-based complexities.
It uses random elements as part of its logic.

1.4 PROGRAM CONTROL AND ALGORITHM?

Program control, on the other hand, refers to the flow of instructions within a computer program.
It determines the order in which instructions are executed and how the program responds to
different inputs or conditions. While algorithms provide a high-level description of a solution,
program control specifies the details of how the solution is implemented in a programming
language.
1.5 ALGORITHM FOR FEEDING A FAMILY:
The steps involved would be as under, one can represent them using a diagram as well:
 Gather Ingredients
 Prepare Cooking Utensils
 Choose a Recipe
 Follow the Recipe Steps
 Cook the Meal
 Serve the Meal
 Enjoy the Meal
2. DEFINE PSEUDOCODE AND PROVIDE A PSEUDOCODE REPRESENTATION FOR
COMPUTING THE OVERALL GRADE IN A SUBJECT, CONSIDERING
ASSIGNMENTS, QUIZZES, EXAMS, AND LABS.

2.1 PSEUDOCODE DEFINED AND EXPLAINED:


In computer science, pseudocode is a description of the steps in an algorithm using a mix of
conventions of programming languages (like assignment operator, conditional operator, loop)
with informal, usually self-explanatory, notation of actions and conditions. Although pseudocode
shares features with regular programming languages, it is intended for human reading rather than
machine control. Pseudocode typically omits details that are essential for machine
implementation of the algorithm. The programming language is augmented with natural
language description details, where convenient, or with compact mathematical notation. The
purpose of using pseudocode is that it is easier for people to understand than conventional
programming language code, and that it is an efficient and environment-independent description
of the key principles of an algorithm. It is commonly used in textbooks and scientific
publications to document algorithms and in planning of software and other algorithms.
No broad standard for pseudocode syntax exists, as a program in pseudocode is not an executable
program; however, certain limited standards exist (such as for academic assessment).
Pseudocode resembles skeleton programs, which can be compiled without errors. Flowcharts,
drakon-charts and Unified Modelling Language (UML) charts can be thought of as a graphical
alternative to pseudocode, but need more space on paper. Languages such as HAGGIS bridge the
gap between pseudocode and code written in programming languages.

2.1 CODE:
1. Initialize totalScore to 0
2. Input and compute assignmentScore
3. Input and compute quizScore
4. Input and compute examScore
5. Input and compute labScore
6. Input and compute assignmentWeightage
7. Input and compute quizWeightage
8. Input and compute examWeightage
9. Input and compute labWeightage
10. Compute weightedSum as (assignmentScore * assignmentWeightage) + (quizScore *
quizWeightage) + (examScore * examWeightage) + (labScore * labWeightage)
11. Set totalWeightage to assignmentWeightage + quizWeightage + examWeightage +
labWeightage
12. Compute overallGrade as weightedSum / totalWeightage
13. Output overallGrade

3. EXPLAIN THE CONCEPTS OF KEYWORDS AND IDENTIFIERS, AND ENUMERATE


VARIOUS KEYWORDS USED IN C++.

3.1 KEYWORDS:
Keywords are reserved words in a programming language that have predefined meanings and
cannot be used as identifiers (variable names, function names, etc.). They convey specific
instructions or represent essential elements of the language.
Keywords are the building blocks of the language's syntax, helping to structure the code and
define its logic.
For example, in C++, keywords include int, if, else, for, while, class, return, and many more.

3.2 IDENTIFIERS:
Identifiers are names given to various program elements such as variables, functions, classes,
etc. They are user-defined and should follow certain rules, unlike keywords.
Identifiers provide a way for programmers to name and reference elements within their code,
making it more readable and understandable.
For example, in C++, identifiers can be names like counter, calculateTotal, myClass, etc.
3.3 DATA TYPES:
int, float, double, char, bool, etc.
 CONTROL FLOW:
if, else, switch, case, break, continue, return, etc.
 LOOPS:
for, while, do, break, continue, etc.
3.4 FUNCTIONS:
void, main, sizeof, etc.
3.5 CLASSES AND OBJECTS:
class, struct, private, public, new, delete, etc.
3.6 MODIFIERS:
const, static, virtual, mutable, volatile, etc.

4. EXPLAIN THE PURPOSE OF USING NAMESPACE STD, #INCLUDE,


CIN&GT;&GT;, COUT&LT;&LT;, INT MAIN(), RETURN 0, ETC.

USING NAMESPACE STD;:

Simplifies the use of standard C++ library elements by bringing them into the current scope.

#INCLUDE:

Includes external libraries or modules, allowing access to additional functionalities.

CIN >>:

Reads data from the standard input (e.g., keyboard) into variables during program execution.

COUT <<:

Outputs data to the standard output (e.g., console) during program execution.

INT MAIN():

Defines the main function, serving as the entry point for program execution.

RETURN 0;:

Indicates successful program termination; conventionally used to signal that the program
executed without errors.
4.2 OUTPUT

You might also like