Sie sind auf Seite 1von 15

Engineering Software Laboratory

MECHANICAL ENGINEERING DEPARTMENT


IIT KHARAGPUR
AUTUMN 2016

More about Programming in C


Functions
Header Files
Arrays and Data Structures
File handling, Data, Input and output

Some notes regarding functions


A functions should be defined or declared before use.
All variables inside a function are local.
Parameters passed to a function also become local

variables.
A function may or may not return a value:

return;
or, return <value>;

Nested function calls are allowed.


A function can also call itself.

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:

x1, x2, x3, , xn

All data items in the group share the same name:


int x[n];
Arrays must be declared before use:
type name[size];
To access individual items use its index
x[0] to x[n-1]
Note that in C the bounds for the index are
not checked

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

Read, print, copy or compare arrays by element-wise

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-

wise operations for two-dimensional arrays.

Passing array to a function


Use the array name as an argument to the function
return_type
func_name(, type array_name[],
)
Array name itself is passed as argument during the function
call
Allows entire array to be passed to the function
Dimension of the array can be passed on as a separate
argument
When passing a 2D array number of columns in the array must
be specified.

Problem
Write a program to compute the mean, and median

of a set of n numbers. The number n is to be


specified by the user.

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:

to read; w: to write (creates a new file, deletes any old one);


a: to append to a file.

Example
FILE* fptr;
char[] filename = filename.dat;
fptr = fopen(filename, w);
if(fptr == NULL)
printf(Error opening file!);

File I/O contd.


fprintf and fscanf
Work similar to printf and scanf, but take an additional
argument
The

first argument is the file pointer

To close a file
fclose(fptr);

Problem (Class work)


Write two functions to compute the factorial of a

given number, using two different methods.

The first function factorial_iter should use a counter


based iteration (for or while loop).
The second function factorial_recur should use a
recursive approach to computing the factorial (by calling
itself). Count the number of times the function is called.

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)

First line: # of matrices


For each matrix:
#number of rows #number of columns
elements of the matrix in a tabular form

Assignment
Implement your own set of functions for matrix

operations.

Prepare a separate header file, and associated .c file to


implement basic matrix operations such as:
Addition/subtraction
Multiplication/inverse
Determinant
Adjoint
Transpose

should be implemented.
Your functions should perform proper checks on inputs, and
generate appropriate warnings if the desired operation cannot
be performed.

Das könnte Ihnen auch gefallen