







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
this could help you pass the first midterms
Typology: Lecture notes
1 / 13
This page cannot be seen from the preview
Don't miss anything!








1. Computer Programming Discipline. The term of an algorithm. Forms of the algorithm representation Computer Programming: It is the process of designing and building executable computer programs to accomplish a specific computing result or to perform a specific task. Notion of Algorithm: An algorithm is a set of well-defined instructions that describes how to solve a problem. It takes a set of inputs and produces the desired output. The steps are executed in a given order, and they can be executed without the knowledge of the problem that is being solved. Ways of Representing an Algorithm: Algorithms can be represented in three main ways: Natural Language: Describing the algorithm in a human language. Pseudo-code: A representation of the algorithm that resembles, but is simpler than, regular programming languages. Flow Diagrams (Flowcharts): A graphical representation of the algorithm, showing each step as a box and control flow as arrows between the boxes. 2. Graphic symbols for the operations. Flowchart of an algorithm. Graphic Symbols for Operations: In the context of algorithms, graphic symbols are typically used in flowcharts to represent different operations: Terminator: Represents the starting or ending point of the system. Process: A box indicates a particular operation. Decision: A diamond represents a decision or branching point. Lines coming out from the diamond indicate different possible situations, leading to different sub-processes. Data: Represents information entering or leaving the system. An input might be an order from a customer. Output can be a product to be delivered. <- input; <- output; Preparation Symbol: It is used to represent a setup for another step in process, if it is required for the variables to be initialized then Preparation Symbol can be added. On-Page Connector: It indicates connects multiple flow lines from different operations into a single one. Flow Lines: Represent the flow of the sequence and direction of a process. Logic Scheme of an Algorithm: A logic scheme of an algorithm refers to the logical structure that an algorithm follows to solve a problem. It includes the sequence of steps, decision-making processes, and data processing procedures that lead to the desired output. The logic scheme can be represented using various methods such as pseudocode, flowcharts, and natural language. The logic scheme is crucial in understanding how an algorithm works and is often used in designing and implementing algorithms. 3. Structure of a program in the C language. Structure of the main() function. Structure of a Program in the C Language: A C program is divided into 6 parts: Documentation: This section consists of comments that describe the program, its name, and the creation date and time. Preprocessor Section: This section declares all the header files of the program. Header files allow us to incorporate other’s improved code into our code. Definition: Preprocessors are programs that process our source code before compilation. The #define preprocessor is used to create a constant throughout the program. Global Declaration: This section contains global variables, function declarations, and static variables. Variables and functions declared in this scope can be used anywhere in the program. Main() Function: Every C program must have a main function. The main() function of the program is written in this section. Operations like declaration and execution are performed inside the curly braces of the main program.
Sub Programs: User-defined functions are called in this section of the program. Structure of the Function Main() in C Language: The main() function is where the execution of a C program starts. It is a user-defined function that is mandatory for the execution of a program. The syntax of main() function can be written in many ways in C language, but one particular popular way is: int main(){/* program; */ return 0;}.
4. The scanf() and printf() functions for input and output operations. Format specifiers. scanf() Function: The scanf() function is used to read input data from the user. It reads formatted data and assigns them to the variables provided in the additional arguments. The syntax of scanf() is scanf("format specifier", &variable);. printf() Function: The printf() function is used to print data on the console. It can print different kinds of data formats on the output string. The syntax of printf() is printf("format specifier", variable);. Format Specifiers: Format specifiers in C are used to tell the compiler about the type of data to be printed or scanned in input and output operations. They always start with a % symbol and are used in the formatted string in functions like printf(), scanf(), etc. Here are some commonly used format specifiers: %c: For character type. %d: For signed integer type. %f: For float type. %lf: For double type. %s: For string type. %u: For unsigned integer type. %e or %E: For scientific notation of floats. %x or %X: Hexadecimal representation. %%: Prints % character. 5. Algorithms with a linear and branched structure. Conditional statements: if, if-else if-else if… wlse if-else. Selection statement switch. Algorithms with a Linear Structure: Linear algorithms are a type of data structure where data elements are arranged sequentially or linearly. Each element has a unique predecessor (except for the first element) and a unique successor (except for the last element). Examples of linear data structures include arrays, linked lists, stacks, and queues. Algorithms with a Branched Structure: Branched algorithms involve decision-making where the flow of execution can branch off based on certain conditions. This is often implemented using conditional statements like if, if-else, and switch. Conditional Instructions: if, if-else if-else: In C programming, if is used to specify a block of code to be executed if a specified condition is true: if-else is used to specify a block of code to be executed if the same condition is false: if-else if-else is used to specify new conditions to test if the first condition is false. Instruction switch: The switch statement in C allows us to execute one code block among many alternatives. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. The syntax of switch statement in C is as follows: switch (expression) { case value1: // statements break; case value2: // statements break; ... default: // default statements } In this syntax, the expression is evaluated once and its value is compared with the values of each case. If there is a match, the associated block of code is executed.
is encountered, the control of the program jumps out of the smallest enclosing loop or switch and starts executing the code that follows immediately after it. Goto Instruction: The goto statement in C is a jump statement which is sometimes also referred to as an unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function. The general form of the goto statement is: goto label; ... ... label: statement; In this syntax, label is an identifier that indicates the target statement. The use of goto statement may lead to code that is buggy and hard to follow, hence it’s generally discouraged.
11. Classification of data types in C. Types of predefined data. Classification of Data Types in C: Data types in C are classified into three types: Primitive (Built-in) Data Types: These are the most basic data types that are used for representing simple values such as integers, float, characters, etc. Examples include void, int, char, double, and float. Derived Data Types: These data types are derived from the primitive or built-in datatypes. Examples include Array, References, and Pointers. User Defined Data Types: These data types are defined by the user himself. Examples include Structure, Union, and Enumeration. Types of Predefined Data in C: The predefined data types in C include: Integer (int): Used to store integer numbers (any number including positive, negative and zero without decimal part). Range: - 2,147,483,648 to 2,147,483,647. Size: 4 bytes. Format Specifier: %d. Character (char): Used for declaring character type variables. The size of the character variable is 1 byte. Floating Point (float): Used to hold real numbers. The size of float (single precision float data type) is 4 bytes. Double (double): Used to hold real numbers with double precision. The size of double (double precision float data type) is 8 bytes. Void (void): An incomplete type. It means “nothing” or “no type”. You cannot create variables of void type. Short (short int) and Long (long int): If you need to use a large number, you can use a type specifier long. If you are sure, only a small integer ( [−32,767, +32,767] range) will be used, you can use short. Signed and Unsigned: In C, signed and unsigned are type modifiers. You can alter the data storage of a data type by using them: signed allows for storage of both positive and negative numbers; unsigned allows for storage of only positive numbers. 10. Operators and logic arithmetic expressions. Operators in C : Operators are symbols that operate on a value or a variable. They can be classified into different categories based on their functionality: Arithmetic Operators: Used to perform mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables). Examples: +, - , *, /, %. Assignment Operators: Used for assigning a value to a variable1. The most common assignment operator is =.
Increment and Decrement Operators: C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1. Relational Operators: Used for the comparison of the values of two operands. Logical Operators: Used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration3. We have 3 logical operators in the C language: Logical AND (&&), Logical OR (||), Logical NOT (!). Bitwise Operators: Used to perform bit-level operations on the operands. Logic Arithmetic Expressions in C: Arithmetic expressions are a combination of operands interjoined with arithmetic operators. They ultimately evaluate to a single value. Logical expressions yield 1 if the result is true and 0 if the result is false.
9. The classification of variables in C. Declaration of variables. Classification of Variables in C: Variables in C can be classified into several types: Local Variables: These are declared inside a function or a block of code. Their scope is limited to the block or function where they are declared. Global Variables: These are declared outside all functions and can be accessed from any function within the program. Static Variables: These retain their values between function calls. Automatic Variables: These are local variables that are declared within a function. Extern Variables : These are defined in another file and can be accessed from different files. Register Variables: These are stored in the register of microprocessor instead of RAM to have faster access. Declaration of Variables in C: The syntax to declare a variable in C specifies the name and the type of the variable. Here is the general syntax for declaring a variable in: data_type variable_name; // defining single variable data_type variable_name1, variable_name2; // defining multiple variables In this syntax, data_type is the type of data that a variable can store, and variable_name is the name of the variable given by the user. 12. Basic Data Types. Type modifiers. Basic Data Types in C: The C language provides four basic arithmetic type specifiers: Integer (int): Used to store integer numbers (any number including positive, negative and zero without decimal part). Range: - 2,147,483,648 to 2,147,483,647. Size: 4 bytes. Format Specifier: %d. Character (char): Used for declaring character type variables. The size of the character variable is 1 byte. Floating Point (float): Used to hold real numbers. The size of float (single precision float data type) is 4 bytes. Double (double): Used to hold real numbers with double precision. The size of double (double precision float data type) is 8 bytes. Type Modifiers in C: Modifiers are keywords in C which change the meaning of basic data types. They specify the amount of memory space to be allocated for a variable. Modifiers are prefixed with basic data types to modify the memory allocated for a variable. There are four type modifiers in C: Short: It limits user to store small integer values from - 32,768 to 32,767. It can be used only on int data type. Long: It allows user to stores very large number (something like 9 Million Trillion) from - 9223372036854775808 to 9223372036854775807. Syntax “long long” is used instead of “long int”. Signed: It is default modifier of int and char data type if no modifier is specified. It says that user can store negative and positive values.
In these examples, num[i] represents the i-th element in the array.
15. Pointers and operations with pointers in C. Pointers in C : A pointer in C is a variable that stores the memory address of another variable. Pointers are one of the core components of the C programming language. They allow low-level memory access, dynamic memory allocation, and many other functionalities. The syntax to declare a pointer in C is datatype *ptr; where ptr is the name of the pointer and datatype is the type of data it is pointing to. Operations with Pointers in C: There are several operations that can be performed on pointers in C: Pointer Declaration: To declare a pointer, we use the * dereference operator before its name. For example: int *ptr;. Pointer Initialization: Pointer initialization is the process where we assign some initial value to the pointer variable. We generally use the & address-of operator to get the memory address of a variable and then store it in the pointer variable. For example: int var = 10; int ptr; ptr = &var;. Dereferencing: Dereferencing a pointer is the process of accessing the value stored in the memory address specified in the pointer. We use the same * dereferencing operator that we used in the pointer declaration. Pointer Arithmetic: C allows arithmetic operations to be performed on pointers. These operations can help in accessing elements of arrays or other data structures. Changing Value Pointed by Pointers: We can change the value stored at a particular memory location using pointers. For example: int pc, c; c = 5; pc = &c; pc = 1; In this example, pc points to c, and we change the value at that memory location to 1 using pc = 1;. As a result, c will also be equal to. 16. The link between pointers and tables in C. In C programming, pointers and arrays (which can be thought of as tables) are closely related. An array name acts like a pointer constant. The value of this pointer constant is the address of the first element. For example, if we have an array named val then val and &val[0] can be used interchangeably. Here’s an example of how pointers can be used to access elements of an array: int x[5] = {1, 2, 3, 4, 5}; int ptr; // ptr is assigned the address of the third element ptr = &x[2]; printf("ptr = %d \n", ptr); // 3 printf(" (ptr+1) = %d \n", (ptr+1)); // 4 printf(" (ptr-1) = %d", *(ptr-1)); // 2 In this example, &x[2], the address of the third element, is assigned to the ptr pointer. Hence, 3 was displayed when we printed *ptr. And, printing *(ptr+1) gives us the fourth element. Similarly, printing *(ptr-1) gives us the second element. However, pointers and arrays are not the same. There are a few cases where array names don’t decay to pointers. 17. 1D table sorting using the linear selection. 1D table sorting using linear selection is often implemented using the Selection Sort algorithm in C programming. Here’s a high-level description of how it works: Initialization: Set min to the first location. Minimum Element Search: Search for the minimum element in the array.
Swapping: Swap the first location with the minimum value in the array. Reassign Min: Assign the second element as min. Repetition: Repeat the process until we get a sorted array. This algorithm iterates through the array and finds the smallest number in the array and swaps it with the first element if it is smaller than the first element. Then, it goes on to the second element and so on until all elements are sorted. The process of finding the minimum element from an array and putting it at the beginning is repeated until the entire array is sorted.
18. 1D table sorting using the selection and interchange method. Same as number 17.. 19. 1D array sort using the Bubble Sort. 1D table sorting using the Bubble Sort method in C programming involves the following steps: Initialization: Start at index zero. Comparison and Swapping: Compare the current element with the next one, and swap them if they are not in the intended order. Iteration: Move to the next element and repeat the comparison and swapping process. Pass Completion: After each pass, the largest element moves to the end of the array. Repetition : Repeat the entire process for all elements until the array is sorted. This algorithm works by repeatedly swapping adjacent elements if they are in the wrong order. The process continues until no more swaps are needed, indicating that the array is sorted. The time complexity of Bubble Sort is O(n²), where n is the number of items in the list. 20. 1D array sort using Insertion Sort. 1D table sorting using the Insertion Sort method in C programming involves the following steps. Initialization: Start at index one. Element Extraction: Extract the element at the current index. Element Comparison and Shifting: Compare this element with all elements in the sorted subarray (elements on the left). Shift all greater elements to the right. Element Insertion: Insert the extracted element at the correct location within the sorted subarray. Repetition: Repeat this process for all elements until the entire array is sorted. This algorithm works by building a sorted array one item at a time, with each item being inserted back into the previous array at the correct position. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages: it is simple, it is efficient for (quite) small data sets, it can sort a list as it receives it, and it can efficiently sort input that is already partially sorted. 21. 2D Tables. Declaration, initiation and processing of 2D tables. In C programming, 2D tables are often implemented using two-dimensional arrays. Here’s how you can declare, initialize, and process 2D tables: Declaration of 2D Tables : A 2D array in C is declared using the following syntax: datatype array_name[rows][columns];. For example, int num[10][5]; declares a 2D array named num that can store 50 elements in a table with 10 rows and 5 columns. Initialization of 2D Tables : A 2D array can be initialized at the time of declaration. For example: int num[3][4] = { {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11} };1. This creates a table with three rows and four columns and initializes it with the specified values. Processing of 2D Tables: Processing of 2D tables involves reading and writing operations on the elements of the array. For example, to read values into a 2D array:
printf("Before swap: a = %d, b = %d\n", a, b); swap(&a, &b); printf("After swap: a = %d, b = %d\n", a, b); return 0; } In this example, we have two functions: main() and swap(). The main() function declares two variables a and b, and passes their addresses to the swap() function using the & operator. The swap() function then uses pointers to modify the values of a and b, which are reflected in the main() function when it resumes execution.
24. Type and returning value of the function. Function call as an expression and as a statement In C programming language, a function can be of two types: value-returning and void. A value- returning function is one that returns a value to the calling function. The return type of the function specifies the type of value that the function returns. If the function does not return any value, it is called a void function. A function call is an expression that calls a function and returns its value. A function call can be used as an expression or as a statement. When used as an expression, the value returned by the function is used in an expression. When used as a statement, the function is called for its side effects, such as printing output to the console or modifying global variables. 25. Two-dimensional statically allocated array. Pointer to one-dimensional array. In C, a two-dimensional statically allocated array can be defined as follows: int arr[10][20]; Here, arr is a 2D array with 10 rows and 20 columns. A pointer to a one-dimensional array can be defined and used like this: int (*ptr)[20]; // pointer to an array of 20 integers ptr = arr; // assign the address of the first element of arr to ptr In this case, ptr is a pointer to an array of 20 integers. It’s assigned the address of the first element of arr, which is itself an array of 20 integers. Please note that the syntax for pointers to arrays can be tricky due to the precedence of the [] and * operators. The parentheses are necessary to correctly declare a pointer to an array. Without them, you would be declaring an array of pointers instead. For example, int *ptr[20]; declares an array of 20 pointers to integers, not a pointer to an array of 20 integers. 26. Passing (sending) one- and two-dimensional arrays into function. just give examples…….. 27. Static and dynamic memory allocation in C language. In C language, memory allocation is a process by which computer programs and services are assigned with physical or virtual memory space. The memory allocation is done either before or at the time of program execution. There are two types of memory allocations: Static Memory Allocation and Dynamic Memory Allocation. Static Memory Allocation: Static Memory is allocated for declared variables by the compiler. The address can be found using the address of operator and can be assigned to a pointer. The memory is allocated during compile time.
In the static memory allocation, variables get allocated permanently, till the program executes or function call finishes. It uses stack for managing the static allocation of memory. In Static Memory Allocation, there is no memory re-usability. In static memory allocation, once the memory is allocated, the memory size cannot change. In this memory allocation scheme, we cannot reuse the unused memory. In this memory allocation scheme, execution is faster than dynamic memory allocation. Example: This static memory allocation is generally used for array. Dynamic Memory Allocation: Memory allocation done at the time of execution (run time) is known as dynamic memory allocation. Functions calloc() and malloc() support allocating dynamic memory. In the Dynamic allocation of memory space is allocated by using these functions when the value is returned by functions and assigned to pointer variables. In the Dynamic memory allocation, variables get allocated only if your program unit gets active. Dynamic Memory Allocation is done during program execution. It uses heap for managing the dynamic allocation of memory. It is more efficient. In Dynamic Memory Allocation, there is memory re-usability and memory can be freed when not required. In dynamic memory allocation, when memory is allocated the memory size can be changed. This allows reusing the memory. The user can allocate more memory when required. Also, the user can release the memory when they need it. In this memory allocation scheme, execution is slower than static memory allocation. Example: This dynamic memory allocation is generally used for linked list. Please note that in both types of allocations, it’s important to manage your memory carefully to avoid leaks or crashes.
28. Functions for dynamic memory allocation, reallocation and deliberation in C language. In C language, dynamic memory allocation is performed using a group of built-in functions in the C library. The functions are defined in the <stdlib.h> header file. Here are the details: malloc(): The malloc() function allocates a specified number of bytes of memory and returns a pointer to the allocated memory. The syntax is as follows: ptr = (castType) malloc(size); For example: ptr = (float) malloc(100 * sizeof(float)); This statement allocates 400 bytes of memory (assuming the size of float is 4 bytes) and the pointer ptr holds the address of the first byte in the allocated memory. calloc(): The calloc() function allocates memory for an array of a specified number of elements, each of which is a specified size. The allocated memory is initialized to zero. The syntax is as follows: ptr = (castType) calloc(n, size); For example: ptr = (float) calloc(25, sizeof(float)); This statement allocates contiguous space in memory for 25 elements each of type float. realloc(): The realloc() function changes the size of the memory block pointed to by a specified pointer. The function returns a pointer to the newly allocated memory, which is suitably aligned for any built-in type and may be different from ptr. The syntax is as follows: ptr = realloc(ptr, newSize);
Selection Sort void selectionSort(int array[], int num) { for(int i = 0; i < num - 1; i++){ int min = i; for(int j = i + 1; j < num; j++){ if(array[min]>array[j]){ min = j; } } int temp = array[i]; array[i] = array[min]; array[min] = temp; } } Insertion Sort void insertionSort(int array[], int num) { for(int i = 1; i < num; i++){ int temp = array[i]; int j = i - 1; while(j >= 0 && array[j] > temp) { array[j+1] = array[j]; j--; } array[j+1] = temp; } }