Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab No 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

Lab Manual

Programming
Fundamentals
CSC-113

FALL 2024
RUBRICS GUIDELINE FOR SOFTWARE BASED LAB

Course Name (Course Code), ______________________Semester, Batch_____________


Name of Student: _______________________________ Roll No. __________________

Criteria Score Exceeds Expectations Meets Developing Unsatisfactory


(>=90%) Expectations (50%-69%) (<50%)
(70%-89%)
Able to use software with Able to use software Able to use Unable to use the
Digital its standard and advanced with its standard software with software
Competencies features without and advanced its standard
3 assistance features with features with
minimal assistance assistance
Able to program/ simulate Able to program/ Able to Unable to
Technical the lab tasks with simulate the lab program/ program/simulate
Implementation 3 simplification tasks without errors simulate lab
tasks with
errors
Able to achieve all the Able to achieve all Able to Unable to
Lab Evaluation 3 desired results in alternate the desired results achieve most achieve the
Results ways of the desired desired results
results with
errors
Laboratory manual has no Laboratory manual Laboratory Laboratory
grammatical and/ or has very few manual has manual has
spelling errors. grammatical/ multiple several
All sections of the report spelling errors. grammatical/ grammatical/
1 are very well-written and All sections of the spelling spelling errors
Laboratory
technically accurate. report are errors. and sentence
Manual
technically accurate. A few construction is
sections of the poor.
report contain
technical
errors.

RUBRIC-LABORATORY MANUAL
Course Name (Course Code), ______________________Semester, Batch_____________
Name of Student: _______________________________ Roll No. __________________

Score Sheet
Lab
Digital Problem Solving Evaluation Lab Total
Competencies Skills Results Manual Score
Lab (3) (3) (3) (1) (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Total

Formula = (obtained marks/140)*25


Total lab manual marks 25

TABLE OF CONTENTS
WEEKS EXPERIMENTS

Introduction to Programming Environment and understanding the concept of


algorithms, flowcharts, and pseudo code in computer programming.
1,2 Basic Structure of C Language, First Console application and output examples,
Comments, Directives, Statements
Variable Types (Integer, Float, Character, Double Declaration, Assignment,
initialization of variables, Const and macros, static)
Casting (Implicit and explicit)
3 Formatted Input and Output using Escape Sequence
printf() and scanf() problems
Operators

4  Arithmetic Operators
 Logical operators
 Relational operator
Decision Making
 One way if statements, If-else statements
5  Nested if statements
 Common errors in selection statements.
 Switch cases
Iterations using loops
 For ()
 While ()
6
 do while ()
 nested Loop ()
 infinite loop ()
Arrays:
 Single
7
 Double
 Multidimensional
8 String Operations and Manipulation
9,10 Function
 Simple functions
 Function that returns a value
 Using arguments to pass data to a function
 User defined function
 Using more than one function
 Global & local (scope)
 Prototype VS classical K and R
 Preprocessor Directives
11 Recursion
Pointers
 Pass by val & by ref
 Returning data from functions
12,13  Pointers and arrays
 Pointers and strings

Lab Manual
 Double Indirection: Pointers to pointers
 Pointer to functions
Structures and Unions.
14
Making header file
15 Programming
Final Project Presentation

Fundamentals
CSC-113

LAB 01

FALL 2024
Table of Content
Lab Objective
No#

1 1. Programming Languages
● Understand the concept, importance, and types of programming
languages.
● Discuss the evolution and history of programming languages,
highlighting key milestones.

2. C Programming Language
● Gain an overview of C, its history, and significance.
● Learn basic syntax and structure; develop simple programs to
practice fundamental concepts.

3. Translators
● Define translators and their role in code conversion.
● Differentiate between assemblers, compilers, and interpreters;
understand program translation and execution.

4. Types of Translators
● Assemblers: Convert assembly language code into machine
code.
● Compilers: Translate high-level languages into machine code,
focusing on efficiency and error-checking.
● Interpreters: Execute code line-by-line, emphasizing flexibility
and debugging ease.

5. Algorithms
● Define algorithms and their significance in problem-solving and
programming.
● Learn characteristics of good algorithms; develop simple
algorithms for practice.

6. Flowcharts
● Understand flowcharts and their role in representing algorithms
visually.
● Learn standard symbols; create flowcharts to visualize program
logic.

7. Pseudocode
● Define pseudocode and its role in bridging algorithms and
coding.
● Learn writing conventions; develop pseudocode for detailing
logic and steps in a language-independent manner.

Lab 1

Course Name : Programming Fundamentals


Course Code : CSC 113
Semester : Summer 2024
Program : BS
Credits :1
Faculty : Taha Shaikh
Student Name :
Student ID :
Total Marks : 100
Obtained Marks :
Submitted Date :

Lab 1

Objective:
The objective of this lab is to give introduction to:
Programming Languages:

1. Understand the concept and importance of programming languages in the field of


computer science.
2. Explore the different types of programming languages, including high-level and low-
level languages.
3. Discuss the evolution and history of programming languages, highlighting key
milestones.

C Programming Language:

1. Gain a comprehensive overview of the C programming language, its history, and its
significance.
2. Learn the basic syntax and structure of C programs.
3. Develop simple C programs to practice fundamental concepts like variables, data types,
and control structures.

Translators:

1. Define what translators are in the context of programming and their role in converting
code.
2. Differentiate between the main types of translators: assemblers, compilers, and
interpreters.
3. Understand the process of translation and execution of programs through these
translators.

Types of Translators:

1. Assemblers: Learn how assemblers convert assembly language code into machine code.
2. Compilers: Study how compilers translate high-level programming languages into
machine code, focusing on their efficiency and error-checking capabilities.
3. Interpreters: Explore how interpreters execute high-level programming languages by
translating code line-by-line, emphasizing their flexibility and ease of debugging.

Algorithms:

1. Define what an algorithm is and understand its significance in problem-solving and


programming.
2. Learn the characteristics of a good algorithm, including correctness, efficiency, and
clarity.
3. Develop simple algorithms for basic problems to practice designing step-by-step
solutions.
Flowcharts:

1. Understand what flowcharts are and how they visually represent algorithms.
2. Learn the standard symbols used in flowcharts and their meanings.
3. Create flowcharts for various algorithms to practice visualizing the logical flow of a
program.

Pseudocode:

1. Define pseudocode and its role in bridging the gap between algorithms and actual coding.
2. Learn the conventions and structure of writing pseudocode.
3. Develop pseudocode for simple algorithms to practice detailing the logic and steps of a
solution in a language-independent manner.

A Dive into Programming

Are you aiming to become a Software engineer one day? Do you also want to develop an
application for Solving Problems that People worldwide would love to use? Are you passionate
enough to take the big step to enter the programming world? Then you are in the right place. In
this article, you will get a brief Introduction to Programming Languages. You will understand the
following pointers:

1. What is Software?

2. Types of Software

3. What is a Language?

4. Types of Computer Languages

5. What is a Programming Language?

Program and Programming:

Program: A Program is a common computer term executed by software that runs on a computer
when the program is used as what it means to create a software program. The set of instructions
is called a program. For Example, Programmers create programs by writing code that instructs
the computer what to do and execute it on a special software designed for it, such as turbo C for
executing ‘C’ Programs.

Programming: - Programming is the implementation of logic to facilitate the specified


computing operations and functionality. Thus, we can say that programming is the process of
writing a program.

What is Software?

Software refers to a collection of data, programs, procedures, and instructions that tell a
computer how to work. This is in contrast to physical hardware, from which the system is built
and actually performs the work. So, Software is a collection of programs that uses the resources
of the Hardware components. A Program is a set of instructions designed for a particular task.

The set of programs is called software. Let us understand this with an example, i.e., Calculator.
For each button, there is some program written inside it. That means a calculator is a collection
of programs. And we can also say that a Calculator is software. So, the software is a collection of
programs.

What is a Language?

Generally, languages are used to communicate with others. The languages like Odia / English /
Marathi / Hindi are called human/regional languages used to communicate with humans.
Computer languages are used to write the programs [software] to communicate with the
machines

Types of Computer Languages:

Computer languages are the means by which computers are instructed to perform specific tasks.
They vary greatly in syntax, level of abstraction, and specific use cases. Here’s an overview of
the different types of computer languages: Basically, computer languages are divided into 3
types.

Machine Language:

● Description: The most basic type of computer language, consisting of binary code (0s
and 1s). It is directly understood by the computer’s central processing unit (CPU).

● Uses: Low-level operations and basic functioning of the hardware.

Assembly Language:

● Description: A low-level programming language that uses symbolic code (mnemonics)


and is a little more human-readable than machine language.

● Uses: System programming, hardware manipulation, and performance-critical tasks


where direct control over the hardware is necessary.

High-Level Languages:

More abstract and closer to human languages, high-level languages are easier to read, write, and
maintain. They are platform-independent and require a compiler or interpreter.
Examples:

● Procedural Programming Languages: Like C, Fortran, and COBOL, focusing on a


series of computational steps or procedures.

● Object-Oriented Programming Languages: Java, C++, and Python, based on the


concept of “objects,” which are data structures containing data and procedures.

● Functional Programming Languages: Like Haskell, Scala, and Erlang, emphasizing the
application of functions.

● Scripting Languages: Used for automating tasks, including Python, Perl, and JavaScript.

C is a high-level language with low-level features. Hence, C is also called middle-level language.
High-level features allow designing application software like calculator, calendar, media player,
etc.; low-level features are used to design system software like OS, device drivers, and
translators. etc. Hence, C is multi-purpose. C is a high-level/middle-level programming
language.
The languages are used to communicate with others. Computer languages are used to write the
programs [software] to communicate with the machines.

What is a Programming Language?

A programming language is a formal language comprising a set of instructions that produce


various kinds of output. Programming languages are used in computer programming to
implement algorithms. A Programming Language is a formal language comprising a set of
instructions used to communicate with the computer. Programming Language is classified into
two types:

● High-Level Programming Language

● Low-level Programming Language

For a better understanding, please have a look at the following image.

High-Level Programming Language:

A high-level programming language is a type of programming language that provides a strong


level of abstraction from the computer’s instruction set architecture. Compared to low-level
programming languages, high-level languages are closer to human languages and further from
machine languages, making them more readable and understandable for programmers.

The High-Level Programming Languages are syntactically similar to English and easy to
understand. High-Level Programming Languages are user-dependent languages. A High-Level
Programming Language combines alphabets, digits, and symbols. It is called micro statements.
By using a high-level programming language, we are developing user interface applications.
Examples: C, C++, VC++, JAVA, C#, Swift, Objective C, D-Language

Low-Level Programming Language:

Low-level programming languages are computer languages with a very small degree of
abstraction from a computer’s machine code or architecture. They are closer to the hardware than
high-level programming languages, making them more complex and harder for humans to
understand, but they offer greater control over the hardware.

The Low-Level Programming Languages are the languages that can be easily understandable to
the system. These are system-dependent languages. The two main categories of low-level
programming languages are as follows:

1. Machine Language

2. Assembly Language

Machine Language:

Machine Language is the fundamental language for the system. It can be directly understandable
without any translation. These are machine-oriented languages that use the collection of the
binary of 1’s and 0’s.

● Nature: This is the most basic level of programming language. Machine language
comprises binary code – a series of 0s and 1s.

● Direct Interaction: It allows direct interaction with the hardware and is specific to a
particular processor type. Programs written in machine language are executed directly by
the computer’s CPU.

● Usage: Due to its complexity and the need for precise hardware knowledge, machine
language is rarely used for programming in modern times, except in very specific
scenarios like bootloader or firmware programming.

Assembly Language:

The Assembly Language can be called Symbolic Language. To remember easily the program
coding be implementing this language. Different types of symbols will be used in this language
to design the program. However, this assembly code is not directly understandable to the system,
so we require translators.

● Mnemonic Codes: Assembly language uses short, mnemonic codes like ADD for
addition and SUB for subtraction, which are easier for humans to understand than binary
code.
● Assembler: Programs written in assembly language are converted into machine language
by a program called an assembler. This conversion process is relatively straightforward
as there is a direct correspondence between assembly language commands and machine
language instructions.

● Control and Efficiency: Assembly language provides more control over the hardware
than high-level languages. It allows programmers to utilize the full potential of the
hardware, achieving performance optimizations that are not possible in higher-level
languages.

● Usage: Commonly used in system programming, embedded systems, device drivers, and
situations where hardware interaction and performance are critical. However, it requires a
deep understanding of the computer architecture and is more error-prone and time-
consuming to write and maintain than high-level languages.

As a programmer, if we know the programming language, it is impossible to interact with


computers because the computer can only understand binary code.

What is a Translator?

The user’s instructions are always in English, which is called source code. But the computer
cannot understand this source code, and the computer’s understandable code is binary / machine.
To convert this source code into binary code, we are using the interface software called
translators.

In programming, a translator is a software tool that converts code written in one programming
language into another language or into a form that a computer can execute. This process is
essential because programmers write code in high-level programming languages, but computers
can only execute instructions in their own machine language. The translators are classified into
three types:

For a better understanding, please have a look at the following image.

Compiler and interpreter are both used to convert high-level programs to machine code.
Assembler is used to convert low-level programs to machine code.
Compiler:

A compiler is the system software that translates High-level programming language code into
binary format in a single step, except for those lines with an error. It checks all kinds of limits,
ranges, errors, etc.

Key Points of Compiler:


● A compiler translates the entire source code of a programming language (like C, C++, or
Java) into machine code before the program is run.

● This translation is done all at once, creating an executable file that can be run
independently of the original source code.

● Since the entire code is converted and optimized at once, compiled programs often run
faster than interpreted ones.

● Examples: GCC for C and C++, javac for Java.

Interpreter:

It is the system software that converts programming language code into binary format step by
step, i.e., line-by-line compilation takes place. It reads one statement and then executes it until it
proceeds further to all the statements. If an error occurs, it will stop the compilation process.
Development-wise, an interpreter is recommended to use.

Key Points of Interpreter

● An interpreter translates and executes the source code line by line on the fly.
● It reads the source code, interprets it, and executes it directly without producing a
separate machine code file.

● This approach is generally slower than compilation because each line is translated during
execution, but it allows for more dynamic programming and quicker testing and
debugging.

● Interpreted languages include Python, Ruby, and JavaScript.

Note: The compiler converts the total source code at once by leaving the error lines. At the same
time, the interpreter is line by line. C & C++ are compiler-based languages. Java / .Net / Python,
etc., are compiler-based interpreted languages.

Assembler:

It is the system software that converts assembly language instructions into binary formats. The
assembler’s working style is similar to the compiler.

Key Points of Assembler:

● An assembler is a specific type of translator used for converting assembly language – a


low-level language closely related to machine language but more readable by humans –
into machine code.

● It performs a straightforward translation of mnemonic operation codes and symbolic


addresses into their machine code equivalents.

Differences Between Compilers and Interpreters and Assemblers:

Compilers, Interpreters, and Assemblers are all types of translators in programming, each serving
a distinct purpose in converting source code into a format that a computer can execute.
Understanding the differences between them is key for programmers. Here’s a detailed
comparison:

Compilers
● Function: Translates the entire source code of a high-level programming language into
machine code (or intermediate code) in one go.

● Execution: The output is an executable file or object code which can be run
independently from the original source code.

● Speed: Generally, results in faster execution of the final program because the translation
is done beforehand.

● Use Cases: Used for languages like C, C++, and Java.

● Debugging: Debugging can be more challenging because the compilation process is


separate from execution.

Interpreters

● Function: Translates high-level programming language into machine code on the fly,
executing line by line.

● Execution: No separate executable file is produced. The interpreter reads and executes
the code directly.

● Speed: Typically, slower execution than compiled programs because translation happens
during runtime.

● Use Cases: Common for scripting and dynamically typed languages like Python,
JavaScript, and Ruby.

● Debugging: It is easier to debug since it executes the code line by line and can stop
immediately upon encountering errors.

Assemblers

● Function: Translates assembly language, which is a low-level language but more human-
readable than machine code, into binary machine code.

● Execution: Produces machine code directly executed by the computer’s CPU.

● Speed: The output, being machine code, is very fast and efficient.

● Use Cases: Mainly used for systems programming and working closely with hardware.

● Debugging: Debugging is more complex due to the low-level nature of assembly


language.
Definition of Algorithm

The word Algorithm means” A set of finite rules or instructions to be followed in calculations or
other problem-solving operations”
Or
” A procedure for solving a mathematical problem in a finite number of steps that frequently
involves recursive operations”.

Therefore, Algorithm refers to a sequence of finite steps to solve a particular problem.

Use of the Algorithms:

Algorithms play a crucial role in various fields and have many applications. Some of the key
areas where algorithms are used include:

1. Computer Science: Algorithms form the basis of computer programming and are used to
solve problems ranging from simple sorting and searching to complex tasks such as
artificial intelligence and machine learning.

2. Mathematics: Algorithms are used to solve mathematical problems, such as finding the
optimal solution to a system of linear equations or finding the shortest path in a graph.

3. Operations Research: Algorithms are used to optimize and make decisions in fields
such as transportation, logistics, and resource allocation.

4. Artificial Intelligence: Algorithms are the foundation of artificial intelligence and


machine learning, and are used to develop intelligent systems that can perform tasks such
as image recognition, natural language processing, and decision-making.

5. Data Science: Algorithms are used to analyze, process, and extract insights from large
amounts of data in fields such as marketing, finance, and healthcare.
Algorithm for Making a Peanut Butter Sandwich

1. Start

2. Gather all necessary ingredients and tools:

o Two slices of bread

o Peanut butter

o A butter knife

o A plate

3. Place the slices of bread on the plate.

4. Open the peanut butter jar.

5. Use the butter knife to scoop a sufficient amount of peanut butter.

6. Spread the peanut butter evenly on one slice of bread using the knife.

7. Place the other slice of bread on top of the peanut buttered slice.

8. Press the two slices together gently.

9. Cut the sandwich in half (optional).

10. Serve the sandwich.

11. End

Algorithm for Finding the Average

1. Start
2. Initialize a variable sum to 0.
3. Initialize a variable count to the number of elements in the list.
4. Loop through each element in the list:
o Add the current element to sum.
5. Calculate the average by dividing sum by count.
6. Print the average.
7. End
Algorithm for Finding the Maximum Value in a List

1. Start

2. Initialize a variable max with the value of the first element in the list.

3. Loop through each element in the list starting from the second element:

o If the current element is greater than max:

▪ Update max to the current element.

4. After the loop ends, max will hold the largest number in the list.

5. Print max.

6. End

There are two main methods of representing algorithms.

1. Pseudo code

2. Flowchart

What is a Pseudocode?

Pseudocode is a tool that can be used to write a preliminary plan for the development of a
computer program. It is a generic way of describing an algorithm without the use of any specific
programming language syntax. In fact, it serves as an English like way to state an algorithm.

Example: A pseudo code for finding average.

Algorithm FindAverage

Input: A list of numbers

Output: The average of the numbers

Begin

Set sum to 0

Set count to the length of the list

For each number in the list


Add number to sum

End For

Set average to sum / count

Print average

End

Example: A pseudo code for finding maximum number in the list.

Algorithm FindMaximum

Input: A list of numbers

Output: The maximum number in the list

Begin

Set max to list[0]

For i from 1 to length of list - 1

If list[i] > max

Set max to list[i]

End If

End For

Print max

End

What is a Flowchart?
Flowchart is a graphical representation of an algorithm. Programmers often use it as a program-
planning tool to solve a problem. It makes use of symbols which are connected among them to
indicate the flow of information and processing.
The process of drawing a flowchart for an algorithm is known as “flowcharting”.

Basic Symbols used in Flowchart Designs


1. Terminal: The oval symbol indicates Start, Stop and Halt in a program’s logic flow. A
pause/halt is generally used in a program logic under some error conditions. Terminal is the
first and last symbols in the flowchart.

● Input/Output: A parallelogram denotes any function of input/output type. Program


instructions that take input from input devices and display output on output devices are
indicated with parallelogram in a flowchart.

● Processing: A box represents arithmetic instructions. All arithmetic processes such as


adding, subtracting, multiplication and division are indicated by action or process
symbol.

● Decision Diamond symbol represents a decision point. Decision based operations such as
yes/no question or true/false are indicated by diamond in flowchart.

● Connectors: Whenever flowchart becomes complex or it spreads over more than one
page, it is useful to use connectors to avoid any confusions. It is represented by a circle.
● Flow lines: Flow lines indicate the exact sequence in which instructions are executed.
Arrows represent the direction of flow of control and relationship among different
symbols of flowchart.

Rules For Creating Flowchart:

A flowchart is a graphical representation of an algorithm.it should follow some rules while


creating a flowchart
Rule 1: Flowchart opening statement must be ‘start’ keyword.
Rule 2: Flowchart ending statement must be ‘end’ keyword.
Rule 3: All symbols in the flowchart must be connected with an arrow line.
Rule 4: The decision symbol in the flowchart is associated with the arrow line
Lab Tasks:

1. Write a Pseudocode to display your name and you student id.

2. Write a Pseudocode for adding, subtracting, multiply and divide two numbers

3. Write a pseudocode and draw a flowchart that reads in the radius and length of a
cylinder and computes the volume using the following formulas:

Area = radius2 * π

Volume = Area * length

4. Write a pseudocode that converts pounds into kilograms. The program prompts the
user to enter a number in pounds, converts it to kilograms, and displays the result. One
pound is 0.454kilogram.

You might also like