Вы находитесь на странице: 1из 52

ARRAYS

An array is a collection of variables of similar data type.

A specific element in an array is accessed by an index.


Arrays may have from one to several dimensions. In C/C++, all arrays consist of contiguous memory

locations.

Integer array of 4 elements


1000 1002 1004 1006

The lowest address corresponds to the first element

and the highest address to the last element.

Single dimension Arrays


The general form declaring an array is

<data type> <variable name>[size];


Eg:

int marks[5]; The values are accessed by the indexes. Eg:


marks[0]=90; marks[1]=95; marks[2]=98; marks[3]=85; marks[4]=80;

Example
/* Program to use arrays */

#include<iostream.h> #include<constream.h>
void main() { int arr[5]; //declaring array variable of size 5 clrscr(); for(int i=0;i<5;i++) { Variable for indexing arr[i]=i+1; cout<<arr[i]<<" "; } getch(); Accessing array elements }

Memory mapping
1
arr[0]

2
arr[1]

3
arr[2]

4
arr[3]

5
arr[4]

Two dimensional Array


C/C++ supports multidimensional arrays.
The simplest form of the multidimensional array is the

two-dimensional array. The declaration syntax is <data type> <variable name>[size][size];

Left Index

Right Index

Example
/* Program for two dimensional array */

#include<iostream.h> #include<constream.h> void main() { int t, i, num[3][4]; for(t=0; t<3; ++t) //Assigning values to the array for(i=0; i<4; ++i) num[t][i] = (t*4)+i+1; for(t=0; t<3; ++t) /* now print them out */ { for(i=0; i<4; ++i) cout<<num[t][i]; cout<<"\n"; } }

Memory mapping

num[t][i]
0 0 1 2 1 2 3

1 5 9

2 6 10

3 7 11

4 8 12

Multi dimensional Array


C/C++ allows arrays of more than two dimensions.
The exact limit, if any, is determined by the compiler. The general form of a multidimensional array

declaration is <data type> <variable name>[size1][size2][size3][sizen];

Example
/* Matrix Addition */ #include<iostream.h> #include<constream.h> void main() { int mat1[3][3]; int mat2[3][3]; int i,j; cout<<"\nGive the values for 1st Matrix: \n"; for(i=0;i<3;i++) { for(j=0;j<3;j++) cin>>mat1[i][j]; } cout<<"\nGive the values for 2nd Matrix:\n";

for(i=0;i<3;i++) { for(j=0;j<3;j++) cin>>mat2[i][j]; } int mat[3][3],k,l; cout<<"\nResultant: \n"; for(k=0;k<3;k++) { for(l=0;l<3;l++) { mat[k][l]=(mat1[k][l]+mat2[k][l]); cout<<" "<<mat[k][l]; } cout<<"\n; } }

Array Initialization
C/C++ allows the initialization of arrays at the time of

their declaration.
Syntax

<data type> <array_name>[size1]. . .[sizeN] = {value_list};


Eg:

int i[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; The value_list is a comma-separated list of values whose type is compatible with data type.

Array Initialization
We can initialize the multi dimensional arrays also.
Eg:

int sqr[3][2] = {1,1,2,4,3,9};


Another way

int sqr[3][2] = {{1,1},{2,4},{3,9}}; This is called Sub aggregate Grouping

Pointers

Pointers
Pointers provide the means by which functions can

modify their calling arguments.


Pointers support dynamic allocation. Pointers can improve the efficiency of certain routines.

What are pointers?


A pointer is a variable that holds a memory address.

This address is the location of another object (typically

another variable) in memory.


Memory Address 1000 1001 1002 1003 1004 . . . Memory Variable in memory 1003

Declaration of Pointer
Syntax:

<base type> *<name>;


The base type of the pointer defines what type of

variables the pointer can point to.

Pointer Operators
There are two special pointer operators: * and &.

The & (Referencing Operator) is a unary operator that

returns the memory address of its operand. m=&count;

* (De-referencing Operator) is the complement of &.


It is a unary operator that returns the value located at the address that follows. q=*m; count m q
2000
1002

1002

2000

Example
/* Program for pointers */ #include<iostream.h> #include<constream.h> void main() { clrscr(); int a,*p; cout<<"Enter a:"; cin>>a; p=&a; cout<<\na:"<<a<<\np:"<<p<<\nvalue:"<<*p; getch(); }

Void Pointers
The void type of pointer is a special type of pointer.
In C++, void represents the absence of type. So void pointers are pointers that point to a value that has no type.

int a; int v1; void *ptr;


a=100; ptr=&a; v1=*(int*)ptr; //defining the type

Example-1
int a,v1; char b,v2; void *ptr; a=100; ptr=&a; v1=*(int*)ptr; //now it s pointing to integer cout<<" %d \n ",v1;

b='S'; ptr=&b; v2=*(char*)ptr; //now it s pointing to character cout<<" %c \n ",v2;

Example-2
#include<constream.h> #include<iostream.h> void display(int x,void *y) { int a; char b; if(x==1) { a=*(int*)y; cout<<"Integer Value %d \n ",a; } if(x==2) { b=*(char*)y; cout<<"Character Value %c \n ",b; } }

void main() { int s1; char s2; void *ptr1; clrscr(); s1=300; ptr1=&s1; display(1,ptr1); //function call with int value s2='J'; ptr1=&s2; display(2,ptr1); //function call with char value getch(); }

Pointer Expression
Assignment

int var,*p1,*p2; p1=&var; p2=p1; In this pointers p1 and p2 points to the same variable var. Arithmetic int var,*p; p=&var; p++; // or p--; In this pointer moves to the next memory location

Pointer with Array


Syntax:

int *ptr; int sample[5]; ptr=sample; It can also use & operator to specify the address of 1st element. ptr=&sample;
Note: Pointers can be arrayed like any other data type.

Multiple Indirection
Multiple indirection or pointers to pointers is a pointer

point to another pointer that points to the target value. Eg:


float bal; float *balance,**newbalance; balance=&bal; newbalance=&balance;

Multiple indirection can be carried on to whatever

extent required. More than a pointer to a pointer is rarely needed.

Functions

Function
Function is basic requirement of modular

programming.
The process of dividing a large program into small sub

programs and manipulating them independently is known as modular programming.


A function is a named, independent, block of

statements that perform well defined, specific task and may return a value.

Types of Function
Pre-defined functions User defined functions

Pre-defined functions
The functions, which has special type of meaning which is

already defined and stored, is called built-in functions or pre-defined functions. Eg: int a=4; x=pow(a,2);

User defined functions


Can be created by the user or programmer for performing

specific task.

Any C++ program contains at least one function. If a program contains only one function, it must be main( ). If a C++ program contains more than one function, then one (and

only one) of these functions must be main( ), because program execution always begins with main( ). present in a C++ program.

There is no limit on the number of functions that might be

Each function in a program is called in the sequence specified by

the function calls in main( ).

After each function has done its thing, control returns to main().

When main( ) runs out of function calls, the program ends.

Structure of Function
<return type> <function name>(arg 1, arg 2,,arg n) { declaration part; statements; return; }

Example
int sum(int x,int y) { int result; result = x + y; return (result); }

Properties
Every function has a unique name. This name is used to

call the function from main() function.


A function can be called from within another function.

A function is independent and it can perform its task

without intervention from or interfering with other parts of the program.


A function returns a value to the calling program. This is

optional and depends upon the task.

Example
#include<iostream.h> void main() { cout<<"\nI am in main"; italy(); cout<<"\nI am finally back in main"; } italy() { cout<<"\nI am in italy"; brazil(); cout<<"\nI am back in italy"; }

brazil() { cout<<"\nI am in brazil"; argentina(); }


argentina() { cout<<"\nI am in argentina"; }

Rules
A function gets called when the function name is followed

by a semicolon.
main() {
func(); }

A function is defined when function name is followed by a

pair of braces in which one or more statements may be present.


func() { statement 1 ; statement 2 ; statement 3 ; }

Rules
A function can be called any number of times.

main() { display() ; display() ; } display() { cout<<"\n Learning C is very Easy"; }

Rules
The order in which the functions are defined in a program and the order in which they get called need not necessarily be same.
main() { display1() ; display2() ; } display2() { cout<<"\n }

Called Later ";

display1() { cout<<"\n Called First "; }

Rules
A function can call itself. This is known as Recursion. int fact(int n) { if(n==0) return(1); return(n*fact(n-1)); }

Note: This function is known as Recursive Function.

Parts of the Function


Function prototype declaration
Definition of a function Function call Actual and formal arguments The return statement

Parts of the Function


Function prototype: It helps the compiler to check the return and argument types of the function. Function prototype declaration consists of

Function return type Name Arguments list

Ex:

void show(void); int sum(int,int); float sum(float,int);

Parts of the Function


Function Definition:
The block of statement followed by function

declarator is called as function definition. The declarator and function prototype declaration should match each other.
Function Call:
A function is called by its name followed by

arguments list enclosed in parenthesis and terminated by semi-colon.

Parts of the Function


Actual and formal arguments:
The arguments declared in calling function are

called actual arguments. The arguments declared in function declarator are called formal arguments. The return statement: The return statement is used to return the value to caller function. The return statement returns only one value at a time.
Syntax : return (variable name);

Parts of the Function


void main() { int sum(int,int); int a=10,b=20,c; c=sum(a,b); } int sum(int x,int y) { return(x+y); }
Function Prototype Function call Actual arguments Function declarator Formal arguments

Function definition

Return statement

Types of Function
A function may belong to any one of the following categories:
1. Functions with no arguments and no return values. 2. Functions with arguments and no return values. 3. Functions with arguments and return values. 4. Function with no arguments and return values.

Types of Function
Functions with no arguments and no return values This is the simplest function ,it does not receive any arguments from the called function and does not return any value from calling function.

Types of Function
Functions with arguments and no return values This function receives arguments from calling function and does not return value to the calling function.

Types of Function
Functions with arguments and return values This function receives arguments from calling function and returns the computed value back to the calling function.

Types of Function
Functions with no arguments and return values

Function Call
There are two ways to call a function.
1.

Call by Value

2. Call by Reference

Вам также может понравиться