C Programming Cheat Sheet
CSE240 Homework 4 Concepts & Patterns
🧠 Programming Methodology
The Three-Step Process
1. INSTRUCTION
Read what the homework says to do
2. TRANSLATION
Convert requirement to C syntax
3. WHY
Understand the reasoning behind the choice
Key Insight: Instruction order ≠ Implementation order. Always think about logical flow, not comment order.
⚡ Essential Function Design Pattern
int myFunction() {
if (somethingWrong) {
return errorCode;
}
convertInputsToProperFormats();
doTheActualWork();
sortOrOrganizeResults();
return successCode;
}
Remember: Always validate before doing work. Check conditions BEFORE attempting operations.
📊 Structs & Arrays of Structs
Basic Struct Syntax
struct studentRecord {
char studentName[MAX_NAME_LENGTH];
char major[MAX_NAME_LENGTH];
schoolYear schoolYear;
unsigned int studentID;
};
struct studentRecord list[MAX_STUDENTS];
Accessing Struct Elements
list[i].studentName
list[i].major
list[i].schoolYear
list[i].studentID
strcpy(list[count].studentName, studentName_input);
list[count].studentID = studentID_input;
🔢 Enums (Enumeration Types)
typedef enum {
freshman = 0,
sophomore,
junior,
senior
} schoolYear;
String to Enum Conversion
schoolYear yearEnum;
if (strcmp(schoolYear_input, "freshman") == 0) {
yearEnum = freshman;
}
else if (strcmp(schoolYear_input, "sophomore") == 0) {
yearEnum = sophomore;
}
Enum to String Conversion
switch (list[i].schoolYear) {
case freshman:
printf("freshman\n");
break;
case sophomore:
printf("sophomore\n");
break;
}
🔤 String Operations
✅ CORRECT
if (strcmp(str1, str2) == 0) {
}
strcpy(destination, source);
❌ WRONG
if (str1 == str2) {
}
destination = source;
strcmp() Return Values
- 0 → strings are identical
- > 0 → first string comes after second alphabetically
- < 0 → first string comes before second alphabetically
🔄 Loop Patterns
Basic Array Traversal
for (int i = 0; i < count; i++) {
}
Bubble Sort Pattern
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (strcmp(list[j].studentName, list[j + 1].studentName) > 0) {
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
Why count - 1? If you have 5 elements, you only need 4 comparisons (pairs: 0-1, 1-2, 2-3, 3-4)
✅ Input Validation Patterns
Essential Validation Checks
if (count >= MAX_STUDENTS) {
return 2;
}
for (int i = 0; i < count; i++) {
if (strcmp(list[i].name, newName) == 0) {
return 0;
}
}
if (count == 0) {
printf("List is empty.\n");
return;
}
📁 File I/O Patterns
Reading Files
FILE* file;
file = fopen(fileName, "rb");
if (file == NULL) {
printf("%s not found\n", fileName);
return;
}
fread(&count, sizeof(count), 1, file);
fclose(file);
Writing Files
FILE* file;
file = fopen(fileName, "wb");
fwrite(&count, sizeof(count), 1, file);
fclose(file);
Critical: Read and write data in the EXACT same order, or your program will crash!
🎯 Common Return Value Patterns
return 0;
return 1;
return 2;
How Calling Functions Use Return Values
int result = addSort(name, major, year, id);
if (result == 0)
printf("Student is already on the list!\n");
else if (result == 1)
printf("Student successfully added!\n");
else
printf("Unable to add. List is full!\n");
💡 Key Programming Insights
- count tracks how many items exist, also points to next empty spot
- Arrays are zero-indexed: first element is [0], last is [count-1]
- Always validate inputs before performing operations
- String operations require special functions (strcmp, strcpy)
- Enum conversion requires explicit if/else or switch statements
- File I/O order matters: read in same sequence as write
- Struct swapping: use temp variable to swap entire structs
⚠️ Common Mistakes to Avoid
❌ Don't Do This
- Use == to compare strings
- Assign strings with = operator
- Forget to check if list is full
- Try to add before validating
- Use count as loop limit when it can change
- Forget to increment count after adding
✅ Do This Instead
- Use strcmp() for string comparison
- Use strcpy() for string copying
- Check count >= MAX_SIZE first
- Validate all inputs before processing
- Use count-1 for loop limits
- Always count++ after successful add
Keep this cheat sheet handy for future C programming assignments!
Remember: Validate → Prepare → Process → Organize → Return