Lecture 8 - Moving on to C++ Flashcards

1
Q

What are compound data types in C?

A

Compound data types allow multiple variables to be grouped under a single identifier. The members of these types need not be of the same type and are addressed by name. They are implemented via struct in C.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a struct in C?

A

A struct in C is a user-defined data type that groups together variables under one name, where each variable can be of a different type. Example: struct Fraction { int numerator; int denominator; };.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you declare and access a struct variable?

A

You declare a struct variable by defining the struct type and then creating an instance. Accessing members is done using the dot operator (.) or the arrow operator (->) when dealing with pointers. Example: f.numerator = 3; pf->denominator = 4;.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the typedef statement in C?

A

The typedef statement allows you to create an alternative name for a data type. It simplifies the declaration of struct variables. Example: typedef struct { int numerator; int denominator; } Fraction; allows you to use Fraction instead of struct Fraction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you multiply two fractions using a struct in C?

A

You can multiply two fractions by accessing and multiplying the numerator and denominator of each fraction. Example: h.numerator = f.numerator * g.numerator; h.denominator = f.denominator * g.denominator;.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is procedural abstraction in C?

A

Procedural abstraction is a programming principle where complex procedures or functions are broken down into simpler ones. It involves defining clear interfaces and implementation details, allowing a program to be understood at different levels of abstraction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the key components to define in a function for procedural abstraction?

A

You need to define the interface to the function, the conditions under which it will operate correctly, what the function does, any side effects, and the exceptions/errors it might raise.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of initFraction in C?

A

The initFraction function initializes the members of a Fraction struct with specific values. Example: void initFraction(Fraction *pf, int numerator, int denominator) assigns the numerator and denominator values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you make a program modular using procedural abstraction?

A

You can break the program into smaller functions that perform specific tasks, hiding implementation details within each module and focusing on the interface. This modular approach allows easier understanding and maintenance of large programs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the advantage of using abstract data types (ADTs) in C?

A

Abstract data types (ADTs) hide the implementation details of a data structure, exposing only a set of functions for manipulating the data. This makes the program more modular, reduces complexity, and allows changes in implementation without affecting the code that uses the ADT.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does it mean to encapsulate data within an abstract data type (ADT)?

A

Encapsulation in an ADT means that the data is not directly accessible. Instead, a set of interface functions provides controlled access to the data, ensuring consistency and allowing error checking (e.g., ensuring a denominator is never zero in a fraction).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can you change the internal representation of a struct without affecting the client code?

A

By using abstract data types (ADTs), the internal representation of a struct can be modified, as long as the interface functions remain consistent. Client code will continue to work without changes since it interacts only with the interface, not the implementation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the role of the printFraction function in C?

A

The printFraction function displays the numerator and denominator of a Fraction struct in the format numerator/denominator using formatted output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is an example of error checking in an abstract data type?

A

An example of error checking in an ADT is ensuring that the denominator of a fraction is never zero. This can be implemented in the interface functions, which manage access to the data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a key benefit of modular and weakly coupled programs?

A

Modular and weakly coupled programs are easier to maintain and debug because each module or component performs a specific task and can be changed or replaced without affecting the rest of the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a more elegant version of multiplying fractions in C?

A

A more elegant version involves encapsulating the logic within a function like Fraction multiply(Fraction *pf, Fraction *pg), which creates a result Fraction struct and returns the product of two fractions.

17
Q

What is the advantage of returning a Fraction struct directly in a function?

A

Returning a Fraction struct directly simplifies the code by reducing the need for pointer manipulation. It allows for cleaner and more readable code, especially when creating new fractions or performing operations like multiplication.

18
Q

How does the typedef improve code readability in C?

A

typedef improves readability by allowing you to create simpler names for complex data types, such as Fraction instead of struct FractionStruct. This leads to cleaner and more concise code.

19
Q

What does weak coupling in modular programming refer to?

A

Weak coupling refers to the practice of designing modules so that they are minimally dependent on each other. This makes it easier to modify or replace one module without affecting the others.

20
Q

What is the primary purpose of using modularity in large programs?

A

Modularity in large programs helps manage complexity by breaking down the program into smaller, manageable units. Each unit focuses on a specific task and can be developed, tested, and debugged independently.