CC213 Programming Applications Week #1
Software Development Method 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 Problem - Specifying the problem requirements forces you to understand the problem more clearly. Analysis - Analyzing the problem involves identifying the problem’s inputs, outputs, and additional requirements. 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. Implementation - Implementing is writing the algorithm as a program. Testing - Testing requires verifying that the program actually works as desired. Maintenance - Maintaining involves finding previously undetected errors and keep it up-to-date.
Converting Miles to Kilometers 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. Analysis We need to get miles as input We need to output kilometers We know 1 mile = 1.609 kilometers Design Get distance in miles Convert to kilometers Display kilometers
4. Implementation
C Program structure Function Header Function Body
Identifiers Declarations An identifier must consist only of letters, digits, and underscores. An identifier cannot begin with a digit. A C reserved word cannot be used as an identifier. A standard identifier should not be redefined. 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
Giving a Value to a Variable
Output Function SYNTAX Examples : printf( format string , print list ) ; Printf(format string); Place holder printf(“That equals %f kilometers. \n”, kms); printf(“enter the distance in miles> ”); printf( “Hello, World?\n“); Escape sequence
Output Function
Input Function SYNTAX Examples : scanf( format string , input list ) ; Place holder scanf(“%lf”, &miles); Ampersand
Input Function
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.
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); }
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 has value 2 11.0 / 4.0 has value 2.75 11 / 4.0 has value 2.75 15 / 3 has value 5 16 / 3 has value 5
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