0% found this document useful (0 votes)
31 views5 pages

On 23rd May 2024 Lesson Preparation

asnknjkankjdfnksj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

On 23rd May 2024 Lesson Preparation

asnknjkankjdfnksj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

GENCP 401: APPLY C PROGRAMMING

ARRAYS

Example1: Write a simple C program to Take 10 integer input from user and store them in an

Array and find the sum of all numbers stored in array.

------------------------------------------------------------------------

#include <stdio.h>

int main() {

int numbers[10]; // Array to store 10 integers

int sum = 0; // Variable to store the sum of numbers

int i;

// Taking input from the user

printf("Enter 10 integers:\n");

for (i = 0; i < 10; i++) {

printf("Enter number %d: ", i + 1);

scanf("%d", &numbers[i]);

// Calculating the sum

for (i = 0; i < 10; i++) {

sum += numbers[i];

// Displaying the sum

printf("The sum of the numbers is: %d\n", sum);

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

float sum = 0.0, average, min_age, max_age;

printf("Enter the number of students: ");

scanf("%d", &num_students);

// Input ages from user

printf("Enter the ages of the students:\n");

for (int i = 0; i < num_students; i++) {

printf("Student %d: ", i + 1);

scanf("%f", &ages[i]);

sum += ages[i];

// Calculate average age

average = sum / num_students;

// Find minimum and maximum age

min_age = max_age = ages[0];

for (int i = 1; i < num_students; i++) {

if (ages[i] < min_age) {

min_age = ages[i];

if (ages[i] > max_age) {

max_age = ages[i];

printf("Average age: %.2f\n", average);

printf("Minimum age: %.2f\n", min_age);

printf("Maximum age: %.2f\n", max_age);


return 0;

Identification of Errors in C Programs

Types of Errors

Syntax Errors:

These occur when the code violates the grammar rules of the C programming language.

Examples: Missing semicolon, incorrect use of braces, or misspelled keywords.

Detected by: The compiler during the compilation phase.

Semantic Errors:

These occur when the statements are syntactically correct, but they make no sense.

Examples: Using an undeclared variable, type mismatches, or incorrect function calls.

Detected by: The compiler during the compilation phase.

Runtime Errors:

These occur when the program is executing.

Examples: Division by zero, accessing out-of-bounds array elements, or null pointer dereferences.

Detected by: The runtime environment or debugger.

Logical Errors:

These occur when the program runs without crashing, but produces incorrect results.

Examples: Incorrect formulae, wrong loop conditions, or flawed algorithm implementation.

Detected by: The programmer through testing and debugging.

Techniques of Correcting Errors

Careful Reading of Compiler Error Messages:

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.

Code Reviews and Pair Programming:

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.

Automated Static Analysis Tools:

Tools like lint or cppcheck can analyze your code for common errors without executing it.

Compilation of the C Program

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.

Just-In-Time (JIT) Compilers:

These compilers compile code at runtime rather than ahead of time.

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.

Often used in simpler or older compilers.

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.

This allows for more sophisticated optimizations and error checking.

Incremental Compilation:

Only the changed parts of the source code are recompiled.

This is useful in large projects to speed up the development process.

Testing of the C Program

Design of Effective Test Cases

Unit Testing:

Test individual components or functions in isolation.

Use frameworks like CUnit or Check for writing unit tests.

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:

Test the complete system to verify it meets the requirements.

Includes functional and non-functional testing like performance and usability.

Boundary Testing:

Test the edges of input ranges to catch off-by-one errors and other boundary issues.

Regression Testing:

Ensure that new changes do not break existing functionality.

Automated tests can be helpful for running regression tests efficiently.

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.

Binary Search for Bugs:

By progressively narrowing down the section of code where the bug might be, you can isolate the
problematic code.

Rubber Duck Debugging:

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.

You might also like