Docsity

Arrays and Functions in C: Working with Multidimensional Arrays and Defining Functions, Slides of Computer Programming

Partial preview of the text

Download Arrays and Functions in C: Working with Multidimensional Arrays and Defining Functions and more Slides Computer Programming in PDF only on Docsity!

Working with Arrays

^ Example: adds up the elements of anarray: int^

array[5]={2,

3,^

4,^ 5,2}

int^

sum^

=^ 0;

for(

i=0;

i<5;

i++) sum^

=^ sum

+^ array[i];

Arrays of Different Types int^ ID[30];

/* 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*/

Example

^ The example uses a two dimensionalarray with two rows, each containingfive columns.

Multi-dimensional Arrays in CExample int array[3][6]; /* Declare a 2D array */int i,j;array[0][0]= 7;

/*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); }

Storage in Memory

^ 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.

What is a function?

^ 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.

About Functions

^ To learn about function you need toknow three things:^ ^ Declaration^ ^ Definition^ ^ Usage/Calling

Defining Functions

^ 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;}

Using Functions

^ 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^ =

4,^

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));

}

void functions

^ 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.

What are these prototype things?  A prototype tells your C program whatto expect from a function - whatarguments it takes (if any) and what itreturns (if any)  Prototypes should go before main()  #include finds the prototypes for libraryfunctions (e.g. printf)  A function MUST return the variabletype we say that it does in theprototype.