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 XX is even, the condition X%2==0X \% 2 == 0 is used.
  • Examples of remainder calculations:
    • 4%2=04 \% 2 = 0, which indicates the number is even.
    • 7%2=17 \% 2 = 1, which indicates the number is odd.
  • Comparison of standard operators:
    • // is used for division.
    • ∗* is used for multiplication.
    • −- is used for subtraction.
    • Modulus\text{Modulus} (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 00.
    • The first element is located at index 0\text{index } 0.
    • The second element is located at index 1\text{index } 1.
    • This pattern continues sequentially.
  • To find the index of the last element in an array, use the formula: Total number of elements−1\text{Total number of elements} - 1.
    • Example: In an array consisting of 1010 elements, the index of the last element is 99.
  • 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 33 rows and 55 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 DisplayMyInfo()\text{DisplayMyInfo()}.
    • Incorrect methods of calling include using keywords like "call", "run", or "execute" (e.g., run DisplayMyInfo()\text{run DisplayMyInfo()} is incorrect).
  • Return Types:
    • The void keyword signifies a function that does not return any value.
    • Functions that return values must specify the data type, such as int for integers, double for decimal numbers, or String for text.
    • The return keyword is used to send a value back from a function, and the value must match the declared return type of the function.

Control Structures and Looping

  • The for loop is considered the most appropriate tool for handling array elements because it provides clear and direct control over the index.
    • Example syntax: for(int i=0;i<10;i++)for(int\,i = 0; i < 10; i++).
  • Other structures include:
    • while and do-while loops.
    • 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 1919 into an algorithm checking for evenness using X%2==0X \% 2 == 0:
    • Calculation: 19%2=119 \% 2 = 1.
    • Results: Since the remainder is not 00, 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 A=15A = 15 and B=20B = 20:
    • Logic: The program checks which value is larger. Since 20>1520 > 15, the output is 2020.
  • Summation of Odd Numbers Algorithm:

    • Goal: Calculate the sum of odd numbers from 11 to 5050.
    • Identified Error: Setting the initial value of the sum variable to 1010 (Sum=10\text{Sum} = 10).
    • Correction: The variable Sum\text{Sum} must be initialized to 00 (Sum=0\text{Sum} = 0) 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 11 and increments by 22 in each step (1,3,5,7,…1, 3, 5, 7, \dots) until the limit of 5050 is reached.
  • Financial Eligibility Logic (Marriage Loan):

    • Scenario: A marriage loan of 60,00060,000 SAR is granted based on salary requirements.
    • Eligibility Condition: An employee's monthly salary (SS) must be 10,00010,000 SAR or less.
    • Logical Condition: S≤10,000S \le 10,000.