Comp Prog Finals

Cards (68)

  • Repetition of action is a fundamental concept in computer programming, allowing efficient handling of large amounts of data. In C, three statements – while, for, and do – facilitate repetitive actions.
  • The while statement allows the programmer to specify an expression that must be true for the loop body to execute repeatedly until the condition becomes false.
  • The do-while statement executes its body first and then tests whether it should continue or terminate based on the specified condition.
  • In the for statement, the initialization, test, and increment/decrement expressions are executed once at the beginning of the loop, followed by repeated execution of the loop body as long as the test expression remains true.
  • WHILE statement: syntax: while (expression) statement;//next statement;
  • Fill the missing words:
    A) while
    B) next statement;
    C) false
    D) statement
  • Fill the missing words:
    A) for
    B) initialization
    C) condition;
    D) increment
    E) statement;
  • fill in the missing parts:
    A) nested for loop
    B) initialization1; condition1; increment1
    C) initialization2; condition2;
    D) statement;
  • fill in the missing parts:
    A) do
    B) initialization; condition; increment
    C) statement;
    D) zero
  • *Characteristics of an ARRAY:

    ▪ An array is a fixed-size, sequenced collection of elements of the same data type.
  • *Characteristics of an ARRAY:

    ▪ It is a sequence of data items that are of the same type, indexable, and stored contiguously.
  • *Characteristics of an ARRAY:

    ▪ Arrays represent a large number of homogeneous values
  • Array Representation:
    ▪ Elements are individually addressed through subscripts.
    ▪ The array as a whole has a name, but each member can be accessed individually using its subscript.

    Index Form:
    Number[0], Number[1], ..., Number[19]
    Subscript Form:
    Number0, Number1, ..., Numbern-1
  • a)Declaration and Definition:
    ▪ Array must be declared and defined before use.

    ▪ Syntax: storage class data-type array[expression];

    ▪ Examples:
    int ccmit[5];
    char bscs[30];
    float bsit[20];
    double ITCS[12];
  • b)Initialization:
    ▪ Initialization is done at declaration.

    ▪ Example:
    int x[4] = {1, 2, 3};

    ▪ Default values: Unassigned elements are filled with zeros.
  • c) Accessing Elements:

    ▪ C uses an index to access individual elements.

    ▪ Index is an integral value or expression.

    ▪ Example:
    scores[0], scores[i]
  • d) Storing Values in Array:

    ▪ Declaration and definition only reserve space.

    ▪ Values must be initialized, read from the keyboard, or assigned.

    ▪ Example:
    scores[4] = 23;
  • ● Introduction to Functions in C:

    Definition: A function is a section of a program that performs a specific task.

    Top-Down Approach: Effective problem-solving involves breaking down problems into manageable pieces using the function construct.
  • Advantages of Using Functions:

    Top-Down Design: Fits naturally with a top-down design approach.

    Reusability: Functions can be used multiple times in a program and in different programs.

    Team Collaboration: Facilitates dividing programming tasks among a team of programmers.

    ▪ Modularity: Functions can be tested individually, simplifying the debugging process
  • ● Function Declarations:

    ▪ Syntax: type function_name (parameter list)

    ▪ Examples:
    int ccmit (int bsit, int bscs);
    void ccmit ();
    float ccmit (float x, float y);
  • ● Function Definitions:

    ▪ The code describing what a function does is called a function definition

    ▪ General Form:
    type function_name (parameter list)
    {
    declaration → local variables statement;
    }
  • ● Local Variables to a Function:

    Variables declared in the function body are local to that function.
    Local variables are known only within their own code block.
    ▪ Exist only during the execution of the block in which they are declared.
  • ● Global Variables to a Function:

    ▪ Known throughout the entire program and may be used by any piece of code.
    ▪ Declared outside of any function.
    ▪ Use sparingly to avoid unnecessary memory usage and potential errors
  • ● The return Statement:

    ▪ Syntax: return; or return(expression);
    ▪ The value returned can be enclosed in parentheses
  • ● Function Prototypes:

    ▪ Functions should be declared before use.
    ▪ Use functional prototypes for better code checking.
    ▪ Example: double sqrt(double);
  • ● Giving Names to Parameters/Parameter Lists:

    Formal parameters are placeholders for actual parameters.
    ▪ Parameters in the function header are replaced by actual parameters when the function is called.
  • ● Function Invocation and Call By Value:

    ▪ Programs start with main()
    Functions are invoked by writing their names and arguments in parentheses.
    ▪ All arguments are passed by value.
  • ● Giving Names to Parameters/Parameter Lists:

    Formal parameters are placeholders for actual parameters.
    ▪ Parameters in the function header are replaced by actual parameters when the function is called.
  • ● Call by Reference or Variable Parameters:

    ▪ To change the value of a variable in the calling environment, use pointers
    ▪ Pass the address of the variable to achieve "call by reference"
  • ● STRING FUNCTIONS (string.h)
    COPY
    stpcpy (string1, string2) / strcpy (string1, string2)
    ✓ Copies contents of string2 to string1.

    o strncpy (target, source, count)
    ✓ Copies up to count characters from source to target.

    o strxfrm(target, source, count)
    ✓ Copies up to count characters from source to target. Returns the length of source
  • ▪ CONCATENATE

    o strcat (string1, string2)
    ✓ Concatenates string1 and string2.

    o strncat (string1, string2, count)
    ✓ Concatenates no more than count characters of string2 to string1.
  • ▪ COMPARES

    o strcmp (string1, string2) / strcoll (string1, string2)
    ✓ Compares string1 and string2. Returns:
    • < 0 if string1 is less than string2.
    • 0 if string1 is equal to string2.
    • 0 if string1 is greater than string2.

    o stricmp (string1, string2) / strcmpi (string1, string2)
    ✓ Compares ignoring cases.

    o strncmp (string1, string2, count) / strnicmp (string1, string2, count)
    ✓ Compares no more than count characters, ignoring cases
  • ▪ ERROR

    o _strerror(string)
    ✓ Displays custom error message followed by the most recent program error.

    o strerror(number)
    ✓ Returns a pointer to the error message associated with an error number.
  • ▪ CASE

    o strlwr (string)
    ✓ Converts the string to lowercase.

    o strupr (string)
    ✓ Converts the string to uppercase.
  • ▪ SET

    o strnset (string, c, count)
    ✓ Sets the first count characters in the string to the value of c.

    o strset (string, ch)
    ✓ Sets all characters in the string to the value of ch.
  • ▪ MATCH

    o strcspn (string1, string2)
    ✓ Returns the index of the first character in string1 that matches any in string2.

    o strspn (string1, string2)
    ✓ Returns the index of the first character in string1 that does not match any in string2.

    o strpbrk (string1, string2)
    ✓ Returns a pointer to the first matching character.

    o strrchr (string, c)
    ✓ Returns a pointer to the last occurrence of c in the string.
  • ▪ OTHERS

    o strchr (string, c)
    ✓ Returns substring from the first occurrence of c to the end.

    o strdup (string)
    ✓ Holds a duplicate of the string.

    o strlen (string)
    ✓ Returns the length of the string.

    o strrev (string)
    ✓ Reverses characters in the string
  • ● MATHEMATICAL FUNCTIONS (math.h)

    abs (int num)
    o Returns the absolute value of num.

    ceil (double num)
    o Returns the smallest integer not less than num represented as a double.

    fabs (double num)
    o Returns the float absolute value of num.

    floor (double num)
    o Returns the largest integer not greater than num, represented as a double.

    fmod (double x, double y)
    o Returns the remainder of x/y.

    pow (double base, double exp)
    o Returns base raised to the power exp.
    pow10 (int n) o Returns 10 raised to the power n. ▪ sqrt (double num) o Returns the square root of num.
  • ● CHARACTER FUNCTIONS (ctype.h)
    isalnum (int ch)
    o Returns non-zero if ch is a letter of the alphabet or a digit.
    isalpha (int ch)
    o Returns non-zero if ch is a letter of the alphabet; otherwise, zero.
    isdigit (int ch)
    o Returns non-zero if ch is a digit (0-9); otherwise, zero.
    islower (int ch)
    o Returns non-zero if ch is a lowercase letter (a-z); otherwise, zero.
    ispunct (int ch)
    o Returns non-zero if ch is a punctuation
  • isalnum (int ch)
    o Returns non-zero if ch is a letter of the alphabet or a digit.