1 CSC103: Introduction to Computer and Programming Lecture No 7.
CSC103: Introduction to Computer and Programming
description
Transcript of CSC103: Introduction to Computer and Programming
![Page 1: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/1.jpg)
1
CSC103: Introduction to Computer and Programming
Lecture No 4
![Page 2: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/2.jpg)
2
Today’s lecture outline
• Complex flowcharts• Function• Algorithm• A simple C program
![Page 3: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/3.jpg)
3
Make a flowchart to fill the bath tub with water
Start
End
Turn on hot and cold water
Too hot or cold
Adjust hot and cold taps
Wait for 2 minutes
Is the bath full
A
A
Turn off hot and cold taps
Y
N
N
Y
1. Turn on the hot and cold taps.
2. Is it too hot or cold? If it is, go to step 3, otherwise go to step 4.
3. Adjust the hot and cold taps and go back to step 2.
4. Wait for 2 minutes.5. Is the bath tub full? If it is,
go to step 6, otherwise go to step 4.
6. Turn off the hot and cold taps.
![Page 4: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/4.jpg)
4
Make a flowchart to find the input number is greater than 100 or less than or equal to 100 Start
End
1. Display message “Enter a number”
2. Read the input in x3. Is x > 100 if it is go to step 4
otherwise go to step 54. Display a message “Greater
than 10” 5. Display a message “Less
than or equal to 100”
Input: x
Display: Enter a number
x > 100False True
Display: Greater than
100
Display: less than or equal
to 100
![Page 5: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/5.jpg)
5
![Page 6: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/6.jpg)
6
![Page 7: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/7.jpg)
7
Functions
• From a programming perspective, functions allow you to group a logical series of activities, or program statements, under one name.
• Every C program must have a main function
• For example, suppose I want to create a function called bakeCake
![Page 8: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/8.jpg)
8
Algorithms
• An algorithm is a finite step-by-step process for solving a problem.
• It can be as simple as a recipe to bake a cake, or as complicated as the process to implement an autopilot system for a 747 jumbo jet.
• Algorithms generally start off with a problem statement.
• It is this problem statement that programmers use to formulate the process for solving the problem.
![Page 9: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/9.jpg)
9
Algorithm for BakeCake
• Mix wet ingredients in mixing bowl• Combine dry ingredients• Spoon batter into greased baking pan• Bake cake at 350 degrees for 30 minutes
Anyone reading my code will see my function called bakeCake and know right away that I’m trying to bake cakes.
![Page 10: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/10.jpg)
10
Functions
• Functions are typically not static, meaning they are living entities,
• Philosophically, that take in and pass back information.
• Thus, our bakeCake function would take in a list of ingredients to bake (called parameters) and return back a finished cake (called a value).
• The main() function group activities and can take in parameters (information) and pass back values (again, information).
![Page 11: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/11.jpg)
11
Function (cont.)• main()
{
}• The main() function begins with the keyword main
and is followed by two empty parentheses (). • The parentheses are used to encompass parameters
to be passed to the main() function.
C is a case-sensitive programming language. For example, the function names main(), Main(), and MAIN() are not the same.
![Page 12: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/12.jpg)
12
Function (cont.)
• Following the parentheses are two braces. – The first brace denotes the beginning of a
logical programming block and – the last brace denotes the end of a logical
programming block.• Each function implementation requires that
you use a beginning brace, {, and a closing brace, }.
![Page 13: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/13.jpg)
13
A Simple C Program
• When the above program is compiled and run, it outputs the text “C you later” to the computer screen
/* C Programming for the Absolute Beginner *///by Michael Vine
#include <stdio.h>main(){ printf("\nC you later\n");}
Go to Program
![Page 14: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/14.jpg)
14
Components of C Program
/* C Programming for the Absolute Beginner *///by Michael Vine
#include <stdio.h>main(){ printf("\n C you later \n") ;}
multi-line comment block
single line comment block
preprocessordirective
standard input output library
begin logicalprogram block
end logicalprogram block
program statement
program statement terminator
printf function escape sequence
![Page 15: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/15.jpg)
15
Comments• Comments help to identify program purpose and explain
complex functions• They can be valuable to you as the programmer and to
other programmers looking at your code.
• Text C Programming for the Absolute Beginner is ignored by the compiler because it is surrounded with the character sets /* and */.
• The character set /* signifies the beginning of a comment block; the character set */ identifies the end of a comment block.
/* C Programming for the Absolute Beginner */
![Page 16: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/16.jpg)
16
Cont.
• Multi-line commenting
• Any characters read after the character set // are ignored by the compiler for that line only
• A multi-line comment block with character set //
/* C Programming for the Absolute Beginner Chapter 1 – Getting Started with C Programming By Michael Vine*/
//C Programming for the Absolute Beginner//Chapter 1 - Getting Started with C Programming//By Michael Vine
//by Michael Vine
![Page 17: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/17.jpg)
17
Keywords
• There are 32 words defined as keywords in the standard ANSI C programming language
• Keywords have predefined uses and cannot be used for any other purpose in a C program– while– if– int– float– char– for
![Page 18: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/18.jpg)
18
Program Statement
• Serve to control program execution and functionality
• Many of these program statements must end with a statement terminator ;
• Any text you want to display in the standard output must be enclosed by quotation marks.
printf("\n C you later \n") ;
![Page 19: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/19.jpg)
19
Cont.• Program statements that do not require the use of
statement terminators are– Comments– Preprocessor directives (for example, #include or #define)– Begin and end program block identifiers– Function definition beginnings (for example, main())
• Preceding program statements don’t require the semicolon (;) terminator because they are not executable C statements or function calls
• Only C statements that perform work during program execution require the semicolons
![Page 20: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/20.jpg)
20
Escape Sequence
• Escape sequences are specially sequenced characters used to format output
printf("\nC you later\n");• This printf() function adds two new lines for formatting
purposes. • Before text is shown, the program outputs a new line. • After the text is written to standard output.
![Page 21: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/21.jpg)
21
Escape Sequence \n• printf("line 1\nline2\nline3\n");
line 1line 2line 3_
• printf("C ");• printf("for the ");• printf("Absolute Beginner\n");
C for the Absolute Beginner_ Go to Program
![Page 22: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/22.jpg)
22
Escape Sequence \t• Escape sequence \t moves the cursor to the next
tab space. printf("\nSun\tMon\tTue\tWed\tThu\tFri\tSat\n");
printf("\t\t\t\t1\t2\t3\n");printf("4\t5\t6\t7\t8\t9\t10\n");printf("11\t12\t13\t14\t15\t16\t17\n");printf("18\t19\t20\t21\t22\t23\t24\n");printf("25\t26\t27\t28\t29\t30\t31\n");
Go to Program
![Page 23: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/23.jpg)
23
Escape Sequence \r
• printf("This escape sequence moves the cursor ");• printf("to the beginning of this line\r");
• printf("This escape sequence moves the cursor \r");• printf("to the beginning of this line");
This escape sequence moves the cursor to the beginning of this line
to the beginning of this line cursor Go to Program
![Page 24: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/24.jpg)
24
Escape Sequence \\
• Escape sequence \\ inserts a backslash into your text.
• Whenever the program reads a backslash in a printf() function, it expects to see a valid escape character right after it.
• backslash character (\) is a special character in the printf() function; if you need to display a backslash in your text, you must use this escape sequence.
printf("c:\\cygwin\\bin must be in your system path");
c:\cygwin\bin must be in your system path
![Page 25: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/25.jpg)
25
Escape Sequence \"
• Used to to insert a double quote into your outputted text
• printf("\"This is quoted text\"");
"This is quoted text"
![Page 26: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/26.jpg)
26
Escape Sequence \'
• Used to insert a single quote into your outputted text
• printf("\nA single quote looks like \'\n");
A single quote looks like '
![Page 27: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/27.jpg)
27
Directives
• Program statement that begins with the hash sign (#) #include <stdio.h>
• When the C preprocessor encounters the hash sign, it performs certain actions depending on the directive that occurs prior to compiling
• In the above example, I told the preprocessor to include the stdio.h library with my program
• stdio.h is short for standard input output header file• It contains links to various standard C library functions,
such as printf().
![Page 28: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/28.jpg)
28
C Compiler
• C program goes through a lot of steps prior to becoming a running or executing program
• C compiler performs a number of tasks for you. Most notable are the following– Preprocesses the program code and looks for various
directives– Generates error codes and messages, if applicable– Compiles program code into an object code and stores
it temporarily on disk– Links any necessary library to the object code and
creates an executable file and stores it on disk
![Page 29: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/29.jpg)
29
How to debug a program
• If your program compiles, exits, or executes abnormally, there is almost certainly an error (a bug) in your program.
• Debugging is an art, the more you practice programming the easier debugging will become!
• Often a program will compile and execute just fine, but with results you did not expect
![Page 30: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/30.jpg)
30
Example
#include <stdio.h>main(){ printf("The format issue can be corrected by using"); printf(" the \n and \\ escape sequences");}
Program output (Program 1)The format issue can be corrected by using the and \ escape sequences
#include <stdio.h>main(){ printf("The format issue can be corrected by using"); printf(" the \\n and \\\\ escape sequences");}Program output (Program 2)The format issue can be corrected by using the \n and \\ escape sequences
![Page 31: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/31.jpg)
31
Common Error # 1
• Missing program block identifier• If you forget to insert a beginning or a corresponding ending
program block identifier ({ or }), you will see error messages
#include <stdio.h>main()
printf("Welcome to C Programming\n");}
Compiling...hello.chello.c(13) : error C2061: syntax error : identifier 'printf‘hello.c(13) : error C2059: syntax error : ';‘hello.c(13) : error C2059: syntax error : 'string‘hello.c(17) : error C2059: syntax error : '}'Error executing cl.exe.hello.obj - 4 error(s), 0 warning(s)
![Page 32: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/32.jpg)
32
Common Error # 2
• Missing statement terminator• Parse errors occur because the C compiler is unable to determine
the end of a program statement such as print statement.
#include <stdio.h>main(){ printf("Welcome to C Programming\n")}
Compiling...hello.chello.c(17) : error C2143: syntax error : missing ';' before '}'Error executing cl.exe.
![Page 33: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/33.jpg)
33
Common Error # 3
• Invalid preprocessor directive• If you type an invalid preprocessor directive, such as
miss spell a library name, you will receive an error
#include <sdio.h>main(){ printf("Welcome to C Programming\n");}
Compiling...esp_seq.chello.c(1) : fatal error C1083: Cannot open include file: 'sdio.h': No such file or directoryError executing cl.exe.hello.obj - 1 error(s), 0 warning(s)
![Page 34: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/34.jpg)
34
Common Error # 4
• Invalid escape sequence• When using escape sequences it is common to use
invalid characters or invalid character sequences.
#include <stdio.h>main(){ printf("Welcome to C Programming \m");}
Compiling...esp_seq.chello.c(13) : warning C4129: 'm' : unrecognized character escape sequencehello.obj - 0 error(s), 1 warning(s)
![Page 35: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/35.jpg)
35
Common Error # 5
• Invalid Comment Blocks• invalid comment blocks generates compile errors
#include <stdio.h>main(){ */ printf(“hello world \n”) \; /* printf("Welcome to C Programming \n");}
Compiling...test.ctest.c(7) : warning C4138: '*/' found outside of commenttest.c(7) : error C2059: syntax error : '/‘test.c(12) : fatal error C1071: unexpected end of file found in commentError executing cl.exe.test.exe - 2 error(s), 1 warning(s)
![Page 36: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/36.jpg)
36
Summary
• Functions allow to group a logical series of activities, or program statements, under one name
• Functions can take in and pass back information• An algorithm is a finite step-by-step process for solving a
problem• Each function implementation requires that you use a
beginning brace { and a closing brace }• Comments help to identify program purpose and explain
complex functions• /* signifies the beginning of a comment block and */
identifies the end of a comment block
![Page 37: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/37.jpg)
37
Summary
• There are 32 words defined as keywords in the standard ANSI C programming language
• Keywords have predefined uses and cannot be used for any other purpose in a C program
• Most program statements control program execution and functionality and may require a program statement terminator ;
• Program statements that do not require a terminator include preprocessor directives, comment blocks, and function headers
![Page 38: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/38.jpg)
38
• printf() function is used to display output to the computer screen
• Special characters (such as n) when combined with the backslash (\), make up an escape sequence.
• stdio.h is short for standard input output and contains links to various standard C library functions, such as printf()
• C compiler preprocess program code, generate error codes and messages if applicable, compile program code into object code, and link any necessary libraries.
![Page 39: CSC103: Introduction to Computer and Programming](https://reader036.fdocuments.us/reader036/viewer/2022062521/56816711550346895ddb7a5c/html5/thumbnails/39.jpg)
39
Summary
• Compile errors are generally the result of syntax issues, including missing identifiers and terminators, or invalid directives, escape sequences, and comment blocks
• A single error at the top of your program can cause cascading errors during compile time
• The best place to start debugging compile errors is with the first error