


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















^ Example: adds up the elements of anarray: int^
array[5]={2,
int^
sum^
for(
i=0;
i<5;
i++) sum^
=^ sum
+^ array[i];
/* Could be used to store the ID numbers of students in a class */ float
temperatures[31];^ /* Could be used to store the daily temperatures in a month */ char
name[20];^ /* Could be used to store a character string*/
^ The example uses a two dimensionalarray with two rows, each containingfive columns.
/*Set an element of it */
for (i= 0; i < 3; i++) {for (j= 0; j < 6; j++) {
array[i][j]= i; /* assign array*/ } } printf ("element 1,1 is %d\n",array[1][1]);
#include^ <stdio.h>void^ main(){ /*^ Declare
a^2 x^5 multidimensional
array^ */
int^ marks[4][5]
=^ {^ {1,^ 2,
3,^ 4,^5 }, {2, 4, 6,^ 8,^ 10},{3,^ 5, 6,^ 7,^9 }, {4, 4, 5,^ 6,^ 10}
};
int^ row,^ column,
sum,^ count=0; for^ (row=0;
row<4;^ row++) { for^
(column=1;
column<5;
column++) {
printf("%d\t",
marks[row][column]); sum^ +=^ marks[row][column];count++; } printf(“\n”);} printf(“avg^ marks=
%f\n”,^ sum/count); }
^ Since computer memory is essentiallyone-dimensional, with memorylocations running straight from 0 upthrough the highest location inmemory. ^ A multidimensional array cannot bestored in memory as a grid. Instead, thearray is dissected and stored in rows. ^ Consider the two-dimensional array.
^ The function is one of the most basicthings to understand in Cprogramming. ^ A function is a sub-unit of a programwhich performs a specific task. ^ We have already (without knowing it)seen
functions from the C library – printf, scanf, sqrt. We need to learn to write our ownfunctions.
^ To learn about function you need toknow three things:^ ^ Declaration^ ^ Definition^ ^ Usage/Calling
^ The function is defined in the body of the c-file, outside of any other functions. ^ The definition explains what the functiondoes with the information passed in and howit determines the information to return.^ int
factorial(int
num)
{^ /*
start
of^ function
definition
*/
..} int max(int a, int b){ if(a>b) return a;else return b;}
^ To use a function, just pass variables orarguments in the correct order. ^ You can use the return value to setanother variable.
void
main(void) { int
a^ =
b; b^ =^
factorial(a); b^ =^
factorial(6); }
Functions can access other functions Once you have written a function, it canbe accessed from other functions.
We
can therefore build more complexfunctions from simpler functions int^ max_of_three
(int,^ int,
int);^ /*
Prototype*/
..^ /*^ Main
and^ rest
of^ code
is^ in^ here
*/
. int^ max_of_three
(int^ i1,
int^ i2,
int^ i3)
/*^ returns
the^ maximum
of^ three
integers
*/
{ return
(max^ (max(i1,
i2),^ i3));
}
^ A function doesn't have to take orreturn arguments.
We prototype such a
function using void.^ void print_hello (void);void print_hello (void)/*^ this function prints
hello^ */ { printf ("Hello\n");}
Prototype (at top of file remember)
void odd_or_even (int num)/*^ this function prints
odd or even
appropriately */
{ if ((num %
2)^ ==^ 0)
{ void odd_or_even (int);printf^ ("Even\n");return;} printf ("Odd\n");}
Function takes and returnsvoid (no arguments)Another prototypeFunction which takes oneint arguments and returns none
Where do functions go in the program ^ Generally speaking it doesn't matter toomuch. ^ main() is a function just like any other (youcould even call it from other functions if youwanted. ^ It is common to make main() the firstfunction in your code. ^ Functions must be entirely separate fromeach other. ^ Prototypes must come before functions areused. ^ A usual order is: Prototypes THEN main THENother functions.