Sie sind auf Seite 1von 16

CC213 Programming

Applications

Week #1

Software Development
Method
1.
2.
3.
4.
5.
6.

Specify problem requirements


Analyze the problem
Design the algorithm to solve the problem
Implement the algorithm
Test and verify the completed program
Maintain and update the program

Steps Defined
1. Problem - Specifying the problem requirements
forces you to understand the problem more clearly.
2. Analysis - Analyzing the problem involves identifying
the problems inputs, outputs, and additional
requirements.
3. Design - Designing the algorithm to solve the
problem requires you to develop a list of steps
called an algorithm that solves the problem and
then to verify the steps.
4. Implementation - Implementing is writing the
algorithm as a program.
5. Testing - Testing requires verifying that the program
actually works as desired.
6. Maintenance - Maintaining involves finding
previously undetected errors and keep it up-todate.
3

Converting Miles to
Kilometers

1. Problem: Your boss wants you to convert a


list of miles to kilometers. Since you like
programming, so you decide to write a
program to do the job.
2. Analysis
We need to get miles as input
We need to output kilometers
We know 1 mile = 1.609 kilometers

3. Design

1. Get distance in miles


2. Convert to kilometers
3. Display kilometers

4. Implementation

C Program structure

Function Header

Function Body

Identifiers Declarations

o An identifier must consist only of letters, digits,


and underscores.
o An identifier cannot begin with a digit.
o A C reserved word cannot be used as an
identifier.
o A standard identifier should not be redefined.
o C compilers are case sensitive. (Rate, rate and
RATE are viewed as different identifiers)

Valid identifiers: letter1, inches, KM_PER_MILE


Invalid identifiers: 1letter, Happy*trout,return
7

Giving a Value to a Variable

Output Function
SYNTAX
printf( format string , print list ) ;
Printf(format string);

Examples :

Place holder

printf(That equals %f kilometers. \n, kms);


printf(enter the distance in miles> );
printf( Hello, World?\n);

Escape sequence
9

Output Function

10

Input Function
SYNTAX
scanf( format string , input list ) ;

Examples :

Place holder

scanf(%lf, &miles);
Ampersand
11

Input Function

12

Programming Examples
Example-1
Write a program to ask the user for the width
and length of a piece of land and then tell him
how many orange trees he can grow on it.
Given that each orange tree requires 4 m2.

13

Programming Examples
#include <stdio.h>
# define one_tree_space 4
int main()
{
int length,width, area, no_of_tree;
printf(Enter length of the land> );
scanf(%d, &length);
printf(Enter width of the land> );
scanf(%d, &width);
area = length * width;
no_of_tree = area / one_tree_space;
printf(The available number of trees is %d
trees\n, no_of_tree);
}

return(0);
14

Arithmetic Operators
Division

the result of the division operator depends on the


type of its operands
if one or both operands has a floating point type,
the result is a floating point type. Otherwise, the
result is an integer type
Examples

11 / 4
11.0 / 4.0
11 / 4.0
15 / 3
16 / 3

has value 2
has value 2.75
has value 2.75
has value 5
has value 5

15

Arithmetic Operators
Modulus
the modulus operator % can only be used
with integer type operands and always
has an integer type result
its result is the integer type remainder of an
integer division
EXAMPLE
3%5=3 5%3=2 4%5=4 5%4=1
5%5=0 6%5=1 7%5=2 8%5=3
15 % 6 = 3 15 % 5 = 0

16

Das könnte Ihnen auch gefallen