On 23rd May 2024 Lesson Preparation
On 23rd May 2024 Lesson Preparation
ARRAYS
Example1: Write a simple C program to Take 10 integer input from user and store them in an
------------------------------------------------------------------------
#include <stdio.h>
int main() {
int i;
printf("Enter 10 integers:\n");
scanf("%d", &numbers[i]);
sum += numbers[i];
return 0;
ASSIGNMENT
Q1. Write a C program to read ages of all students in class and save them in an array which can store
floating point and find average, minimum and maximum age.
#include <stdio.h>
int main() {
int num_students;
float ages[100]; // Assuming a maximum of 100 students, adjust as needed
scanf("%d", &num_students);
scanf("%f", &ages[i]);
sum += ages[i];
min_age = ages[i];
max_age = ages[i];
Types of Errors
Syntax Errors:
These occur when the code violates the grammar rules of the C programming language.
Semantic Errors:
These occur when the statements are syntactically correct, but they make no sense.
Runtime Errors:
Examples: Division by zero, accessing out-of-bounds array elements, or null pointer dereferences.
Logical Errors:
These occur when the program runs without crashing, but produces incorrect results.
Pay close attention to the compiler error messages as they often point to the exact location and
nature of the error.
Incremental Development:
Write and test small pieces of code incrementally to catch errors early.
Having another set of eyes review the code can help catch errors that the original programmer might
miss.
Use of Debuggers:
Tools like gdb can help trace the execution of a program to find where it goes wrong.
Tools like lint or cppcheck can analyze your code for common errors without executing it.
Types of Compilers
Native Compilers:
These compilers generate machine code for the same platform on which the compiler is running.
Example: GCC (GNU Compiler Collection) for Linux, Clang for macOS.
Cross Compilers:
These compilers generate machine code for a different platform than the one on which the compiler
is running.
Example: A compiler running on a Windows machine generating code for an embedded system.
Example: The Java Virtual Machine (JVM) compiles Java bytecode to machine code at runtime.
Compilation Techniques
Single-Pass Compilation:
The source code is read and compiled in one pass. This is faster but less powerful.
Multi-Pass Compilation:
The source code is read multiple times (passes) to perform different stages of compilation like syntax
analysis, semantic analysis, optimization, and code generation.
Incremental Compilation:
Unit Testing:
Integration Testing:
Test combined parts of the application to ensure they work together.
Gradually integrate modules and test the data flow between them.
System Testing:
Boundary Testing:
Test the edges of input ranges to catch off-by-one errors and other boundary issues.
Regression Testing:
Debugging Techniques
Print Statements:
Inserting print statements in the code to trace the values of variables and the flow of execution.
Using a Debugger:
Tools like gdb allow you to step through the code, set breakpoints, and inspect the state of the
program.
Backtracking:
Working backwards from the point where the error occurred to find the source of the problem.
By progressively narrowing down the section of code where the bug might be, you can isolate the
problematic code.
Explaining your code and thought process to an inanimate object or another person can often help
you see errors you missed.
By following these guidelines and using these tools, you can effectively identify, correct, compile,
and test your C programs.