CSC204: Intro to Computer Science II

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which of the following is the primary objective when actively participating in computer science labs?

  • To solely focus on typing and compiling programs to meet deadlines.
  • To type, compile programs, and, more importantly, understand the underlying logic. (correct)
  • To prioritize the completion of lab exercises over understanding program functionality.
  • To compete with peers in typing speed and debugging efficiency.

In the context of top-down design, what is a 'stub'?

  • A comment inserted at the beginning of each function.
  • A fully functional component ready for integration.
  • A substitute component employed temporarily for compilation or testing. (correct)
  • A detailed diagram illustrating the program's control flow.

What is the main advantage of using meaningful identifiers in programming?

  • Enhances program security by obfuscating variable functions.
  • Reduces the amount of typing required, speeding up the coding process.
  • Minimizes compilation time by shortening variable names.
  • Improves program readability and understandability. (correct)

What is the primary purpose of comments in a computer program?

<p>To provide documentation within the code, explaining the purpose and function of different sections. (A)</p>
Signup and view all the answers

Which factor does NOT directly affect the running time of a program?

<p>The number of comments in the source code. (D)</p>
Signup and view all the answers

What does Big O notation primarily express?

<p>The upper bound of an algorithm's running time in terms of input size. (A)</p>
Signup and view all the answers

What is the purpose of 'bench testing' in software development?

<p>To have programmers review another's code to identify potential issues and agree on tests. (B)</p>
Signup and view all the answers

What action does the preprocessor perform when it encounters a #include directive?

<p>It includes the specified file into the program before compilation. (C)</p>
Signup and view all the answers

If the if statement expression evaluates to zero, which statement gets executed?

<p>next statement. (D)</p>
Signup and view all the answers

What is the key difference between break and continue statements in loops?

<p><code>break</code> exits the loop entirely, while <code>continue</code> skips the rest of the current iteration. (C)</p>
Signup and view all the answers

What is the data type for controlling expression in a switch statement?

<p>Integral. (C)</p>
Signup and view all the answers

Int sum = 0; for (i = 1; i <= 5; ++i) sum += i;

<ol start="15"> <li>(C)</li> </ol>
Signup and view all the answers

In the context of arrays in C, what does the term 'subscript' refer to?

<p>An index used to access individual elements within the array. (B)</p>
Signup and view all the answers

How does C handle the indexing of arrays?

<p>Arrays are indexed starting from 0. (B)</p>
Signup and view all the answers

Given the declaration float arr[10];, which of the following is the correct way to access the first element of the array arr?

<p>arr[0] (B)</p>
Signup and view all the answers

What is a function prototype?

<p>A declaration that specifies the function's return type, name, and parameter list. (D)</p>
Signup and view all the answers

What happens if a function definition does not include a return statement?

<p>The function returns control naturally to the calling environment upon reaching the end of the function body. (D)</p>
Signup and view all the answers

In a function prototype, what is the purpose of the 'void' keyword in the parameter list?

<p>It indicates that the function does not accept any arguments. (C)</p>
Signup and view all the answers

Given the macro definition #define SQUARE(x) x * x, what will SQUARE(a + b) be expanded to?

<p>a + b * a + b (C)</p>
Signup and view all the answers

Why are macros considered 'inherently unsafe' in C programming?

<p>The preprocessor does not check for syntactic correctness. (B)</p>
Signup and view all the answers

What is the purpose of the typedef keyword in C?

<p>To assign an alternative name to an existing type. (B)</p>
Signup and view all the answers

Which standard library function is used to calculate the square root of a number?

<p>sqrt() (D)</p>
Signup and view all the answers

Which format specifier is used with printf() to print an integer in hexadecimal format?

<p>%x (C)</p>
Signup and view all the answers

How are char and int variables related in C?

<p>Both can be used to represent characters, and arithmetic expressions involving characters are valid. (D)</p>
Signup and view all the answers

Assume the max value for int is 32000, what type will the constant 33000 be assigned to in C if no suffix is specified?

<p>long. (B)</p>
Signup and view all the answers

What is the purpose of the sizeof operator in C?

<p>To determine the number of bytes required to store a variable. (A)</p>
Signup and view all the answers

What is 'casting' in C programming?

<p>Explicitly converting an expression from one data type to another. (D)</p>
Signup and view all the answers

What happens when there is a conflict between the identifier and the type name enum?

<p>The names are handled separately and there is no conflict. (C)</p>
Signup and view all the answers

What is the primary difference between getchar() and scanf() when reading character input?

<p><code>scanf()</code> is used for formatted input, while <code>getchar()</code> reads a single character. (B)</p>
Signup and view all the answers

In C, what happens if a variable is declared as an integer but assigned a character value?

<p>The character value is implicitly converted to its ASCII integer representation. (C)</p>
Signup and view all the answers

What does the mnemonic EOF stand for in C programming?

<p>End Of File. (B)</p>
Signup and view all the answers

In regard to parameters, what are pointers usually used for?

<p>They are frequently for the call-by-reference of the called parameter. (B)</p>
Signup and view all the answers

What best decribes the scope of an identifier?

<p>The part of the source code in which the identifier is known or accessible. (A)</p>
Signup and view all the answers

Which of the following statements about array names is correct?

<p>They are pointer constants, and their value cannot be changed. (D)</p>
Signup and view all the answers

When are dynamic memory allocations essential?

<p>When the size of an array only becomes clear at runtime. (D)</p>
Signup and view all the answers

What are the return types of the calloc() and malloc() functions?

<p>void *. (B)</p>
Signup and view all the answers

What is the purpose of a null character ('\0') in a C-style string?

<p>To mark the end of the string. (B)</p>
Signup and view all the answers

What is the difference between strcpy() and strncpy()?

<p><code>strncpy()</code> only copies a specified number of characters. (C)</p>
Signup and view all the answers

Which function searches for the first occurence of the substring within a string?

<p>strstr() (D)</p>
Signup and view all the answers

How are arguments passed to the main() function?

<p>Through command-line arguments, using <code>argc</code> and <code>argv</code>. (A)</p>
Signup and view all the answers

Which of the following is a file pointer automatically available in C?

<p>stdin (B)</p>
Signup and view all the answers

What does the fopen() function return if the file cannot be opened?

<p>NULL (B)</p>
Signup and view all the answers

What file operation must be done before a file is written to or read from?

<p>Open. (A)</p>
Signup and view all the answers

What is one difference between struct and union types?

<p>Variables of <code>struct</code> are located in sequential, while <code>union</code> variables overlap. (D)</p>
Signup and view all the answers

What is recursion?

<p>A function calls itself. (C)</p>
Signup and view all the answers

Which of the following describes sequential search?

<p>The algorithm needs a pre-sorted dataset. (C)</p>
Signup and view all the answers

Flashcards

Structured Programming

A disciplined approach to writing programs that are easier to test, debug, and modify.

Top-Down Design

Repeatedly breaking down a big problem into smaller, manageable ones.

Stub

A substitute component employed temporarily in a program during development.

Simplicity in coding

Striving for code that is easy to read and understand.

Signup and view all the flashcards

Meaningful Identifier Names

Choosing variable names that clearly indicate their purpose.

Signup and view all the flashcards

Documentation and Comments

Adding explanations within code to clarify statements and logic.

Signup and view all the flashcards

Program Readability

Writing programs that are easy to read and understand through indentation.

Signup and view all the flashcards

Algorithm

A general method for solving a problem in Computer Science.

Signup and view all the flashcards

Efficiency

Measure of an algorithm's impact on computer resources (time and space).

Signup and view all the flashcards

Big O Notation

Expressing the running time of an algorithm in terms of N while ignoring constants.

Signup and view all the flashcards

Correctness of Algorithms

Verifying that a program works correctly for a particular set of data through testing.

Signup and view all the flashcards

#include directive

A pre-processor directive that includes the specified file into the program before compilation takes place.

Signup and view all the flashcards

Header files

Function prototypes included in the program, the compiler will not work properly if any of the I/O functions are called in the program.

Signup and view all the flashcards

Branch Statements

These statements alter the sequential flow of programs so that one of several possible paths is followed if a given condition holds.

Signup and view all the flashcards

Break Statement

Causes an exit from the innermost enclosing loop or switch statement

Signup and view all the flashcards

Continue Statement

Causes the current iteration of the loop to end and next iteration to begin immediately.

Signup and view all the flashcards

Switch Statement

A multiway conditional statement generalizing the if-else statement.

Signup and view all the flashcards

Conditional Operator (?)

Is ternary, i.e., it takes three expressions as operands.

Signup and view all the flashcards

Iteration (Loops)

Is used to repeatedly execute a statement or a block of statements.

Signup and view all the flashcards

Array

A composite data structure comprising items of the same type.

Signup and view all the flashcards

Function Definition

Code that is made up of three parts.

Signup and view all the flashcards

Typedef

Type definition to simplifies complicated or lengthy user-defined types.

Signup and view all the flashcards

#define

It used to define a macro, and can be used to do a lot more than defining constants.

Signup and view all the flashcards

Types char, signed char, and unsigned char

Each require one byte of storage. Type signed char has values -128 to 127, while type unsigned char has values 0 to 255

Signup and view all the flashcards

sizeof Operator

Is used to determine the number of bytes that your machine requires to store a variable of the specified type.

Signup and view all the flashcards

Conversions and Casts in C

Causes the value of i to be converted to double.

Signup and view all the flashcards

Floating Types (float, double, long double)

Floating type variables hold real values. Depending on the machine, a variable of type float may require less storage than a variable of type double.

Signup and view all the flashcards

scanf() Function

Function returns the number of successful conversions of made (i.e., of the data string read from the input stream and converted to the specified data type).

Signup and view all the flashcards

Print Func

Returns the name of the functions.

Signup and view all the flashcards

Pointers

Used to access memory and manipulate memory addresses.

Signup and view all the flashcards

Pointers to void

If it is declared as a pointer to void

Signup and view all the flashcards

Call-by-Reference

It is the addresses (not copies of values) of the variables that are passed to the function, and if the contents of these memory addresses are changed within the function

Signup and view all the flashcards

Scope of Identifiers

The part of the source code in which the identifier is known or accessible.

Signup and view all the flashcards

Array

A sequence of data items of the same type

Signup and view all the flashcards

Passing Arrays to Functions

base address of the array is passed to the function call-by-value

Signup and view all the flashcards

Dynamic Memory Allocation

There is a need of dynamic memory allocation by using pointers.

Signup and view all the flashcards

Strings

A 1-D array of type char

Signup and view all the flashcards

Function strings

String handling functions form part of the standard library, but their prototypes are given in the string.h header file.

Signup and view all the flashcards

Structures

Is used to aggregate a collection of data items possibly of different types.

Signup and view all the flashcards

Assign values ()

Assign values to the 52 cards in a deck

Signup and view all the flashcards

Recursion

In which a function invokes itself

Signup and view all the flashcards

Loop Break

Search function finds

Signup and view all the flashcards

Study Notes

  • CSC204: Introduction to Computer Science II builds upon CSC203 for students minoring in Computer Science.
  • The course continues studying the C programming language and introduces writing reports using word processors.
  • Programming is best learned by example, with labs serving as an essential course component.
  • Students are strongly advised to actively understand the reason programs work.

Course Structure:

  • Lectures are scheduled:
    • Tuesdays, 17:00 – 19:00 at Restau VI
    • Thursdays, 07:00 – 09:00 at CRB I 150 A
  • Problem-solving exercises are incorporated into lectures, replacing formal tutorials.
  • Students must register for laboratory groups.
  • Examinations consist of 30% continuous assessment and 70% end-of-semester examination according to University regulations.
  • End-of-semester examinations emphasize laboratory work to reward consistent effort.
  • There will be at least one written test, with a possible second programming project or assignment.
  • Students receive feedback on tests and can verify continuous assessment marks before final examinations.
  • Missing a test requires an official letter to the Vice-Dean of the Faculty of Science within 72 hours, with supporting documents.
  • Only medical reports from the University's medical doctor are considered for medical excuses, with final decisions made by the University Senate.
  • Students are advised to read the University of Buea Students’ Guide, particularly the section on examination fraud to avoid issues.
  • Examination fraud includes all forms of cheating aimed at unfairly influencing the score, not just during examinations.
  • Computer science: An overview (7th ed.) by Brookshear, J. G. (2003)
  • C by Dissection (4th ed.) by Kelley, A., & Pohl, I. (2000)
  • The C programming language: ANSI C version by Kernighan, B., & Ritchie, D. (1988)

Course Outline:

  • Techniques in program development
  • Introductory Notions in Algorithms Design
    • Efficiency
    • Introduction to the analysis of algorithms
    • Correctness
    • Notion of proofs
  • Flow of control in C
    • Branch construct revisited
    • Break and continue statements
    • Switch statement
    • The conditional operator
    • Iteration revisited
    • Arrays revisited
  • Functions and Structured Programming
    • Use of functions in top-down design
    • Mathematical functions
    • Functions vs Macros
  • Data types revisited
    • Fundamental Data Types
    • Conversions and Casts in C
    • Enumerated Types
    • User-defined types
  • Character processing.
  • Functions and pointers
    • Parameter passing
    • Scope of identifiers
    • Side effects
  • Arrays and Pointers
  • Strings and pointers
  • Structures and Abstract Data Types
  • Introduction to Recursion
  • Simple Search and Sort Algorithms
  • Introduction to File Processing
  • Introduction to some major areas of Computer Science
    • Operating systems
    • Databases
    • Artificial intelligence
    • Computer Networks
    • Parallel Distributed Computing
    • Trends in Computer Architecture

Techniques in Program Development:

  • Elegant programs are written using techniques to enhance programming style and design, especially useful for large projects.

Top-Down Design:

  • Top-down design, or stepwise refinement, involves breaking down a large problem into smaller, manageable parts as part of the evolution of structured programming.
  • The original problem is refined into easily coded sub-problems.
  • The main program is written first, with complex tasks delegated to functions (procedures).
  • Stubs can represent functions, allowing compilation and testing of the main program before functions are fully written.
  • Stepwise refinement applies recursively to functions, breaking them into smaller sub-functions.

Advantages of Top-Down Approach:

  • Division of a complex problem into smaller, easier sub-problems (divide and conquer).
  • Enables independent work on modular parts, even if some parts are initially beyond the programmer's ability.
  • Facilitates independent testing of functions, simplifying debugging.
  • Allows planning of the program structure extensively before writing code.

Top-Down Design Example: Computing Class Average:

  • A program to compute the class average for an exam uses a top-down design.
  • Stage I, is the top level, involves determining the class average for the test.
  • Stage II, is the first refinement, divides top level into smaller tasks:
    • Initialize variables.
    • Input, sum up, and count the number of grades.
    • Calculate and print the class average.
  • Stage III, is the second refinement, breaks down each step from Stage II:
    • Running total and count of grades are needed - variables for average and individual grades are required.
    • Grades uses a loop to input each grade, as the exact number of grades is unknown.
    • A sentinel-controlled loop is suitable, with -1 as the sentinel value.
    • Two possibilities to calculate and print the class average are considered:
    • If at least one grade, computes the average.
    • Informs the user that "no grades were entered", avoiding division by zero.

Simplicity:

  • Code should be easy to read and understand.
  • Use the simplest possible approach to solve your problem.

Premature Optimization:

  • Consider the trade-off between programming costs and program efficiency.
  • Optimize when the time saved justifies the programmer's time.

Meaningful Identifier Names:

  • Variable names should be informative to enhance understanding (e.g., avg instead of x).
  • Function names should describe the function's purpose (e.g., read_data).

Documentation and Comments:

  • Comments are key to explaining code statements and function.
  • Act as documentation within the code.
  • User manuals and technical documents are required for a complete project.

Program Readability:

  • Indentation improves code readability and error detection:
    • Indent statements within a block.
    • Similarly indent blocks of code nested in other blocks.

Algorithmic Efficiency:

  • An algorithm is a general method, whereas a program is its language-specific implementation.
  • Efficiency measures a program's impact on computer resources.
  • Time, measured by running time, is influenced by:
    • Computer language with source code.
    • Computer speed.
    • Compiler.
    • Operating system.
    • System load.
  • Computer scientists prefer counting the number of steps proportional to running time.
  • An algorithm with N/2 steps is twice as fast as one taking N steps.

Efficiency Example: Summing a Sequence of Numbers:

  • First algorithm uses a loop.
  • Second algorithm uses Gauss's formula i.e. (*sum of the first and last number) * N/2 to sum number series.

Number of Statements:

  • The first algorithm involves N + 4 steps, proportional to N (linear).
  • The second algorithm uses 4 statements, irrespective of N (constant).
  • Gauss's algorithm is preferable for large N values.

Big O Notation:

  • It expresses running time in terms of N.
  • Constant multiples or smaller terms are ignored.
  • Examples include:
    • O(1): Constant time
    • O(logâ‚‚N): Logarithmic time
    • O(N): Linear time
    • O(N²): Quadratic time
    • O(N³): Cubic time
    • O(2^N): Exponential time.
  • Algorithms with exponential complexity are inefficient except for small N.
  • Big O notation extends to the use of memory, i.e., space.

Correctness of Algorithms:

  • Testing shows that a program works for a particular data set.
  • Testing helps increase confidence that the program works correctly.

Examples of Testing Methods:

  • Bench Testing: Programmer walks through their code with a small group.
  • Static Analysis: Analyzers spot errors compilers miss.
  • Trace Tools: Track variable changes to spot design problems.
  • Data Testing: Generate test data automatically.

Notion of Proofs:

  • Program proofs verify that a program will always produce correct results with assertions.
  • Focus on the implementation through program proof.
  • Correctness of machine code may not always obey the rules.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Use Quizgecko on...
Browser
Browser