1 Problem Solving With Computer

C Programming · Unit 1 · 2 hrs

Problem Solving with Computer

Exam-focused notes for Problem Solving with Computer (C Programming, CSC115): what the TU syllabus asks and how it has actually been tested, with 6 solved past questions from this unit.

Problem Solving with Computer in short

This chapter traces how a problem becomes a working C program: analysing what is being asked, designing an algorithm and flowchart, writing and compiling the source code, and then debugging, testing and documenting it, before looking at where the C language itself came from and how a C program is structured.

Problem Solving with Computer: notes

Problem Analysis

Before any code is written, the problem itself must be understood completely. Problem analysis is the process of breaking a problem into clear, separate parts so that a solution can be designed systematically rather than by trial and error. It has four concerns: understanding exactly what is being asked, identifying the inputs (the data that is given or will be supplied), identifying the outputs (the result that is expected), and identifying any constraints or special conditions the solution must respect. Once these are clear, the programmer defines the process, the sequence of operations that converts the input into the required output. Jumping straight into coding without this analysis is one of the most common causes of a wrong or incomplete solution, since a program built on a misunderstood problem will run without errors and still give the wrong answer.

Algorithms and Flowchart

An algorithm is a finite, step by step set of instructions for solving a problem. To qualify as a good algorithm it must satisfy five properties: finiteness (it terminates after a finite number of steps), definiteness (each step is stated unambiguously), input (it accepts zero or more inputs), output (it produces at least one output), and effectiveness (each step is simple enough to be carried out exactly). For example, an algorithm to find the larger of two numbers a and b can be written as:

  1. Start.
  2. Read a and b.
  3. If a > b, print a; otherwise print b.
  4. Stop.

A flowchart is the graphical equivalent of an algorithm: a diagram that uses standard shapes joined by arrows to show the flow of control from start to end. An oval marks the start or stop of the process, a parallelogram marks an input or output step, a diamond marks a decision point, and a rectangle marks an ordinary processing step. The algorithm above corresponds to the flow

Start -> Read a, b -> is a > b? --Yes--> Print a -> Stop
                              --No--> Print b -> Stop
SymbolShapeMeaning
TerminalOvalStart / Stop
Input-OutputParallelogramInput / Output
DecisionDiamondDecision
ProcessRectangleProcessing step

What this unit covers

  • Problem analysis
  • Algorithms and Flowchart
  • Coding
  • Compilation and Execution
  • History of C
  • Structure of C program
  • Debugging, Testing and Documentation

Structure of C program

2081 · 5 marks

Explain the basic structure of C Programming. [5]

Every C program follows a standard, well-defined structure. Understanding this structure is essential before writing any C program. --- - Contains comments that describe the purpose of the program, author name, date, etc. - Comments are written between / and / for multi-line, or // for single-lin...

Open this answer on its own page →

2074 · 10 marks

Discuss structure of a C program with suitable example.[10]

Show solved answer

Structure of a C Program

Introduction

Every C program follows a standard, well-defined structure. Understanding this structure is essential because the compiler processes each section in a specific order. A well-structured C program is easier to read, debug, and maintain.


Sections of a C Program

A C program is typically divided into the following sections:

1. Documentation Section

  • Contains comments that describe the purpose of the program, author name, date, etc.
  • Comments are ignored by the compiler.
  • Single-line comment: // comment
  • Multi-line comment: /* comment */
/* Program to add two numbers
   Author: Ram
   Date: 2081 */

2. Preprocessor Directives

  • Lines that begin with # are processed before compilation.
  • #include is used to include header files (libraries).
  • #define is used to define constants or macros.
  • These directives make library functions like printf() and scanf() available to the program.
#include <stdio.h>
#define PI 3.14

3. Global Declarations

  • Variables or functions declared outside all functions are called global.
  • They are accessible throughout the entire program.
int count = 0;   /* global variable */

4. main() Function

  • The main() function is the entry point of every C program.
  • Execution always begins from main().
  • It returns an integer value to the operating system.
  • return 0 signals that the program ended successfully.
  • Curly braces { } define the body of the function.
  • Every statement inside must end with a semicolon (;).
int main()
{
    /* statements */
    return 0;
}

5. User-Defined Functions

  • Functions written by the programmer to perform specific tasks.
  • They are defined outside main() but can be called from main() or other functions.
  • They help in making the program modular and reusable.
int add(int x, int y)
{
    return x + y;
}

Complete Example

The following example demonstrates all sections of a C program:

/* Program to add two numbers and display the result */   /* 1. Documentation */

#include <stdio.h>                                        /* 2. Preprocessor directive */

int result;                                               /* 3. Global declaration */

int add(int x, int y);                                    /* Function prototype */

int main()                                                /* 4. main() function */
{
    int a, b;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);
    result = add(a, b);
    printf("Sum = %d\n", result);
    return 0;                                             /* Signals successful end */
}

int add(int x, int y)                                     /* 5. User-defined function */
{
    return x + y;
}

Sample Output

Enter two numbers: 5 3
Sum = 8

Explanation of Key Elements

ElementPurpose
#include <stdio.h>Includes standard I/O library for printf() and scanf()
int main()Starting point of the program; returns integer to OS
{ }Curly braces define the body of a function
return 0Signals the program ended successfully
;Every statement must end with a semicolon
scanf()Reads input from the user
printf()Displays output to the screen

How a C Program Runs

Once the source code is written following the above structure, it goes through the following stages before producing output:

source.c  -->  Compiler  -->  source.obj  -->  Linker  -->  source.exe  -->  Output
  1. Compilation: The compiler translates source code into object code and catches syntax errors.
  2. Linking: The linker combines object code with library functions (like printf, scanf) to produce an executable file.
  3. Loading: The loader places the executable into memory.
  4. Execution: The CPU runs the program and produces output.

Summary

A well-structured C program contains five main sections: documentation, preprocessor directives, global declarations, the main() function, and user-defined functions. Following this structure makes programs organized, readable, and easy to maintain. The main() function is mandatory in every C program as it serves as the entry point for execution.

Open this answer on its own page →

History of C

2078 · 5 marks

Write short notes on: History of C, Bitwise operator. [5]

--- C is a general-purpose, structured programming language. The following points outline its historical development: - 1960 - ALGOL (Algorithmic Language) was developed. It introduced the concept of structured programming and served as the base for later languages. - 1967 - BCPL (Basic Combined ...

Open this answer on its own page →

Algorithms and Flowchart

2077 · 5 marks

Explain flowchart with example. What are the benefits of using flowchart? What are the benefits of using flowchart? [3+2]

(a) Flowchart with Example A flowchart is a diagram that represents an algorithm using standard symbols connected by arrows. It shows the flow of control from start to end in a visual, step-by-step manner. Symbol Shape Purpose ------------------------ Terminal Oval Start / Stop Input/Output Paral...

Open this answer on its own page →

2074 · 5 marks

What is algorithm? How is it different from flow chart? [2+3]

Show solved answer

Algorithm and Flowchart

What is an Algorithm? [2 marks]

An algorithm is a finite, step-by-step set of instructions to solve a problem. It must be clear, unambiguous, and always terminate.

Properties of a Good Algorithm:

PropertyDescription
FinitenessMust end after a finite number of steps
DefinitenessEach step must be clear and unambiguous
InputHas zero or more inputs
OutputProduces at least one output
EffectivenessEach step must be simple enough to be carried out

Example: Algorithm to find the largest of two numbers

Step 1: Start.
Step 2: Read a and b.
Step 3: If a > b, print a. Else print b.
Step 4: Stop.

Difference Between Algorithm and Flowchart [3 marks]

A flowchart is a diagram that represents an algorithm using standard symbols connected by arrows. It shows the flow of control from start to end.

BasisAlgorithmFlowchart
NatureWritten in plain text or pseudocode as numbered stepsRepresented as a diagram using standard symbols and arrows
FormTextual / verbal descriptionVisual / graphical representation
SymbolsNo special symbols requiredUses standard symbols (oval, rectangle, diamond, parallelogram, etc.)
Ease of UnderstandingEasier to write but may be harder to follow for complex logicEasier to understand visually, especially for branching and loops
ModificationEasy to modify by editing textHarder to modify as the diagram must be redrawn
PurposeDescribes the logic of a solution in stepsDepicts the flow of control from start to end graphically

Key Relationship

An algorithm and a flowchart both serve the same purpose: to plan and represent the solution to a problem before coding. The algorithm is the textual form, while the flowchart is its graphical equivalent. A flowchart is essentially a visual translation of an algorithm.

Open this answer on its own page →

Compilation and Execution

2077 · 5 marks

Write short notes on: Compilation and execution, Operator precedence and associativity. [5]

--- A computer only understands machine language (binary). Source code written in C (a high-level language) must be converted before it can run. This conversion happens in the following stages: Stage Description ------------------------- Compilation The compiler reads the source code (source.c) a...

Open this answer on its own page →