View
214
Download
1
Category
Preview:
Citation preview
Goals of this course
• Introduction to the programming language C
• Learn how to program
• Learn ‘good’ programming practices
Programming
What is computer programming?
• Representation of a task or algorithm in a computer language.
What is an algorithm?
• A sequence of directions for accomplishing a task.
Example of an Algorithm
The algorithm for calculating the arithmetic mean(i.e., the average) of a set of numbers is1. add all the numbers together2. divide this sum by the number of numbers
In mathematical terms this is written as
Levels of Abstraction
Suppose a student is asked to come to the front of the class
• high-level – stand, walk to front
• mid-level – stand, turn 90◦ to the right, walk 15 feet, turn 90◦ to the left, walk 10 feet
• low-level – contract specific muscles in a specific order
• lowest-level – you think about walking, initiating many electrochemical reactions
Levels of Abstraction cont.
Computer languages that correspond to these levels of abstraction:
• high-level – perl, python, matlab
• mid-level – C
• low-level – assembly language
• lowest-level – machine code
Analyzing a Problem
Computers understand concrete steps, not abstract concepts.
To determine the concrete steps involved in solving a problem, we may
• Represent the problem using pseudocode
• Work through the process using a simpler or smaller version of the problem
Analyzing a Problem cont.
Example: You must sort 1000 numbers in ascending order.
Simpler version: Sort 3, 2, 4, 1 in ascending order
Pseudocode:
Basic Concepts
Much of programming consists of the following:
• Storing and updating information – Variables and Statements
Example: store the answer given by a user
• Making decisions – ConditionalsExample:if temperature > 150 degrees
print ‘‘It’s too hot!’’
• Repeating certain tasks – LoopsExample: calculate the first 1000 prime numbers
Basics of CExample Code
#include<stdio.h> #define PI 3.14159
/* Finds the area and circumference of circle with radius r */ main ( void ) { double radius, /* radius of circle */ circumference, /* circumference of circle */ area; /* area of circle */
/* get radius of circle */ printf( “Please enter radius: “ ); scanf( “%lf”, &radius );
/* compute area and circumference */ area = PI * radius * radius; circumference = 2 * PI * radius;
/* display the result */ printf(“The area of the circle is %f and its circumference is %f\n”,
area, circumference); } /* main */
Basics of CForm of a Simple C Program
Preprocessor directives
main ( void )
{
declarations
executable statements
}
Basics of CPreprocessor Directive
#include <standard header file>
The C language includes only a minimal set of operations
Many useful functions and symbols are contained in libraries
Each library has a standard header file which has a name ending in .h
The #include instruction gives a program access to the corresponding library
Example: #include <stdio.h>
• gives access to (among others) the library functions scanf and printf which allow reading input from the keyboard and writing to the monitor
Basics of CPreprocessor Directive
#define NAME constant_value
This tells the preprocessor to replace every instance of NAME with the constant_value.
Makes the program easier to read, write, and maintain.
Example:
#define PI 3.14159
#define NUM_STUDENTS 85
#define OZ_PER_POUND 16
Basics of CFunction main
Every C program has a main function
Program execution begins with main function
main ( void ) is the main function heading
{ } braces are used to enclose the body of the function
Basics of CDeclarations
Declarations tell the compiler to set aside space for variables
Types are associated with the variables
The declaration:
double radius;
– tells the compiler space is needed for a variable named radius, and that it will be used to store doubles (reals).
Basics of CDeclarations and Data Types
General Syntax of a variable declaration:
type variable_list;
examples:
int count;
double radius,
area,
circumference;
char initial, code;
int for integers
double for real numbers
char for individual characters
Basics of CReserved Words and Standard
Identifiers
Reserved words have special meaning in C and cannot be used for anything else in the program
Must be lower case
Examples: int, double, void, return
Standard identifiers also have a special meaning in C, but they can be redefined by the programmer (though it is not a good idea).
Examples: printf, scanf
Basics of CUser-Defined Identifiers
Users create their own identifiers to name the variables and other items in the program (e.g. to name functions other than main)
Rules for creating user-defined identifiers:may contain only letters, digits, underscorecannot begin with a digitcannot contain spacescannot be reserved wordshould not be a standard identifier
Correct: Incorrect:
sum sum#1
hours_worked hours worked
R2D2 2R2D
MyVal My-Val
goodint int
Basics of CExecutable Statements
Executable statements tell the computer to take action during the running of the program
Statements are terminated by a semicolon
Examples:
• Assignment
area = PI * radius * radius;
• Input function
scanf( “%lf”, &radius );
• Output function
printf(“The area of the circle is %f and its circumference is %f\n”,area, circumference);
Basics of CComments
Comments are used to document programs
Syntax: /* text of comment */
Listed in the program but ignored by the compiler
Can go across lines, until terminating */ is written
Programs should begin with comments (This is called the Header Section). They should contain at least:
your namedue datelab numberlab sectionlab instructors namebrief description of the program
Comments should be used throughout to document the declaration of variables and main algorithm steps
Recommended