sharing on basic knowledge on fundamental programming which is using programming c as a platform
Size: 8.78 MB
Language: en
Added: Mar 11, 2025
Slides: 56 pages
Slide Content
1.0 Introductory to Programming
This Chapter explains the Programming. At the end of this unit, you will be able to LEARNING OUTCOMES 1.1 1.3 1.2 1.4 Apply the simple C programs Remember the Programming Language Remember Definition and Types of Programming Remember algorithm, flowchart and pseudocode Understand the structured programming methodology 1.5
1.1 Remember the Programming Language Describe history of C Language Describe C Programming Describe Source Code Describe IDE Identify background of C programming
REMEMBER R Computer is a device that cannot think or make decisions on its own. Being an electronic machine, a computer needs human to provide it with instructions to make it perform tasks. Therefore, we give instructions to computer through programming to speak in the computer’s language. One of the programming language is C.
History of C Language C was developed by Dennis Ritchie in 1972 at AT&T Bell laboratories. It was designed as a system programming language for developing the UNIX operating system, which was also being created at Bell Labs. C is derived from an earlier language called B , which was developed by Ken Thompson . it has become one of the most influential programming languages in computer science. Dennis Ritchie D Describe
D C Programming Describe C can create lists of instructions for a computer to follow C is portable, powerful and flexible, therefore, C is used for both system and application level programming. C is reliable, simple and easy to use. Therefore, it is easier to learn newer languages once you are familiar with C language C is a structured language as it enforces logical structure in the program. C also follows modularity. Therefore, it is suitable for writing large and complex programs. C is one of thousands of programming languages currently in use and has been around for several decades because it gives programmers maximum control and efficiency.
D Describe Source Code Source code is the human-readable set of instructions written by a programmer using a programming language. It defines what a program does and how it operates. Source code is written in a high-level language like C, Python, Java, C++, or JavaScript, which is easier for humans to understand compared to machine code (binary). Key Points about Source Code: Editable - It can be modified and updated by programmers to add features or fix issues. Translation Required - It needs to be compiled or interpreted into machine code so that a computer can execute it. Essential for Software Development - Source code is the blueprint for software and is the first step in creating functional applications. Source code is written in an IDE
D IDE Describe IDE stands for I ntegrated Development Environment. Some popular IDE for C programming: Key Features of an IDE Code Editor A text editor specifically designed for writing source code. Includes syntax highlighting, code formatting, and autocompletion. Compiler/Interpreter Translates code into executable programs or interprets it for immediate execution. Debugger Helps identify and fix errors (bugs) in the code by allowing step-by-step execution and inspection. Build Automation Tools Automates repetitive tasks like compiling, linking, and packaging. Integrated Tools Often includes version control (e.g., Git), testing frameworks, and database connections. An IDE is essential for programming as it integrates tools like a code editor , compiler , and debugger into one interface , enhancing productivity, reducing errors, and simplifying the development process. Where u write , compile & execute your program
Expand macros, include preprocessor, remove comments Translate to assembly code, identify syntax errors or issues Convert to machine code (binary instructions) and Create object code Combines object files and link to libraries to produce executable file E Compile xplain Compilers are programs, which convert a high-level language program into machine level language program. This is because computers cannot understand programs written in high-level language. 1
E Execute xplain 2 Execution is the process of running the program to get the desired output. Control flow is determined by the program code Executable file Loading Linking libraries Executing instructions terminate Load executable code from disk into memory by a program called loader CPU runs instruction in main() function
1.2 Remember the Definition and Types of Programming Define PROGRAMME, PROGRAMMER AND PROGRAMMING LANGUAGE Describe various types of programming language
DEFINE D A program is a set of instructions given to the computer to perform any task. The process of writing these instructions is known as programming.
DEFINE D The person who writes the program is called a programmer
DEFINE D A programming language is a language that is used for writing computer programs
D Types of Programming Language Describe FORTRAN, ALGOL, COBOL, PASCAL, ADA, C SQL, LISP, PROLOG JAVA, C++, PYTHON, C#, PHP, Ruby, MATLAB, JavaScript, Visual Basic, .NET, Perl, Swift Execution is fast because data is already in binary Difficult to understand and modify Error fixing cannot be done Easier to use compared to machine language Easy to locate errors Easy to modify Machine dependent. Different machine, different set of instructions. Require knowledge of hardware Machine independent Easy to learn and write No need to know hardware Slower Must be translated into machine language programs
D Types of Programming Language Describe Ditulis berdasarkan internal architecture (design) mesin yang diprogramkan . Setiap processor bagi mesin berbeza hanya faham set arahan nya tersendiri . Bahasa yang lebih dekat dengan Bahasa manusia Programmer tidak perlu tahu tentang hardware (memory/ registry processor) computer yang digunakan Program yang sama boleh digunakan di pelbagai jenis komputer
D Machine Language Describe Machine language is the only language that computers can understand and all the instructions in this language are in the form of 1s and 0s. 101000010011001101 - is the instruction in machine language to add two numbers. 101000100011001101 - is the instruction in machine language to subtract two numbers. Assembly language is a programming language that use symbolic instructions called mnemonics or names. Computers cannot understand assembly language but can understand machine language. Assembly language instructions must therefore be converted into machine language instructions. Translators are used for this purpose. ADD R1 R2 – is the instruction in assembly language to add two numbers stored in registers R1 and R2. SUB R1 R2 – is the instruction in assembly language to subtract two numbers stored in the registers R1 and R2. Assembly Language High-Level Language High-level languages are programming languages that use English words and mathematical symbols to represent instructions in programs. a and b are the variables used in the program to represent the data These languages appear very similar to human languages. It is thus easier to program with these languages. Computers, however, cannot understand these languages. These languages have to be converted into machine language using the translator
1.3 Remember the Algorithm, Flowchart and Pseudocode Identify the algorithm in programming Identify the flowchart in programming Identify the pseudocode in programming Describe the advantages and disadvantages of flowchart
REMEMBER R Various programming tools, such as algorithm, flowchart and pseudocode, are used for solving problems. These tools are used to represent the instructions and designing the sequence of instructions in a program
Identify algorithm I An Algorithm is the sequence of steps required to provide a solution to a problem. The word algorithm was derived from the name of a mathematician, Mohammed ibn-Musa al-Khwarizmi . He developed the concept of the algorithm in mathematics Algorithm for making coffee Boil the water. Add coffee powder and sugar. Mix the contents. Drink or serve the coffee Example Characteristics of Algorithms: Instructions must be clear, simple, accurate and practical. Well-defined inputs Well-defined outputs Instructions should be language-independent Instructions should not be repeated infinitely.
Notice that the algorithm is a set of instructions written in simple English. Each step of the algorithm should be simple and it should clearly represent the input , process and output . Figure below represents the input, process and output of this algorithm. Input Water Coffee powder Sugar Process Boiling the water Adding coffee powder and sugar Mixing the contents Output Coffee algorithm I IPO Table
Identify pseudocode I A pseudocode is an informal “text-based” program description . It does not contain code syntax. It summarizes program’s steps (or flow) but excludes underlying details. The word algorithm was derived from the name of a mathematician, Mohammed ibn-Musa al-Khwarizmi. He developed the concept of the algorithm in mathematics Example Characteristics of pseudocode: Shows algorithm clearly it does not have any natural-language sentences or phrases it is independent from any particular programming language its syntax is not totally free, but it is more flexible and open than programming language syntax the actions of a single line of pseudocode must be executable in constant time k { i.e it must be O(1)} it must be understandable, and self-documenting with headers and comments and meaningful identifiers (labels and variable names) it makes good use of spacing and alignment (rather than punctuation such as {, }, ; etc ) to show statement termination and nested relations among statements
Keywords in pseudocode I There are numbers of various keywords that are used to show common input, output and processing operations such as : INPUT: obtain , get , read OUTPUT: display , show , print PROCESS: compute , calculate , determine INITIALIZE: set, init ADD ONE: increment , bump The word algorithm was derived from the name of a mathematician, Mohammed ibn-Musa al-Khwarizmi. He developed the concept of the algorithm in mathematics Example
Identify flowchart I A flow chart is a graphical or symbolic representation of a process. Each step in the process is represented by a different symbol and contains a short description of the process step. The flow chart symbols are linked together with arrows showing the process flow direction. Example
Flowchart SYMBOL I Advantages using flowchart Easy to Understand - Visual representation makes complex processes simple. Improves Communication - Helps everyone, technical or not, understand the logic. Better Planning - Organizes ideas before coding starts, reducing mistakes. Helps Debugging - Makes it easier to spot and fix errors in logic. Good Documentation - Acts as a reference for maintenance or new team members. Simplifies Testing - Clearly shows all possible paths to test. Encourages Logical Thinking - Promotes step-by-step problem-solving. Easy to Update - Quick to modify as changes are needed. Disadvantages of using flowcharts: Time-Consuming - Creating and updating can take time. Complexity Issues - Hard to manage for large or detailed programs. Skill Needed - Requires expertise to design effectively. Easily Outdated - Frequent updates can render them less useful. Inconsistent Standards - Different styles may cause confusion.
1.4 Understand the Structure Programming Methodology Explain the Sample of C Programme Explain the following types of programming Structured programming Modular Programming Object-oriented Programming
Sample of C Programming Preprocessor Directives Begin with # and are used to include files or define macros. Main Function Every C program must have a main() function, which serves as the entry point for execution. Comments Used to describe the code and improve readability. Single-line comments: // Multi-line comments: /* */ Key Characteristics Case-Sensitive: Keywords, variables, and function names are case-sensitive. Simple Syntax: Based on a small number of keywords (e.g., if, for, while). Semicolon Terminator : Each statement ends with a semicolon (;). Braces for Blocks : Code blocks are enclosed within { }. E xplain
E Types of Programming xplain During the early days of programming, programmers followed different approaches to write programs. In order to have a common approach, a specific set of rules was created and a common methodology was followed. Sequence structure Selection structure Looping structure
E Structured Programming xplain Structured programming is a programming methodology in which the instructions are written in a sequence. The structure of a program refers to the order in which the statements are executed in the program. Structured programming can be written in three types of structure Sequence Selection Repetition / looping
E Modular Programming xplain Modular programming is a programming methodology in which the complex program is broken into number of simple modules. Normally, problems that are very complex will have more number of code lines. It becomes very difficult for programmers to write and maintain such huge programs. To overcome this problem, a new approach to programming called modular programming was developed. In modular programming, a program is divided into a number of modules or procedures. Each modules or procedures perform a specific task. You can perform these tasks in any order. For example, when you write a program for performing arithmetic calculations, you write different modules for addition, subtraction, multiplication and division.
E Object-Oriented Programming xplain In both structured and modular programming, data and the code written to manipulate the data are treated as separate components. However, in Object-oriented programming, the data and the code are treated as a single unit . In this approach, importance is given to the process and not to the data. Data might get changed in any of the modules. Object-oriented programming gives more importance to the data. This programming approach aims to provide solutions for real-world related problems. You will learn more about this when you learn C++.
1.5 Apply the Simple C Programs Construct flowcharts for the given problem Apply flowchart for the following: Sequence structure Selection structure Looping structure Apply flowchart construction using AI Demonstrate sample of C Practice COMPILE and EXECUTE programs Examine common programming errors
Flowchart construction using AI Apply You may insert programming questions into any AI tools (e.g. chatGPT ) to help analyze problem and draw flowchart for you Here are some prompts that you can use to ask AI help you in programming Can you analyze this C programming problem? Please help to draw flowchart Please help me to detail with the shapes of the flowchart Please transform to mermaid code I have copied the code in Mermaid Live Editor but it has error Help me troubleshoot this error Mermaid Live Editor is a diagramming tool that helps you quickly create flowcharts, sequence diagrams, Gantt charts, and more using a simple text-based syntax (or mermaid code).
Sequence structure Apply Construct a flowchart for a program that takes three numbers from user, and calculate the average of the three numbers, then display the average of the numbers. start end Get Number1 Get Number2 Get Number3 Display Average Calculate Average = (Number1 + Number2 + Number3) / 3 Program will execute instructions in the order the instruction appears in the source code Input Process output number1 number2 number3 average = (Number1 + Number2 + Number3) / 3 average You may copy this into any AI tools ( chatGPT ) to help analyze problem and draw flowchart for you
emonstrate #include < stdio.h > int main() { int number1, number2, number3, average; printf (“Insert Number 1: "); scanf ("%d",&number1); printf (“Insert Number 2: "); scanf ("%d",&number2); printf (“Insert Number 3: "); scanf ("%d",&number3); average = (number1 + number2 + number3)/3; printf (" \ nAverage is %d. “, average); return 0; } Calculate average Accepts the input from the user and stores it in the variable number1 . D Accepts the input from the user and stores it in the variable number2 . Accepts the input from the user and stores it in the variable number3 . Print average Declare variables
structure of selection - simple if Construct a flowchart for a program that asks the number of day in January. If the user answer 31, display “You are correct”. If the answer is not 31, it should display “You are out from this branch”. Apply start end Print “Number of days in January:” Get days Display “You are correct” Is days == 31? true Display “You are out from this branch” Input Process output days 1. If days == 31 If true: display “you are correct” 2. Display “You are out from this branch” 3. End display You may copy this into any AI tools ( chatGPT ) to help analyze problem and draw flowchart for you false
emonstrate #include < stdio.h > int main() { int days; printf ("Number of days in January : "); scanf ("% d",&days ); if (days == 31) { printf (" You are correct. "); } printf (" \n You are out of if branch. "); return 0; } This expression will check whether the value of days is equal to 31 . If “days == 31” is true, Block 1 is executed. If “days == 31” is false, Block 1 is skipped. Accepts the input from the user and stores it in the variable days . D Block 1
Construct a flowchart for a program that asks the number X and Y from user. Then, the program evaluates, if X is greater than Y. If X is greater than Y, display “ X is greater than Y ”. Else if X is not greater than Y, it should display “ Y is greater than X ”. Apply Input Process output X Y 1. Evaluate X > Y If true: display “X is greater than Y” If false: display “Y is greater than X” 2. End display structure of selection - if-else You may copy this into any AI tools ( chatGPT ) to help analyze problem and draw flowchart for you start end Print “Enter X and Y:” Get X and Y values Display “X is greater than Y” Is X > Y? true Display “Y is greater than X” false
emonstrate #include < stdio.h > main() { int x,y ; printf ("Enter X and Y values : "); scanf ("%d % d",&x,&y ); if (x > y) printf (" X is greater than Y"); else printf (" Y is greater than X"); return 0; } This expression will check whether the value of x is greater than the value of y . This statement 1 will be executed when x> y is true . Statement 2 is skipped. This statement 2 will be executed when x>y is false . Statement 1 is skipped. D Statement 1 Statement 2
Construct a flowchart for a program that asks the number X and Y from user. Then, the program evaluates, if X is greater than Y. If X is greater than Y, display “ X is greater than Y ”. Else if X is not greater than Y, evaluate if X is equal to Y. If it is true, it should display “Y is equal to X”. Else it should display “ Y is greater than X ”. Apply Input Process output X Y 1. Evaluate X > Y If true: display “X is greater than Y” If false: evaluate X==Y If true: display “Y is equal to X” If false: display “Y is greater than X” 2. End display structure of selection - nested if-else start end Print “Enter X and Y:” Get X and Y values Display “X is greater than Y” Is X > Y? true Display “Y is greater than X” false Is X == Y? Display “X is equal to Y” true false You may copy this into any AI tools ( chatGPT ) to help analyze problem and draw flowchart for you
emonstrate #include < stdio.h > main() { int x,y ; printf ("Enter X and Y values : "); scanf ("%d % d",&x,&y ); if (x > y) { printf (" X is greater than Y"); } else { if (x==y) { printf (" Y is equal to X"); } else { printf (" Y is greater than X");} } return 0; } This expression will check whether the value of x is greater than the value of y . If it is true that x>y, block 1 is executed, and block 2 is skipped. If it is false that x>y, block 1 is skipped and Block 2 is executed. This statement will be executed when x is equal to y . This expression will check whether the value of x is equal to value of y . This statement will be executed when “x==y” is false D Block 1 Block 2
Construct a flowchart for a program that asks the user to insert one number, either number 7 or 8. If the user enter number 7, it should display “You have entered number Seven”. If the user enter number 8, it should display “You have entered number Eight”. Apply Input Process output num 1. Evaluate num If num == 7: display “ You have entered number Seven ” If num == 8: display “ You have entered number Eight ” 2. End display structure of selection - switch start end Print “Enter a value (7 or 8):” Is num == 8? true Display “ You have entered number Seven ” Is num == 7? Display “ You have entered number Eight ” true false false You may copy this into any AI tools ( chatGPT ) to help analyze problem and draw flowchart for you
emonstrate #include < stdio.h > main() { int num ; printf ("\n Please enter a value (7 or 8) : "); scanf ("%d",& num ); switch ( num ) { case 7: printf (" You have entered Seven "); break; case 8: printf (" You have entered Eight "); break; default: printf (" Wrong Entry "); } return 0; } This case is executed when the value of num is 7 . This case is executed when the value of num is 8 . This case is executed when the value of num is not equal to 7 or 8 . The value of x will be checked against each case and the corresponding statements will be executed. D
Construct a flowchart for a program that will print out Sifir 1 – Sifir 12 of a certain number N. Ask the number N from the user. Then, display in the form of this example (N = 2): 1 x 2 = 2 2 x 2 = 4 3 x 2 = 6 . . 12 x 2 = 24 Apply Input Process output N counter Init counter = 1 hasil = counter x N; display “counter x N = hasil ” counter++ If counter <= 12, repeat step 1 End hasil structure of looping start end Print “Enter a number N:” counter <= 12? true false Get N counter = 1 Display “ counter x N = hasil ” hasil = counter x N counter++ You may copy this into any AI tools ( chatGPT ) to help analyze problem and draw flowchart for you
emonstrate #include < stdio.h > i nt main() { int N, hasil , counter ; printf ("\n Please enter a number N: "); scanf ("% d",& N ); counter = 1 ; while(counter <=12) { hasil = counter * N; printf (“%d X %d = %d \n”, counter, N, hasil ); counter++; } return 0; } This will print the value of each variable “counter” x “N” = “ hasil ” for all values of counter from 1 to 12 Value of counter is increased by 1. This instruction will always repeat the Block 1 as long as the value of counter is smaller or equal to 12. If value of counter is no more smaller or equal to 12, the Block 1 will be skipped and program proceed to next instruction. D Block 1
Summary 1 C is a high-level programming language used by many programmers to write programs that instruct computers to perform task. S 2 C programming can be done in an IDE so that the source code can be compiled and executed without errors. 3 Algorithms are applied in problem solving either by using pseudocode or flowchart to help in writing a well-planned program. 4 C program can be written in structured and modular method which can apply sequence, selection and repetition structure.
Practice COMPILE and EXECUTE programs When compiling and executing programs, there are possibilities to have any of these errors: Syntax Errors b. Logical Error c. Runtime error code does not follow the syntax rules of the programming language. The program won’t run until the syntax is corrected Example : Missing brackets, incorrect spelling of keywords, or improper punctuation cause it to produce incorrect results, even though it runs without crashing. The program does what you told it to do, but not what you intended it to do. Example : Using subtraction instead of addition in a calculation. occur while the program is running, usually caused by unexpected conditions or invalid input. The program starts but crashes during execution. Example : Dividing a number by zero, or trying to open a file that doesn’t exist.
Practice COMPILE and EXECUTE programs Write a C program to print “ Hello world . I love C programming !” 1 You may copy this into any AI tools ( chatGPT ) to help analyze problem and write program for you. Then copy the code into any C programming IDE, COMPILE and EXECUTE the program!
Practice COMPILE and EXECUTE programs Write a C program to to print “ Hello world . I love C programming !”. Then, prompt the user to give his name and accept the name. Then display the text “ Hello (user’s name), welcome to C programming.” 2 You may copy this into any AI tools ( chatGPT ) to help analyze problem and write program for you. Then copy the code into any C programming IDE, COMPILE and EXECUTE the program!
Write a C program to accept a number and find whether the number is negative or positive. 3 Practice COMPILE and EXECUTE programs You may copy this into any AI tools ( chatGPT ) to help analyze problem and write program for you. Then copy the code into any C programming IDE, COMPILE and EXECUTE the program!
Write a C program to accept a number from the user and check whether the number is divisible by 10. If the condition is satisfied, display the text Number is divisible by 10 . If the condition fails, display the text Number not divisible by 10 . 4 Practice COMPILE and EXECUTE programs You may copy this into any AI tools ( chatGPT ) to help analyze problem and write program for you. Then copy the code into any C programming IDE, COMPILE and EXECUTE the program!
Write a C program to accept the ages of three brothers and find the eldest of the three. 5 Practice COMPILE and EXECUTE programs You may copy this into any AI tools ( chatGPT ) to help analyze problem and write program for you. Then copy the code into any C programming IDE, COMPILE and EXECUTE the program!
Write a C program to accept a character from the user. If the input is V or v, display the text Violet . If the input is B or b, display the text Blue . If the input is R or r, display the text Red . For any other value, display the text Invalid input . 6 Practice COMPILE and EXECUTE programs You may copy this into any AI tools ( chatGPT ) to help analyze problem and write program for you. Then copy the code into any C programming IDE, COMPILE and EXECUTE the program!
Write a C program to print out multiplication table of 1, 2 ,3, 4, 5, 6, 7, 8, 9,10,11 and 12. 7 Practice COMPILE and EXECUTE programs You may copy this into any AI tools ( chatGPT ) to help analyze problem and write program for you. Then copy the code into any C programming IDE, COMPILE and EXECUTE the program!
Write a C program to print out time in the format HH: MM from 8.00 AM to 5.00 PM. 8 Practice COMPILE and EXECUTE programs You may copy this into any AI tools ( chatGPT ) to help analyze problem and write program for you. Then copy the code into any C programming IDE, COMPILE and EXECUTE the program!