CC123 Programming 1 Week 2 Student Activity Sheet PDF
Document Details
Uploaded by EliteFuturism
Cebu Technological University
CC123
Tags
Summary
This document contains a student activity sheet for a C programming course, covering introduction to the language, programming logic, and its different types. It touches upon algorithms, and provides examples of C code.
Full Transcript
CC123 – Programming 1 Week 2 Information Technology Department...
CC123 – Programming 1 Week 2 Information Technology Department STUDENT ACTIVITY SHEET Materials INTRODUCTION TO C PROGRAMMING Computer, Smartphone, Student Activity Sheet, GSuite Intended Learning Outcomes References At the end of the lesson, you should be able to: Create an algorithm using the flowchart diagram based given the specific programming problems. CLO1, CLO2 Utilize the input and output statement following the programming SDG Integration style best practices in program creation CLO1, CLO2 SDG # 4 – Quality Education (Ensure inclusive Code a C program applying correct syntax in declaring assignment and equitable quality education and promote life- statement, interactive input and the basic mathematical library long learning opportunities for all functions SDG # 9 – Innovation and Infrastructure (Build CLO1 resilient infrastructure, promote inclusive and sustainable industrialization and foster innovation Lesson Preparation/ Review/Preview Direction: Answer the question below. 1. In your own understanding, what is programming. Ans:______________________________________________ 2. What do you call a container of a data. Ans:______________________________________________ 3. Give one example of a data type. Ans:______________________________________________ 4. Is C language a case sensitive language? Yes, or no? Ans:______________________________________________ 5. Printf(“HELLO BSIT”); What is the output. Ans:______________________________________________ Concept Notes Presentation Refer on the Handouts of the discussion. OVAL – START HEXAGONAL – Preparation OR it could be PARALELLOGRAM (INPUT /OUTPUT) RECTABGLE – Process OVAL - END #include int main(){ int a, b, sum; a = 5; b = 2; sum= a + b; printf("%d", sum); return 0; } HOW THE PROGRAM WORKS IN YOUR COMPUTER? C Program Structure C program basically contains the following: 1. Headers The segment of a C program where libraries are included in the compilation process and where macros to be defined are written and stated. It may contain the following: a. Preprocessor. A system program that modifies a C program prior to its compilation. b. Preprocessor directive. A C program line beginning with # that provides an instruction to the preprocessor. The two most commonly use directives are: #include – for libraries used in the program. #define – for constant macro definitions. A constant macro is a name that is replaced by a particular constant value before the program is sent to the compiler. Example #define pi_value 3.14159 #define k 2 Note: The contents of a named constant memory location cannot change during program execution. 2. Declaration All functions, variables, units or objects that contain values or group of values must be made known to the computers so that memory spaces will be allocated for each of them. 3. Functions These are parts of a C program that performs a specific task. The task assigned to a function is performed whenever C encounters the function name. In C, there should be at least one function and it is the function main. The function main() in C language declares the main program and call other functions. Note: C is case sensitive! 4. Comments These are remarks and non-executable phrases or sentences. Importance of Comments on Programs Program documentation contains information that enhances the readability of the program. Comments are part of program documentation because they help others read and understand the program. The compiler, however, ignores comments and they are not translated into machine language. Types of Comments: Multiple line comments – are comments enclosed by. Single line comments – are comments placed after // symbols. Identifiers Identifiers are names given to various program elements, such as variables, functions and arrays. The following are the guidelines in creating identifiers: It consists of letters and digits without space, in any order, except that the first character must be a letter. Uppercase and lowercase are permitted but they are not interchangeable. As an exception, an underscore may be used as the first character of the identifier. Reserved words and standard functions must not be redefined. Example of Valid Identifiers X num_1 y1 _area NAME Sum Example of Invalid Identifiers Reason for Invalidity 1st starts with a digit tax rate contains space $Area contains a special character void main a reserved word int must not be redefined Reserved Words Reserved words are keywords that have standard predefined meaning in C. These reserved words can be used only for intended purpose and they cannot be used as programmer defined identifiers. printf scanf int struct goto void main long switch auto do register typedef break else return union case enum short unsigned char extern signed void const float sizeof volatile continue for static while default goto include struct do if int double Basic Data Types There are four basic data types in C as shown in the table below: Typical Memory Data Type Description Requirements positive and negative numbers without decimal 2 bytes or 1 word (varies from int point one computer to another) char single character 1 byte number containing decimal point and or float 4 bytes exponent double precision floating point number (higher double 8 bytes precision) Type Modifiers / Qualifiers In order to give more flexibility and extend the range of the various data types there are four type qualifiers: signed unsigned long short The modifiers long and short have been covered for integers. Prior to the ANSI standard (the main organization supporting the development of technology standards in the United States.) there were similar modifiers for floats. However, the introduction of double removed the requirement for a long float. This is why a double has the format command % If in the printf() and scanf() functions. It is, however, now possible to declare a long double which uses 16 bytes of storage and has approximately 24 digits of precision. The signed and unsigned qualifiers are used to remove the special meaning assigned to the first bit of storage for char and int. By default these are signed. If the first (signed) bit is zero the number is positive, if it is one the number is negative. The effect of making a variable unsigned is that the first bit becomes part of the positive number, thus extending the possible range above zero. The full list of ranges can be seen in the table given. Type Format Length Range Precision unsigned char %c 8 bits 0 to 255 - char %c 8 bits -128 to 127 - unsigned int %i or %d 16 bits 0 – 65535 - short int %i or %d 16 bits -32,768 to 32,767 - int %i or %d 16 bits -32,768 to 32,767 - unsigned long %lu 32 bits 0 to 4,294,967,295 - long %ld 32 bits -2,147,483,648 - to 2,147,483,68 float %f 32 bits 3.4 X 10 –38 to 3.4 X 10 +38 6 digits double % If 64 bits 1.7 X 10 –308 to 1.7 X 10 +308 12 digits long double %If 80 bits 3.4 X 10 –4932 to 24 digits 3. X 10 +4932 Variable A variable is an identifier whose value is allowed to change during the execution of a program. In programming, a variable is an identifier that serves as a container for storing data values. Variable Declaration Every variable must be individually declared (or defined) before it can appear in a program. The syntax is: data type variable list; Example: int x, y, z; //x, y and z are variables that store integer values char a, b, c //a, b and c are variables that store char values Example in a code if (number > 0) { printf("The number is positive.\n"); }else{ printf("The number is not positive.\n"); } Variable Initialization A variable can be given a value at the time of declaration. This is known as initialization. Example: int sum = 0; //line 1 char mi = ‘S’; //line 2 The statement labeled line 1 initializes an integer variable sum with the value zero. The statement labeled line 2 initializes a char variable mi with the value ‘S’. Input and Output Statement in C The standard C library contains two functions that perform formatted input and output. These are: printf () which writes to the screen scanf () which reads data from the keyboard The term formatted means that these functions can read and write data in various formats that the programmer decides. The header file associated with the standard input and output is stdio.h. Any program using these functions must incorporate this header file in the preprocessor directives. printf () function printf () has the following forms: printf (“Control String”, Argument List); The Control String The control string in the printf() function can contain three types of characters: Literal text to be printed Format commands beginning with a % which indicates the data type to be printed Escape sequences beginning with a left switch (\) to help format the literal text and data. Any item in the control string which is not proceeded by a % or \ will be printed literally to stdout (standard output, normally the screen). Format Commands The format commands can be used in any combination as long as the number of arguments exactly matches the number of format commands. Commas separate successive arguments from each other. For example: printf(“Code %c %d : Price %f : Product %s \n”, ‘D’, 1416, 1.67, “Ink” ); This gives the output: Code D1416: Price 1.670000: Product Ink %c Single Character %s String of Characters %d Decimal %i Integer (same as %d) %f Decimal Floating-Point Number %e Scientific Notation %g Uses %e or %f, whichever is shorter %o Octal %u Unsigned Decimal %x Hexadecimal %% Percent Sign %p Pointer Format Commands for printf() and scanf(). The program format.c demonstrates the format commands in printf #include void main () { printf(“This is a single character : %c \n”, ‘A’); printf(“This is a string : %s \n”, “Hello World”); printf(“This is a decimal number : %d \n”, 24); printf(“This is a floating point number : %f \n”, 37.51); } Output: This is a single character : A This is a string : Hello World This is a decimal number : 24 This is a floating point number : 37.510000 There are two format command modifiers to printf() which allow it to display either short or long integers. The printf function is used for output. It prints (displays) text, numbers, or other information to the console or standard output. Syntax: printf("format string", variables); Example The scanf function is used for input. It reads data from the standard input (usually the keyboard) and stores it in variable Syntax: scanf("format string", &variables); Example Compu Skill (Practice) 40 mins Direction: Label the Flowchart by the name of each shape and function. Direction: Label the sample code below. #include 1 header void main() 2 { int a, b, c; variable declaration 4 3 a = 87; b = 243; variable initialization c = a * b; printf(“\n%d times %d equals %d\n”, a,b,c); printf()format } Direction: Run the code. Filename Untitled.c #include int main(){ printf("Hello, World!\n"); return 0; } Practice this …. File sample.cpp #include int main() { int a, b, c; a = 87; b = 243; c = a * b; printf("\n%d times %d equals %d\n", a, b, c); return 0; } Shared Syntax and Compatibility: C++ is a superset of C, meaning that most valid C code is also valid C++ code. This compatibility allows C code to compile and run even when the file has a.cpp extension. Direction: Write a simple code. 1. Write a simple code that will output the product of a number. A=87 B= 243 C=a*b; ANSWER REFER SAMPLE ABOVE 2. Write a simple code that will output the age, height, gender and civil status of a student. Age= Gender= male, female Civil status= S,W,M Height= must be a double/float ANSWER: #include int main() { int age; double height; char gender; // Single char char civils; // Single char printf("Enter your age: "); scanf("%d", &age); printf("Enter your height: "); scanf("%f", &height); printf("Enter gender (M/F): "); scanf(" %c", &gender); // Read a single character printf("Enter civil status (S/W/M): "); scanf(" %c", &civils); // Read a single character // Output the entered information printf("\nYou entered:\n"); printf("Age: %d\n", age); printf("Height: %.2f\n", height); printf("Gender: %c\n", gender); printf("Civil Status: %c\n", civils); return 0; } Performance Direction: Answer the following. Use Lastname_SASWEEK# as a filename. 1. Make a flowchart that will Input two numbers and display the sum 2. From the flowchart above, write it in a code. 3. Define Algorithm. 4. Define and explain a Pseudocode. 5. Create a flowchart that will input age and print the string can vote if the age is >= (greater than or equal to) 18 otherwise print the string cannot vote 6. Create a flowchart that will input three (3) numbers and display the highest number. (Assume no equal values.)