Fundamentals of Programming and Data Structures Study Guide
Mathematical and Logical Operators in Programming
- The modulus operator () is used to determine the remainder of a division operation.
- To determine if a number is even, the condition is used.
- Examples of remainder calculations:
- , which indicates the number is even.
- , which indicates the number is odd.
- Comparison of standard operators:
- is used for division.
- is used for multiplication.
- is used for subtraction.
- (represented by ) is used for the remainder.
Array Fundamentals and Data Structures
- An array is a data structure that can only store elements of the same data type. It cannot store different types of data simultaneously.
- Array indexing always begins at the number .
- The first element is located at .
- The second element is located at .
- This pattern continues sequentially.
- To find the index of the last element in an array, use the formula: .
- Example: In an array consisting of elements, the index of the last element is .
- Array Dimensions:
- A one-dimensional array (1D array) consists of a single row containing multiple columns.
- A two-dimensional array (2D array) consists of multiple rows and columns. For instance, an array with rows and columns is a two-dimensional array.
Functions and Subroutines
- The primary purposes of using functions in programming include:
- Reducing code repetition.
- Increasing program execution speed.
- Decreasing the overall size of the program code.
- Facilitating easier maintenance and modification of the software.
- Functions do not execute automatically within a program. They must be explicitly called by writing their name in the source code.
- Function call syntax: A function is called by writing its name followed by parentheses, such as .
- Incorrect methods of calling include using keywords like "call", "run", or "execute" (e.g., is incorrect).
- Return Types:
- The
voidkeyword signifies a function that does not return any value. - Functions that return values must specify the data type, such as
intfor integers,doublefor decimal numbers, orStringfor text. - The
returnkeyword is used to send a value back from a function, and the value must match the declared return type of the function.
- The
Control Structures and Looping
- The
forloop is considered the most appropriate tool for handling array elements because it provides clear and direct control over the index.- Example syntax: .
- Other structures include:
whileanddo-whileloops.if, which is a conditional statement (branching), not a loop.
Algorithms and Flowchart Logic Scenarios
Even/Odd Logic Test:
- If a user inputs the number into an algorithm checking for evenness using :
- Calculation: .
- Results: Since the remainder is not , the condition is not met, and the program follows the "No" path to print "Odd" (فردي).
Maximum Value Selection:
- If an algorithm compares two variables, where and :
- Logic: The program checks which value is larger. Since , the output is .
Summation of Odd Numbers Algorithm:
- Goal: Calculate the sum of odd numbers from to .
- Identified Error: Setting the initial value of the sum variable to ().
- Correction: The variable must be initialized to () to ensure the total is accurate and only reflects the sum of the odd numbers without an incorrect starting offset.
- Process logic: The counter starts at and increments by in each step () until the limit of is reached.
Financial Eligibility Logic (Marriage Loan):
- Scenario: A marriage loan of SAR is granted based on salary requirements.
- Eligibility Condition: An employee's monthly salary () must be SAR or less.
- Logical Condition: .