Beruflich Dokumente
Kultur Dokumente
variables.
A function may or may not return a value:
return;
or, return <value>;
Header files
Contain function prototypes for library functions
<stdio.h>, <stdlib.h>, <math.h> etc.
Load with
#include <filename>
Custom header files
Create and save file as filename.h
Load with
#include
filename.h
To reuse functions
Content of the header file will be included in the current file
before compilation.
Storage class
Refers to the scope of a variable
auto
Default storage class
Scoped is confined to the function in which variable is defined
Variable does not retain its value once control is transferred out of the
defining function
static
Same scope as automatic variables
Static variables retain their values throughout the life of the program.
They can be initialized only once.
extern
Their scope extends from point of definition through the remainder of
the program.
May also be defined outside the function, at the beginning.
Arrays
Arrays are used to handle a group of similar data
items:
Arrays contd.
Initialization:
type name[size] = {list of values};
Array names cannot be used directly for operations
For example, if a and b are array variables, you cannot do
= b to assign one variable to another
a == b to compare arrays
scanf or printf directly to print arrays
a
operations
Two-dimensional arrays
Allow us to store a table of items.
type name[row_size][column_size];
To access use two indices
First index indicates row, second indicates column.
Note that in C bounds are not checked for array indices.
As with 1D arrays, you can only perform element-
Problem
Write a program to compute the mean, and median
File I/O
FILE* represents a pointer to a file
fopen is used to open a file
Returns NULL if it can not open the file
Three modes to open file:
r:
Example
FILE* fptr;
char[] filename = filename.dat;
fptr = fopen(filename, w);
if(fptr == NULL)
printf(Error opening file!);
To close a file
fclose(fptr);
Problem (Class)
Implement a program to:
Read a set of matrices of varying sizes from a file
Compute the transpose of the read matrices
Write the resulting matrices in a separate file.
File format (txt file)
Assignment
Implement your own set of functions for matrix
operations.
should be implemented.
Your functions should perform proper checks on inputs, and
generate appropriate warnings if the desired operation cannot
be performed.