Programming for Problem Solving using C

Programming for Problem Solving

Page 1

  • Faculty Name: Ms. Gunjan

  • Branch: B.Tech(CSE)

  • Date: 07/08/2024

  • Subject Name: Programming for Problem Solving in C

Page 2: Contents

  1. UNIT 1

    • Q1: What is a problem?

    • Q2: What do you mean by computer language?

    • Q3: Explain programming language with its types.

    • Q4: Name some program planning tools.

    • Q5: Explain about algorithms.

    • Q6: What are the steps to solve problems in a computer system?

    • Q7: What are the characteristics of an algorithm?

    • Q8: Write an algorithm to check whether a given number is prime or not.

    • Q9: Explain about a flowchart.

    • Q10: Explain the symbols in a flowchart.

    • Q11: Write an algorithm and flowchart for swapping two numbers.

    • Q12: Explain the programming languages.

    • Q13: Explain the history of C.

    • Q14: What is the general structure of a C program and explain with an example?

    • Q15: What is a variable and rules for constructing a variable?

    • Q16: How to declare and initialize a variable?

    • Q17: Explain data types in C.

    • Q18: Describe different types of constants in C with example.

    • Q19: What do you mean by reserved words/keywords in C?

    • Q20: Difference between local and global variables in C.

    • Q21: Discuss types of type conversions/typecasting in C with example.

    • Q22: List built-in type casting functions.

    • Q23: Explain header files in C.

    • Q24: Describe standard header files in C.

    • Q25: Examples to show the use of standard header files in C.

    • Q26: Describe non-standard header files in C.

    • Q27: Explain basic input and output functions.

    • Q28: Explain formatted and unformatted I/O functions with examples.

    • Q29: Explain compilation in C.

    • Q30: Explain errors in C with types.

    • Q31: Differentiate between source code and object code.

    • Q32: What is an operator? List different categories of C operators.

  2. UNIT II (starting from page 61)

Page 3:

UNIT I Definition of Problems.
  • Problem: Defined as the objective or specific output we aim to attain through a sequence of steps and activities with specified input.

  • Examples:

    • Kids wanting sweets

    • Students conducting science experiments

    • Mechanics repairing cars

    • Doctors studying bacteria

Steps in Problem Solving:
  1. Identify the problem

  2. Understand the problem

  3. Identify alternative solutions

  4. Select the best solution

  5. List instructions using the selected solution

  6. Evaluate the solution

Page 5:

Computer Language:
  • Definition: Formal language used to instruct a computer.

  • Types:

    • Construction language

    • Command language

    • Configuration language

    • Programming language

    • Scripting language

    • Query language

    • Data exchange language

    • Markup language

    • Modeling language

    • Architecture description language

    • Hardware description language

    • Page description language

    • Simulation language

    • Specification language

    • Style sheet language

Page 6:

Programming Language and Types:
  • Programming Language: Set of grammatical rules that instruct a computer or device to behave in a certain way.

  • Types:

    • Low-level

      • Assembly Language: Basic instructions; one level above machine code.

      • Machine Language: Binary (1s and 0s); understood directly by computer’s processing unit.

    • High-level: Designed for ease of use, consisting of languages like C, C++, JAVA.

Page 10:

Flowchart:
  • Definition: A visual representation of a problem-solving process, using symbols to denote different types of instructions.

  • Purpose:

    • Provides communication

    • Offers an overview

    • Checks program logic

    • Facilitates coding

    • Aids in documentation and debugging

Page 12:

Characteristics of Algorithms:
  • Properties:

    • Unambiguous: Each step must be clear

    • Input: Zero or more inputs supplied

    • Output: At least one output produced

    • Definiteness: Each instruction is clear

    • Finiteness: Terminates in a finite number of steps

    • Effectiveness: Operations can be performed

Page 15:

Example Algorithms:
  • Prime Number Check Algorithm:

    1. Start

    2. Read number n

    3. Set f=0

    4. For i from 2 to n-1

    5. If n mod i=0 then, set f=1 and break

    6. If f=0 then print 'The number is prime' else print 'not prime'

    7. Stop

  • Swapping Two Numbers Algorithm:

    • Using third variable:

Page 20:

Structure of a C Program:

  1. Documentation Section

    • Comment lines containing program details

  2. Link Section

    • Includes necessary libraries (e.g., #include<stdio.h>)

  3. Definition Section

    • Defines symbolic constants (e.g., #define A 10)

  4. Global Declarations Section

    • Declares variables to be used across functions

  5. Main Function Section

    • Main part of the program

    • Consists of declaration and executable parts

  6. Subprograms Section

    • Contains user-defined functions called in main()

Page 23:

Variable in C:
  • Definition: A symbol that stores data values.

  • Rules for Naming:

    • First character must be a letter or underscore

    • Can include letters, digits, and underscores

    • Length should not exceed specified limits

Page 24:

Data Types in C:

  • Classes of Data Types:

    1. Primary Types: int, char, float, double

    2. User-defined Types: defined by the programmer

    3. Derived Types: arrays, pointers, etc.

    4. Empty Type: void

Page 29:

Constants in C:
  • Types include:

    1. Integer Constants

    2. Real Constants

    3. Character Constants

    4. String Constants

Page 31:

Keywords in C:

  • Categorized as reserved words with predefined meanings. Example: int, return, void, etc.

Page 32:

Type Conversion in C:

  • Implicit Conversion: Automatic data type conversion without programmer intervention.

  • Explicit Conversion: Manual conversion performed by the programmer using cast operators.

Page 36:

Header Files:

  • Usage: Provides access to standard libraries.

  • Examples: stdio.h, stdlib.h etc.

Page 41:

Input and Output Functions:

  • printf(): Displays output on the screen.

  • scanf(): Takes input from the user.

Page 47:

Compilation & Errors:

  • Compilation Process: Series of steps converting source code into machine code, including preprocessing, compiling, assembling, and linking.

  • Common Errors: Syntax, runtime, linker errors, and logical errors.

Page 53:

Operators:

  • Arithmetic, relational, logical, and assignment operators are examples used in C programming to manipulate data.

Page 61:

UNIT II

Key Topics:

  • Expression evaluation, precedence and associativity of operators

  • Control statements: Conditional and looping

  • Storage classes

  • Functions and recursion

Page 62:

Conditional Statements:
  • if, else if, switch: Used for executing different sections based on conditions. Example syntax and use case provided.

Page 67:

Loops:

  • Types: for, while, do-while loops.

  • Example Programs: Show implementation of loops with outputs.

Page 85:

Arrays:

  • Definition, types (1D and 2D arrays), and operations performed on them.

  • Example Programs: Demonstrations of array initialization, manipulation, and application use cases.

Page 92:

Strings in C:

  • Explanation of character strings as arrays and manipulation functions.

  • Example Programs: Demonstrating string handling operations.

Page 100:

Pointers in C:

  • Definition, declaration, and operations using pointers.

Page 104:

Miscellaneous Topics:

  • Union in C: Overview and example program.

  • File Handling: Concepts and file operations.

Page 121:

Pseudocode and Flowchart:

  • Discussion on how to represent algorithms with pseudocode and flowcharts, including examples.

Page 125:

Conclusion:

The structure of the notes comprehensively covers the fundamentals of programming for problem-solving using C, focusing on various constructs, data handling, storage mechanism, and coding practices. It serves as an effective guide for students preparing for exams.