Академический Документы
Профессиональный Документы
Культура Документы
locations.
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]
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
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
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:
Pointers
Pointers
Pointers provide the means by which functions can
Declaration of Pointer
Syntax:
Pointer Operators
There are two special pointer operators: * and &.
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.
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;
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
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
Functions
Function
Function is basic requirement of modular
programming.
The process of dividing a large program into small sub
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);
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.
After each function has done its thing, control returns to main().
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
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"; }
Rules
A function gets called when the function name is followed
by a semicolon.
main() {
func(); }
Rules
A function can be called any number of times.
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 }
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)); }
Ex:
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
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);
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