Programming in "C"
Lecture I

CSC 105
Dr. Sorin Draghici
Wayne State University

Introduction

The "C" programming language
Why "C" ?
Summary of Course

How do we solve a problem?
What is an algorithm?
What are the main steps of writing a program?

Vocabulary

Glossary of terms

Algorithm
Pseudo-code
Program development cycle
Source code, object code, executable code
Libraries

How do we solve a problem?

We must go through the following steps:
Understand the problem
Solve (manually) an instance of the problem
Describe the solution in natural language
Write the pseudo-code

Pseudo-code - a set of instructions described in natural language but in a more formal way

Pseudo-code

if (condition) then
instruction
else
instruction

variable = value or variable = expression

for ( counter = initial_value to final_value using step )
instruction

while (condition) do
instruction

do
instruction
while (condition)

Example

There is a list with names and phone numbers:
Find John Smith's phone number
Solving the problem:
  1. Understand the problem
  2. Solve an example: (John Doe, 577-5675; Mary DelMonte, 577-3432; John Smith, 577-8493)
  3. describe the solution:
    Read the list sequentially until you find the name John Smith; output the phone number and stop; if the list is finished and the name was not found, report the failure.
  4. Describe the solution in pseudo-code:

    read the name to be searched; store it in target_name
    while (there are name in the list) do
    read a name
    if ( name = target_name)
    print the corresponding phone number
    return
    end_while
    print "name not found"
    return

Exercise

Solve the following problem:
Given a list with names and phone numbers, write a program in pseudo-code which is able to find look-up a phone number and find the corresponding person.
Example: find the person whose phone number is: 577-8492.

What is an algorithm?

An algorithm is a 'recipe' for solving a specific problem; it is a sequence of steps which, if followed, will produce the desired output.

Example:

Scrambled eggs ( Input: 2 eggs, 50 mg. of oil. Output: hypercaloric dish called 'scrambled eggs') :

Take 2 eggs
Break shells and put their content in a pan containing 50mg of oil
Stir with a fork while cooking for 5 minutes

Main steps of writing a program

From pseudo-code to executing:

source code -> compiler -> assembler -> assembler -> object code -> linker (libraries) -> executable


The program development cycle

There are seven steps:

Define the program objectives (specifications)
Design the program (design)
Write the code (implementation or coding)
Compile
Run
Test and debug (testing)
Maintain and modify (maintenance)

Summary

We now know:
How to solve a problem
What an algorithm is
What the main steps of writing a program are

Reading

Textbook: New C Primer Plus, SAMS Publishing
Chapter 1 (pgs. 1-22)
Chapter 2 (pgs. 23-43)

Please pay attention to:
Review questions
Programming exercises