Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSC103: Introduction to Computer and Programming Lecture No 4.

Similar presentations


Presentation on theme: "1 CSC103: Introduction to Computer and Programming Lecture No 4."— Presentation transcript:

1 1 CSC103: Introduction to Computer and Programming Lecture No 4

2 2 Today’s lecture outline Complex flowcharts Function Algorithm A simple C program

3 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.

4 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 x 3.Is x > 100 if it is go to step 4 otherwise go to step 5 4.Display a message “Greater than 10” 5.Display a message “Less than or equal to 100” Input: x Display: Enter a number x > 100 False True Display: Greater than 100 Display: less than or equal to 100

5 5

6 6

7 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

8 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.

9 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.

10 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).

11 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.

12 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, }.

13 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 main() { printf("\nC you later\n"); } Go to Program

14 14 Components of C Program /* C Programming for the Absolute Beginner */ //by Michael Vine #include main() { printf("\n C you later \n") ; } multi-line comment block single line comment block preprocessor directive standard input output library begin logical program block end logical program block program statement program statement terminator printf function escape sequence

15 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 */

16 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

17 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

18 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") ;

19 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

20 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.

21 21 Escape Sequence \n printf("line 1\nline2\nline3\n"); line 1 line 2 line 3 _ printf("C "); printf("for the "); printf("Absolute Beginner\n"); C for the Absolute Beginner _ Go to Program

22 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

23 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

24 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

25 25 Escape Sequence \" Used to to insert a double quote into your outputted text printf("\"This is quoted text\""); "This is quoted text"

26 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 '

27 27 Directives Program statement that begins with the hash sign (#) #include 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().

28 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

29 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

30 30 Example #include 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 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

31 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 main() printf("Welcome to C Programming\n"); } Compiling... hello.c hello.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)

32 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 main() { printf("Welcome to C Programming\n") } Compiling... hello.c hello.c(17) : error C2143: syntax error : missing ';' before '}' Error executing cl.exe.

33 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 main() { printf("Welcome to C Programming\n"); } Compiling... esp_seq.c hello.c(1) : fatal error C1083: Cannot open include file: 'sdio.h': No such file or directory Error executing cl.exe. hello.obj - 1 error(s), 0 warning(s)

34 34 Common Error # 4 Invalid escape sequence When using escape sequences it is common to use invalid characters or invalid character sequences. #include main() { printf("Welcome to C Programming \m"); } Compiling... esp_seq.c hello.c(13) : warning C4129: 'm' : unrecognized character escape sequence hello.obj - 0 error(s), 1 warning(s)

35 35 Common Error # 5 Invalid Comment Blocks invalid comment blocks generates compile errors #include main() { */ printf(“hello world \n”) \; /* printf("Welcome to C Programming \n"); } Compiling... test.c test.c(7) : warning C4138: '*/' found outside of comment test.c(7) : error C2059: syntax error : '/‘ test.c(12) : fatal error C1071: unexpected end of file found in comment Error executing cl.exe. test.exe - 2 error(s), 1 warning(s)

36 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

37 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

38 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.

39 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


Download ppt "1 CSC103: Introduction to Computer and Programming Lecture No 4."

Similar presentations


Ads by Google