Download presentation
Presentation is loading. Please wait.
Published byLaurel Cummings Modified over 9 years ago
1
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices
2
Programming What is computer programming? Representation of a task or algorithm in a computer language. What is an algorithm? A set of directions for accomplishing a task.
3
Example of an Algorithm The algorithm for calculating the arithmetic mean (i.e., the average) of a set of numbers is 1. add all the numbers together 2. divide this sum by the quantity of numbers In mathematical terms this is written as
4
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
5
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
6
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
7
Analyzing a Problem cont. Example: You must sort 1000 numbers in ascending order. Simpler version: Sort 3, 2, 4, 1 in ascending order Pseudocode:
8
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 – Conditionals Example: if temperature > 150 degrees print ‘‘It’s too hot!’’ Repeating certain tasks – Loops Example: calculate the first 1000 prime numbers
9
Basics of C Example Code #include #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 */
10
Basics of C Form of a Simple C Program Preprocessor directives main ( void ) { declarations executable statements }
11
Basics of C Preprocessor Directive #include 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 gives access to (among others) the library functions scanf and printf which allow reading input from the keyboard and writing to the monitor
12
Basics of C Preprocessor 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
13
Basics of C Function 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
14
Basics of C Declarations 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).
15
Basics of C Declarations 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
16
Basics of C Reserved 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
17
Basics of C User-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, underscore cannot begin with a digit cannot contain spaces cannot be reserved word should not be a standard identifier Correct:Incorrect: sumsum#1 hours_workedhours worked R2D22R2D MyValMy-Val goodintint
18
Basics of C Executable 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);
19
Basics of C Comments 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 name due date lab number lab section lab instructors name brief description of the program Comments should be used throughout to document the declaration of variables and main algorithm steps
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.