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

Chap_1_prob_solving

Chapter 1 covers problem-solving with computers, focusing on problem analysis, algorithm design, and the history and structure of the C programming language. It outlines key steps in problem-solving, including data gathering, analysis, programming, testing, and deployment. The chapter also details the process of creating and executing a C program, emphasizing the importance of testing and debugging.

Uploaded by

jeevanpkrl17
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)
20 views

Chap_1_prob_solving

Chapter 1 covers problem-solving with computers, focusing on problem analysis, algorithm design, and the history and structure of the C programming language. It outlines key steps in problem-solving, including data gathering, analysis, programming, testing, and deployment. The chapter also details the process of creating and executing a C program, emphasizing the importance of testing and debugging.

Uploaded by

jeevanpkrl17
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/ 24

Chapter 1

Unit 1: Problem Solving with Computer [6 Hrs.]


1.1 Problem analysis
1.2 Algorithms and Flowchart
1.3 History of C
1.4 Structure of C program
1.4.1 Debugging
1.4.2 Compiling a C program
1.4.3 Executing a C program
1.5 Testing and Documentation
1.6 Preprocessor and Macros

Problem Solving with Computer

Problem solving with a computer involves leveraging computational tools, software, and algorithms to
identify, analyze, and address challenges. This fundamental process is crucial in various fields, from
software development to data analysis and artificial intelligence.

This process typically includes several key steps

 Problem Identification
The first step is to clearly define and understand the problem you want to solve. This involves
identifying the objectives, constraints, and requirements of the problem.

 Data gathering
In many cases, data is crucial for problem solving. You may need to collect, retrieve, or generate data
that is relevant to the problem at hand.

 Analysis
Using computational tools, you can analyze data and gain insights into the problem. This might involve
data visualization, statistical analysis, or more advanced techniques like machine learning.

 Algorithm Design
Once you have a good understanding of the problem, you design algorithms or procedures that outline
how the computer should process the data to find a solution. This step is critical for creating an efficient
and effective solution.

1|Page
 Programming
You implement the designed algorithms by writing code in a programming language. This code
instructs the computer on how to perform the necessary computations and operations.

 Testing and Debugging


After writing the code, you test it with different inputs and scenarios to ensure it works correctly.
Debugging involves identifying and fixing errors or issues in the code.

 Optimization
Depending on the problem's complexity, you may need to optimize the code to make it more efficient,
using techniques like algorithmic improvements, parallelization, or hardware acceleration.

 Validation
Ensure that the solution meets the requirements and objectives of the problem. This may involve
comparing the results with expected outcomes and validating the accuracy of the solution.

 Deployment
If the solution is intended for practical use, you deploy it in a real-world environment where it can
be accessed and used by others.
 Maintenance
Continuously monitor and maintain the solution to ensure it remains functional and up-to-date. This
includes fixing any issues that arise and making improvements as needed.

Problem Analysis

Problem analysis is the initial phase of the problem-solving process. It involves a systematic
examination and understanding of a problem or challenge to gather information, identify its underlying
causes, and define its boundaries. The primary goal of problem analysis is to gain insight into the
problem's nature and characteristics, setting the stage for developing effective solutions.

It's a systematic exploration of a problem to:

 Problem Definition:
Clearly articulate and define the problem. This includes specifying its scope, objectives, and any
constraints or limitation.

 Information Gathering:
Collect relevant data and information related to the problem. This may involve conducting research,
interviews, surveys, or data analysis to understand the issue thoroughly.
2|Page
 Root Cause Analysis:
Identify the root causes or contributing factors that lead to the problem. This step helps in addressing
the underlying issues rather than just the symptoms.

 Boundary Definition:
Determine the boundaries of the problem. Understand what is included and excluded from the scope of
the problem to avoid scope creep.

 Stakeholder Analysis:
Identify and involve relevant stakeholders or individuals affected by the problem. Understanding their
perspectives and needs is crucial for finding a solution that is acceptable to all parties.

 Problem Decomposition:
Break down the problem into smaller, more manageable components or sub-problems. This simplifies
the analysis and solution-finding process.

 Data Validation:
Ensure the accuracy and reliability of the data used in the analysis. Validate data sources and check for
any inconsistencies or errors.

 Documentation
Keep thorough records of the problem analysis process. Document findings, insights, and any
assumptions made during this phase.

 Quantitative Analysis:
If applicable, use quantitative methods such as statistics or modeling to analyze data and quantify the
problem's impact.

 Qualitative Analysis:
Consider qualitative factors, such as human behavior, organizational culture, or subjective experiences,
that may be relevant to the problem.

 Problem Prioritization :
Assess the urgency and importance of the problem. Determine whether it requires immediate attention
or can be addressed over time.

 Risk Assessment:
Identify potential risks and consequences associated with the problem and its solutions. Evaluate the
potential impact of different courses of action.
3|Page
 Alternative Perspectives:
Encourage diverse viewpoints and alternative perspectives on the problem. This can lead to more
innovative and comprehensive solutions.

 Visualization :
Use visualization tools or techniques like diagrams, flowcharts, or mind maps to represent the problem
and its relationships visually.

Algorithm

o An algorithm is a step-by-step set of instructions for solving a particular problem or performing


a specific task. It provides a clear, unambiguous, and systematic approach to problem solving
o Algorithms are independent of programming languages and can be implemented in various
languages or pseudocode.
Characteristics of algorithms:

Finiteness: The algorithm must have a finite number of steps

Definiteness: Each step must be well-defined and unambiguous

Input: An algorithm typically takes some input data

Output: It produces a result or output based on the input and the steps performed

Effectiveness: It must be possible to execute each step in a finite amount of time

Problem 1: Find the area of a Circle of radius r

Algorithm:

Step1: Start

Step2: Read\input the Radius r of the Circle

Step3: Area= PI*r*r // calculation of area

Step4: Print Area

Step5: End

Problem2: Write an algorithm to read two numbers and find their sum

Algorithm:

4|Page
Step1: Start

Step2: Read\input the first num1.

Step3: Read\input the second num2.

Step4: Sum= num1+num2 // calculation of sum

Step5: Print Sum

Step6: End

Problem 3: Convert temperature Fahrenheit to Celsius

Step1: Start

Step 2: Read Temperature in Fahrenheit F

Step 3: C= 5/9*(F-32)

Step 4: Print Temperature in Celsius: C

Step5: End

Type of Algorithms

The algorithm and flowchart, classification to the three types of control structures. They are:

1. Sequence
2. Branching (Selection)
3. Loop (Repetition)
Problem4: write algorithm to find the greater number between two numbers [Branching
(Selection)]

Step1: Start

Step2: Read/input A and B

Step3: If A greater than B then C=A

Step4: if B greater than A then C=B

Step5: Print C

Step6: End

5|Page
Problem5: A algorithm to find the largest value of any three numbers.

Step1: Start

Step2: Read/input A,B and C

Step3: If (A>=B) and (A>=C) then Max=A

Step4: If (B>=A) and (B>=C) then Max=B

Step5:If (C>=A) and (C>=B) then Max=C

Step6: Print Max

Step7: End

Problem6: An algorithm to calculate even numbers between 0 and 99

1. Start

2. I ← 0

3. Write I in standard output

4. I ← I+2

5. If (I <=98) then go to line 3

6. End

Problem7: Design an algorithm which gets a natural value, n,as its input and calculates odd
numbers equal or less than n. Then write them in the standard output:

1.Start

2. Read n

3. I ← 1

4. Write I

5. I ← I + 2

6. If ( I <= n) then go to line 4

7. End

6|Page
Problem8: Design an algorithm which generates even numbers between 1000 and 2000 and then
prints them in the standard output. It should also print total sum:

1. Start

2. I ← 1000 and S ← 0

3. Write I

4. S ← S + I

5. I ← I + 2

6. If (I <= 2000) then go to line 3

else go to line 7

7. Write S

8. End

Problem9: Design an algorithm with a natural number, n, as its input which calculates the
following formula and writes the result in the standard output:

S = ½ + ¼ + … +1/n

1. Start

2. Read n

3. I ← 2 and S ← 0

4. S= S + 1/I

5. I ← I + 2

6. If (I <= n) then go to line 4

else write S in standard output

7. End

Flowchart

A flowchart is a graphical representation of an algorithm or a process. It uses various symbols and


shapes to depict different steps, decisions, and actions within a procedure. Flowcharts are a valuable

7|Page
tool for visually conveying the logic and structure of an algorithm or a process. Here are some common
symbols used in flowcharts

Advantages of flowcharts:

 Clarity And understanding:


Flowcharts visually represent processes, enhancing understanding, particularly for those unfamiliar
with the process.

 Simplified Communication
Serve as a universal language, transcending language barriers and aiding communication across diverse
backgrounds

 Identification of Bottlenecks:
Visualizing process flow helps identify slowdowns or bottlenecks, facilitating optimization for
improved efficiency.
8|Page
 Documentation
Function as clear documentation for processes, offering a concise representation for analysis,
understanding, and improvement.

 Problem solving
Aid in systematic analysis of steps, making it easier to pinpoint errors and devise solutions during
troubleshooting.

 Decision Making
Valuable tools for decision-making, allowing visualization of consequences and aiding in strategic
planning within a process.

 Standardization
Serve as standards for processes, ensuring consistency and guiding individuals to follow the same set of
steps

 Training and Onboarding


Useful for training and onboarding by providing a step-by-step guide for individuals new to a process.

 Process Improvement
Facilitate continuous improvement by highlighting areas for enhancement, particularly in quality
management and Six Sigma methodologies.

 Visualization of alternative paths


Allow representation of decision points and alternative paths in a process, useful in scenarios with
different actions based on specific conditions.

Main rules for creating an effective flowchart

 Start and end with terminals:


Begin and conclude the flowchart with oval-shaped terminal symbols labeled "Start" and "End”.

 Use Rectangles for Processes:


Represent each task or action with a rectangle, clearly labeled with a brief description.

 Diamonds for Decisions:


Use diamonds to denote decision points, labeling them with questions and indicating possible outcomes
with arrows.

 Connect with Arrows:


9|Page
Connect symbols using arrows to indicate the flow of the process.

 Keep it on One page:


Ideally, ensure the flowchart fits on a single page; if not, use connector symbols for continuity.

 Maintain logical Flow


Ensure a clear and logical sequence from start to end.

 Use straight lines


Keep lines straight for readability and organization.

 Label clearly
Clearly label symbols, arrows, and additional information.

 Connector symbols for complexity


If complex, use connector symbols to link different parts of the flowchart.

 Review for Accuracy:


Regularly review the flowchart for accuracy, eliminating errors or ambiguities.

 Keep it simple:
Aim for simplicity to ensure easy understanding at a glance.

 Test with others


Have someone unfamiliar with the process review the flowchart for clarity and potential improvements.

Problem 1: To draw the flow chart the area of a Circle

Problem 2: To draw the flow chart to Convert temperature Fahrenheit to Celsius


10 | P a g e
Problem3: To draw the flow chart to find the greater number between two numbers

Problem4: A algorithm to find the largest value of any three numbers

11 | P a g e
Problem5: An algorithm to calculate even numbers between 0 and 100

Problem6: Design an algorithm which gets a natural value, n,as its input and calculates odd
numbers equal or less than n. Then write them in the standard output:

12 | P a g e
Example of a flowchart for the same "FindMax" algorithm mentioned earlier:

The History of C Language

Here is a brief history of C language that also showcases the hierarchy of programming languages
before C came into existence:

 The development of the C programming language can be traced back to its parent node,
ALGOL 60 (algorithmic language), which was introduced in 1960 as the first computer
programming language to use a block structure.

 From ALGOL, Martin Richards developed a type less language called BCPL (Basic Combined
Programming Language). This was also a high-level computer language

 In 1970, Ken Thompson created a language using BCPL called B, which was also type less.

 Then, Dennis Ritchie at AT&T Bell Labs took the development of C into his hands. Using the
foundations laid down by previous languages BCPL and B, he developed the C programming
language in 1972.

 C was initially used to re-implement the Unix operating system, which was rewritten in C from
assembly language, and it quickly gained popularity for its portability and efficiency. So,
the development of the Unix operating system started in 1969. And in the year 1972, its code
was rewritten in the C programming language.

 In 1985, windows 1.0 was released, and while its source code is not publicly available, it has
been stated that its kernel is mostly written in K programming language.

13 | P a g e
 Linux, a popular operating system, started its development in 1991 using C programming
language and Lisp. After almost a year of development, it was released under the GNU license
and was used as part of the GNU operating system.

 Oracle, the most widely used relational database management system (RDBMS), began its
development in 1977. In 1983, its code was rewritten from assembly language to C
programming language.

The C language was designed as a general-purpose programming language that helps in system
programming as well as application programming.

Feature of C programming Language;

14 | P a g e
Basic Structure of C Program

The components of the basic structure of a C program consists of 7 parts

1. Document section

2. Preprocessor/link Section

3. Definition section

4. Global declaration section

5. Function declaration section

6. Main function

7. User-defined function section

15 | P a g e
How Does C Programming Works?

C language is a compiled programming language that is widely used for system programming,
embedded systems, and other low-level applications. The process of creating and executing a C
program involves several steps, including preprocessing, compiling, linking, and executing.

16 | P a g e
Creating
the Program:

The first step in using C is to create the program. This involves writing the source code in a text editor
or integrated development environment (IDE). The source code is a raw set of instructions that define
the behavior of the program.

Preprocessing:
Once the program has been created, it needs to be preprocessed. This step includes running the source
code through a preprocessor, which is a program that performs certain operations on the code before it
is compiled. These operations may include inserting header files, expanding macros, performing
conditional compilation, etc.

Compiling the Program:

17 | P a g e
After preprocessing, the program must be compiled. The compilation process consists of translating
the source code into machine code that can be executed by the computer's processor. The compiler
reads the source code and generates an object file, which contains the compiled code for the program.

Linking the Program with Functions from the C Library:


After the compilation process, the program needs to be linked with functions from the C library. The C
library is a collection of prewritten functions that can be used by C programs. Linking is the process of
combining the object file generated by the compiler with the necessary functions from the C library to
create an executable program.

Executing the Program:


The final step is to execute the file. The executable file created in the previous step can be run by the
operating system, which loads the program into memory and begins executing it. The program will
then perform its defined operations until it terminates or is interrupted.

In conclusion, the process of creating and executing a C program involves several steps, including
preprocessing, compiling, linking, and executing. Understanding these steps is crucial for developing
efficient and effective C programs.

Testing

o Testing is a crucial phase in software development to ensure the correctness and reliability of a
C program
o Types of testing include unit testing (testing individual functions), integration testing (testing the
interaction between functions/modules), and system testing (testing the entire program
o Testing involves creating test cases, running the program with various inputs, and verifying that
it produces the expected outputs
o Tools like debugging, assertions, and testing frameworks can aid in testing C programs
Documentation

o Documentation is essential to make code understandable and maintainable


o Types of documentation include comments, code comments (to explain code logic), and
external documentation (user manuals or developer guides)
o Commenting code helps other developers understand your code, and it can also serve as a
reference for future work
o Writing clear, concise, and well-structured comments is a best practice in C programming
18 | P a g e
Preprocessor

o The preprocessor is a tool that processes the code before actual compilation
o Preprocessor directives start with # and include #include, #define, # ifdef and others
o # include is used to include header files, providing declarations of functions and data types
o # define is used to create macros, which are essentially symbolic constants or code fragments
o The preprocessor simplifies code maintenance and helps manage code modifications
Macros

o Macros are defined using #define and are replaced with their values during preprocessing
o They can be used for defining constants (e.g., #define PI 3.1415) or for creating code shortcuts
o Macros are commonly used in conditional compilation (e.g., # ifdef and #ifndef ), where code is
included or excluded based on certain conditions

Check Your Progress

A very short question

1.What is the first step in problem solving with a computer?


The first step is problem identification.
2.Why is data gathering essential in the problem-solving process?
Data gathering provides crucial information for analysis, aiding in informed decision-making during
the problem-solving process.
3.What is the purpose of algorithm design in problem solving?
Algorithm design provides a systematic and efficient approach to processing data, forming the
foundation for effective problem-solving.
4.Why is optimization important in computer problem solving?
Optimization enhances efficiency by improving algorithms or code, making the problem-solving
process more effective and resource-efficient.
5.What is the final step in the problem-solving process if the solution is intended for practical
use?
The final step is deployment, where the solution is implemented in a real-world environment for
practical use.
6.What is an algorithm?

19 | P a g e
An algorithm is a step-by-step set of instructions for solving a particular problem or performing a
specific task
7.Why are algorithms considering independent of programming language?
Algorithms provide a language-agnostic, high-level description of a solution, making them adaptable to
various programming languages.
8.What is the significance of effectiveness in algorithm?
Effectiveness in algorithms implies that each step can be executed in a finite amount of time, ensuring
the practical feasibility and efficiency of the solution.
9.What is the primary purpose of a flow chart?
The primary purpose of a flowchart is to visually represent the steps and logic of a process or
algorithm, providing a clear and structured way to understand and communicate the flow of activities
10.Name one advantage of using flow chart for communication?
Flowcharts serve as a universal language, transcending language barriers and aiding communication
across diverse backgrounds, ensuring a common understanding of processes.
11.Why is it recommended to start and end a flow chart with terminal?
Starting and ending a flowchart with terminals, represented by oval shapes, establishes clear entry and
exit points for the process, making it easy to identify where the flow begins and concludes

B short questions
1.Explain the step involved in algorithm design.
Algorithm design encompasses several key steps:

a. Problem identification: Clearly define the problem.

b. Data Gathering: Collect relevant data for analysis.

c. Analysis: Understand the problem and gain insights.

d. Algorithm Design: Develop step-by-step procedures.

e. Programming: Implement the algorithms in code.

f. Testing and Debugging: Ensure the code works correctly.

g. Optimization: Improve efficiency if needed.

h. validation: Confirm the solution meets requirements.

20 | P a g e
I. Deployment: Implement the solution in a real-world setting.

j. Maintenance: Continuously monitor and improve the solution

2.What are the key activities in the testing and debugging phase of problem solving?

During testing and debugging:

a. Test code: Apply various inputs to assess functionality.

b. Identify Error: Detect and locate bugs or issues.

c. Debugging: Fix errors to ensure correct code execution.

d. scenario Testing: Assess code performance in different scenarios.

e. Optimization: Enhance code efficiency if needed

3.How does problem decomposition contribute to a more manageable problem-solving process?

Problem decomposition involves breaking down complex problems into smaller, more manageable
components. This:

a. simplifies Analysis: Focusing on smaller parts is more straightforward.

b. Facilitates solution: Solving smaller components is easier.

c. improves understanding: Individual components are easier to comprehend.

d. Enhances Collaboration: Different team members can work on sub-problems.

e. Aid Reusability: Solutions to smaller problems can be reused

4.why is risk assessment an important aspect of problem analysis?

Risk assessment is crucial because:

a. informs Decision Making: Identifies potential issues and consequences.

b. Guides Planning: Helps plan for mitigation and risk management.

c. Prevents Surprises: Anticipates challenges before they arise

d. Ensure Preparedness: Enables proactive problem-solving.

e. Improve Decision confidence: Informed decisions lead to more confidence

5.Describe the significance of visualization tools in problem analysis?


21 | P a g e
Visualization tools, like diagrams and flowcharts, play a vital role in problem analysis by:

a. Enhance understanding: Visual representation aids in grasping complexities.

b. facilitating Communication: Visuals transcend language barriers.

c. Supporting Decision Making: Visualization clarifies consequences and relationships.

d. Service as Documentation: Offers clear representation for analysis and improvement.

e. Promoting Standardization: Provides a standardized way to represent processes

Exercise

1. What is the primary goal of problem analysis in computer programming?


2. Explain the key steps involved in problem analysis?
3. Differentiate between an algorithm and a flowchart
4. How does a flowchart contribute to the clarity of an algorithm?
5. Briefly outline the historical background of the C programming language?
6. What motivated the development of the C programming language?
7. Explain the essential components of a C program
8. Why is it necessary to include a "main" function in a C program
9. Define debugging in the context of programming
10. Describe the steps involved in compiling and executing a C program?
11. Why is testing an integral part of the software development process?
12. What is the role of the preprocessor in C programming?
13. Explain how macros are used in C programs?
14. Write an algorithm to find average age of a group of 10 players?
15. Write algorithm to this problem: Ram goes to market for buying some fruits and vegetables. He
is having a currency of Rs 500 with him for marketing. From a shop he purchases 2.0 kg Apple
priced Rs. 50.0 per kg, 1.5 kg Mango priced Rs.35.0 per kg, 2.5 kg Potato priced Rs.10.0 per
kg, and 1.0 kg Tomato priced Rs.15 per kg. He gives the currency of Rs. 500 to the shopkeeper.
Find out the amount shopkeeper will return to Ram. and also tell the total item purchased.
16. To draw flow chart and algorithm to Find factorial of N?
17. Explain steps involve in drawing of a flowchart.
18. Explain uses of Flowchart.
19. Draw a flowchart to find the sum of first 100 natural numbers.
20. Draw a flowchart to find the largest of three numbers x, y and z.

22 | P a g e
21. Draw flowchart for the problem of determining prime number?
22. Draw a flowchart which generates first 50 items of the Fibonacci series: 1, 1, 2, 3, 5, 8,…?
23. Design an algorithm to convert a decimal number, n, to binary format?

23 | P a g e
24 | P a g e

You might also like