General Flashcards
(16 cards)
what is the structure for fprintf
fprintf(‘format string’, variables);
what are the specifiers for an integer, a floating point number, float with 2 decimal places, scientific notation, string and a new line
Specifier | Meaning |
| ————- | ———————————– |
| %d
| Integer (decimal) |
| %f
| Floating-point number |
| %.2f
| Float with 2 decimal places |
| %e
| Scientific notation (e.g.1.23e+03)|
| %s
| String |
| \n
| New line |
structure of for loop matlab
for variable = start_value : increment : end_value
% statements (code to execute in each loop)
end
structure of while loop matlab
while condition
% statements (code to execute repeatedly)
end
structure of if statement matlab
if condition
% statements if condition is true
elseif another_condition
% statements if the elseif condition is true
else
% statements if all above conditions are false
end
structure of function matlab
function [output1, output2, …] = functionName(input1, input2, …)
% function body (statements to perform)
end
List 5 of the ways you can make your code more readable and maintainable
Use Meaningful Variable and Function Names
Add Comments
Use Proper Indentation and Formatting
Avoid Hardcoding Values (Use Constants or Variables)
Break Code into Functions
Libraries make coding faster, easier, safer, and more powerful by giving you tools that are ready to use.
🔹 Roles of Libraries in Programs:
Provide Predefined Functions & Tools
Libraries contain useful functions (e.g., for math, plotting, file handling) that can be used directly.
Increase Development Speed
You can focus on solving your problem rather than writing low-level code from scratch.
Enhance Code Reliability
Libraries are usually well-tested and debugged by experts, reducing the chance of errors in your code.
Improve Code Readability & Maintainability
Complex tasks can be handled by a single library function, making code shorter and easier to understand.
Enable Access to Advanced Features
Specialized libraries offer capabilities like machine learning, image processing, or signal analysis, which would be hard to develop on your own.
examples of libraries matlab
imread imshow
What does it mean for a variable to be “in scope”? C
A variable is “in scope” when it is accessible or can be referred to directly in a certain part of the program. If a variable is in scope, you can use its name to read or modify its value. If it’s out of scope, the variable is not visible or accessible from that part of the code.
What determines the scope of a variable in C?
The scope depends on where the variable is declared — inside a function/block (local scope) or outside (global/file scope).
List and give an example of three different defensive programming practices C
Input Validation
What: Always check that inputs to functions or programs are valid before processing them.
Why: Prevents unexpected behavior or crashes due to invalid data.
heck for NULL Pointers
What: Always verify pointers before dereferencing to avoid segmentation faults.
Why: Prevents crashes caused by dereferencing invalid or uninitialized pointers.
Use Assertions
What: Use assertions to check assumptions during development, catching logic errors early.
Why: Helps identify bugs by verifying conditions that should always be true.
Explain the difference between: 1) an int and a long int Include in your explanation when you would use each. C
int:
Usually a 32-bit signed integer on most modern systems (can represent values roughly from –2 billion to +2 billion).
Used for whole numbers within a moderate range.
Requires less memory (usually 4 bytes).
long int:
Typically also 32 bits on some systems, but often 64 bits on modern 64-bit systems, allowing much larger integer values (up to about ±9 quintillion for 64-bit).
Used when you need to store very large integer values beyond the range of int.
Takes more memory (4 or 8 bytes depending on system).
When to use:
Use int for most whole number calculations when you are confident the values won’t exceed the typical 32-bit range.
Use long int when working with large integers, such as file sizes, counts, or IDs that can be very large.
difference between float and double
Difference between float and double
float:
Single-precision floating-point number, typically 32 bits.
Has about 7 decimal digits of precision.
Uses less memory and is faster on some hardware but less accurate.
double:
Double-precision floating-point number, typically 64 bits.
Has about 15–16 decimal digits of precision.
Uses more memory but provides much higher precision and range.
When to use:
Use float when memory is limited or performance is critical and precision is less important (e.g., graphics, embedded systems).
Use double for scientific calculations, financial applications, or whenever more accuracy and precision are needed.
Explain the difference between call-by-reference and call-by-value. Give an
example of each in your explanation. C
Call-by-Value
When a function is called by value, the actual value of the argument is copied into the function’s parameter.
Inside the function, you work with this copy, so any changes to the parameter do not affect the original variable outside the function.
This is the default behavior in C.
Call-by-Reference
When a function is called by reference, the function receives the address (pointer) of the variable.
Changes made inside the function directly modify the original variable.
C does not have native call-by-reference, but this behavior is simulated by passing pointers.