1/59
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What is the basic structure of a C program?
#include <stdio.h>
int main(void)
{
//Declare variables
//Input
//Program Logic (loops, conditions)
//output
return 0;
}What does #include <stdio.h> do?
It allows the program to use input/output functions like:
printf()
scanf()How do you declare variables in C?
int x;
float y;
char c;Variables store data in the program.
When should variables be declared?
At the top of a function, usually right after {.
What does printf() do?
Prints output to the screen.
printf("Hello");
What does scanf() do?
Reads input from the user.
scanf("%d", &x);
& means the address of the variable.
Common format specifiiers
%d -> integer
%f -> float
%c -> characterWhat does #define do?
Creates a constant value.
#define SIZE 200
Used often with arrays.
What arithmetic operators are used in C?
+ addition
- subtraction
* multiplication
/ division
% modulus (remainder)When do you use % (modulus)?
To check divisibility.
if (num % 2 == 0)
Checks if a number is even.
When do you use an if statement?
When the program must make a decision.
if (x > 0)
{
printf("Positive");
}What does an if-else statement look like?
if (condition)
{
//code if true
}
else
{
//code if false
}Common relational operators
== equal (comparison)
!= not equal
> greater than
< less than
>= greater or equal
<= less or equalWhen do you use a for loop?
When you know how many times the loop should run.
Example:
for (i=0; i<n; i++)
{
printf("%d", i);
}Often used with arrays.
Structure of a for loop
for (initialization; condition; update)
{
statements;
}Example:
for (i=0; i<10; i++)
When do you use a while loop?
When a loop runs while a condition is true, and the number of repetitions is not known.
Example:
while (x>0)
{
printf("%d", x);
}Structure of a while loop
while(condition)
{
statements;
}When do you use a do-while loop?
When the loop must run at least once.
Example:
do
{
printf("Hello");
}
while(x>0);What does a break do?
Immediately exits a loop/
Example:
if (x == 5)
{
break;
}What is an array?
A collection of variables of the same type stored together.
Example:
int numbers [10];What are valid array indexes?
If array size is:
int arr[10];
//Indexes are 0 to 9How do you read values into an array?
for(i = 0; i<n; i++)
{
scanf("%d", &arr[i]);
}How do you print values from an array?
for (i=0; i<n; i++)
{
printf("%d", arr[i]);
}How do you search for a number in an array?
for (i=0; i<n; i++)
{
if (arr[i] == key)
{
// found it
}
}What is a function?
A reusable block of code that performs a specific task.
Example:
int square(int x)
{
return x * x;
}What is a function prototype?
Declares the function before main.
Int square(int x);
How do you call a function?
result = square(5);
When do you use a switch statement?
When selecting one option from multiple choices.
Often used for menus.
Structure of a switch statement
switch(x)
{
case 1:
statements;
case 2:
statements;
break;
default:
statements;
}Why is break important in a switch?
without break, the program continues executing the next cases.
What are common causes of compile errors? (Hint: 6 common mistakes)
missing ;
missing }
wrong variable name
using variable before declaring it
forgetting & in scanf
mismatched function names
What should you do first when given a lab exam prompt?
Write main structure
Declare variables
Write input section
Write loops/logic
Write output section
Compile and fix errors
What is a flag variable?
A variable used to remember whether something happened during a loop.
int found = 0;
if(arr[i] == key)
{
found = 1;
}often used when searching for arrays
When should you use a flag variable?
Use a flag when you need to track:
if a value was found
if a condition occurred in a loop
if a number is prime
Example:
Searching Arrays
Prime Checking
Validating input
What does i++ and ++i mean?
i++ is a post-increment operator, which increments the value of ‘i’ but returns the original value that ‘i’ held before being incremented. Equivalent to i = i + 1;
++i is a pre-increment operator, which increments the value of ‘i’ immediately and returns the increments value.
What does i- - and - -i mean?
i- - is a post-increment operator, which decrements the value of ‘i’ but returns the original value that ‘i’ held before being decremented. Equivalent to i = i - 1;
- -i is a pre-increment operator, which decrements the value of ‘i’ immediately and returns the decrement value.
What does continue do?
Skips the rest of the current loop iteration and goes into the next iteration.
if(x < 0)
{
continue;
}Difference between break and continue?
Break exits the loop completely
Continue skips to the next iteration
What is a nested loop?
A loop inside of another loop.
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
printf("%d", arr[i][j]);
}
}Often used for:
comparing arrays
pattern printing
searching combinations
When are nested loops commonly used?
comparing two arrays
checking all combinations
matrix-style problems
searching multiple lists
How do you sum elements of an array?
sum = 0;
for(i = 0; i < n; i++)
{
sum = sum + arr[i];
}How do you calculate average of an array?
average = sum / n;After computing the sum.
How do you find the largest number in an array?
largest = arr[0];
for(i=1; i<n; i++)
{
if(arr[i] > largest)
largest = arr[i];
}How do you find the smallest number in an array?
smallest = arr[0];
for(i=1; i<n; i++);
{
if(arr[i] < smallest)
smallest = arr[i];
}How do you search for a number in an array?
for(i = 0; i < n; i++)
{
if(arr[i] == key)
{
printf("Found");
}
}What is linear search?
Checking each element of the array one by one until the value is found.
How do you reverse an array?
Swap elements from the front and back.
Example logic:
temp = arr[i];
arr[i] = arr[n-1-i];
arr[n-1-i] = temp;When are menu-driven programs used?
When the user chooses options from a list.
Example:
1. Add
2. Subtract
3. ExitUsually uses switch + loop
What is the typical structure of a menu program
do
{
print menu
scanf choice
switch(choice)
{
case 1:
...
break;
case 2:
...
break;
}
} while(choice != exit);What type must a case label be?
A constant value.
Example:
case 1:
case 2:
case 3:Expressions are not allowed
What causes infinite loops?
condition never becomes false
update statement missing
incorrect loop condition
Example mistake:
for(i = 0, i < 10;)Basic logic for checking if a number is prime
Assume the number is prime
Try dividing from 2 to n-1
if divisible → not prime
Example:
for(i = 2; i < n; i++);
{
if(n % 1 == 0)
not prime
}What does pass-by-value mean?
Functions receive a copy of the variable, not the original.
Changes inside the function do not affect the original value.
How do you pass an array to a function?
void process(int arr[], int size)Call:
process(arr, n);What does tracing code mean?
Following the program step-by-step to determine what it prints
Often used with:
loops
arrays
counters
How should you approach a coding lab problem?
Read prompt carefully
identify needed tools (loops, arrays, switch)
Write program structure
Add logic step by step
Compile frequently
Most common pattern in array problems
read n
read array
loop through array
apply condition
print resultStandard loop for processing an array
for(i = 0; i < n; i++)
{
// work with arr[i]
}Structure of a function
returnType functionName(parameters)
{
//statements
return value;
}Example:
int square(int x)
{
return x * x;
}Structure of an if-else statement
if(condition)
{
statements;
}
else
{
statements;
}