100% found this document useful (1 vote)
47 views

2.array and Functions

This document provides information on C++ programming concepts including arrays and functions. It discusses arrays, how to create and initialize them, and access array elements. It provides examples of using arrays to find total marks, average temperature, and sum of even numbers. It also covers functions, their general format and types (returning values vs not returning values). It discusses calling functions and provides examples of functions to display stars, numbers from 1 to 100, and even/odd numbers in a range.

Uploaded by

sktaufik753
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
47 views

2.array and Functions

This document provides information on C++ programming concepts including arrays and functions. It discusses arrays, how to create and initialize them, and access array elements. It provides examples of using arrays to find total marks, average temperature, and sum of even numbers. It also covers functions, their general format and types (returning values vs not returning values). It discusses calling functions and provides examples of functions to display stars, numbers from 1 to 100, and even/odd numbers in a range.

Uploaded by

sktaufik753
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

C++ Programming | CCIT

1
C++ Programming | CCIT

Table of Contents
Array 3
Introduction to array 3
Create an array 3
Array Initialization 4
Functions 8
Introduction to Array 8
General Format of Function 8
Return type9
Calling functions 9
Functions Arguments 12
Functions returning value 21
Function declaration/prototype 29
Scope of Variables 30
Scope Resolution Operator 31
Reference Variables 31
Reference Arguments 33
Function Overloading 34
Default arguments 40

2
C++ Programming | CCIT

Array
 An array is a derived data type that can store multiple values of same type.
 Array is a group of elements of same type sharing same name and stored in
consecutive memory locations.

Creating an Array
 An Array can be created by specifying data type, array name and its size.
Syntax:

datatype arrayname[ size ] ;

5050 5054 5058 5012 5016 5020 5024 5028 5032 5036

For eg: int a[10] ;

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]

Accessing Array Elements


 Elements of an array can be accessed by using subscript operator
Syntax:

arrayname [ index ]

Note: index number begins with zero.

WAP to find total marks of 5 subjects.


3
C++ Programming | CCIT

#include<iostream> Enter marks 5 for


subject 85 63 74 59 64
using namespace std;
Total marks 345
main()
{
int a[5],s=0;
cout<<"Enter marks 5 for subject"<<endl;
for(int i=0;i<=4;i++)
cin>>a[i];
for(int i=0;i<=4;i++)
s=s+a[i];
cout<<"Total marks "<<s<<endl;
}

Array Initialization
While defining an array we can directly store values into it.
Syntax:
datatype arrayname[size]={values…};

Note: If array is initialize then size of array is optional


For eg:
 int a[5]={32,12,55,2,34};
 int a[]={32,12,55,2,34};
 int a[5]={32,12,55};
 int
a[5]={32,12,55,2,34,54,22,6}; //ERROR

4
C++ Programming | CCIT

WAP to read 10 no in an array and display it.


#include<iostream> Enter 10 int no.
using namespace std; 8 5 6 3 7 4 5 9 6 4
main() Array: - 8 5 6 3 7 4 5 9 6 4
{
int a[10],s=0;
cout<<"Enter 10 int no."<<endl;
for(int i=0;i<=9;i++)
cin>>a[i];
cout<<"Array:-";
for(int i=0;i<=9;i++)
cout<<a[i]<<" ";
}

WAP to read 10 nos in an int array and find sum of all elements.
#include<iostream> Enter 10 int no.
using namespace std; 8 5 6 3 7 4 5 9 6 4
main() Sum is 57
{
int a[10],s=0;
cout<<"Enter 10 int no."<<endl;
for(int i=0;i<=9;i++)
{
cin>>a[i];
s=s+a[i];
}
cout<<"Sum is "<<s;
} 5
C++ Programming | CCIT

WAP to read temperature values for 7 days of a week and find average
temperature of that week.
#include<iostream>
using namespace std;
main()
{
int a[6],s;
float Avg;
cout<<"Enter temperature 7 days"<<endl;
for(int i=0;i<=6;i++)
{
cin>>a[i];
s=s+a[i];
Avg=s/7.0;
}
cout<<"Average temperature is "<<Avg;
}

Enter temperature 7 days


21 23 24 25 26 21 20
Average temperature is 22.8571

6
C++ Programming | CCIT

WAP to read 10 nos in an int array and find sum of all even nos.
#include<iostream>
using namespace std;
main()
{
int a[9],s;
cout<<"Enter 10 no."<<endl;
for(int i=0;i<=9;i++)
{
cin>>a[i];
if(a[i]%2==0)
s=s+a[i];
}
cout<<"Sum of even no. is "<<endl;
}

Enter 10 int no.


8 5 6 3 7 4 5 9 6 4
Sum of even no. is 28

7
C++ Programming | CCIT

Functions
Functions
A function is a block of code design to perform some specific task. Functions are
used to divide program into logical modules. They are used to avoid repetition of
code in different part of program. Once a function is designed it can be used
anywhere in program whenever required.

According to return type function are of two type


 Functions returning value
o Such functions when called always return us some value.
o eg:-
 Z = 100 + fact( 5 ) ;
 P = sqrt( 10 ) ;
 cout << fact( 5 ) ;
o - As such functions returns an value So such function must be used in an
expression
 Functions not returning values
o Such functions are called just to perform some task.
o eg:-
 clrscr( );
 circle( 200 , 100 , 50 ) ;
o Such function doesn’t return any values so they can’t be used in an
expression.

8
C++ Programming | CCIT

General Format of Function


o
returntype
o
functionName(datatype args1,datatype args2,...)
{
Statements
---------------
return value ;
}

return type
 It indicates the datatype of value the function is going to return.
 If function is returning a value then it can be any datatype such as int, long,
float, double, char, pointer, structure etc.
 If function is not returning any value then return type must be void.
 if no return type is specified then by default return type is int.

Calling functions
Once a function is designed it can be call whenever required.
Syntax:

function-name( arg1 , arg2 , . .)


 It transfers program control to specified function.
 It executes all statements of function.
 Then it return program control to the point from where the function was call.

9
C++ Programming | CCIT

Design a function to display 25 stars


. #include<iostream>
using namespace std;
void star()
{
int i;
for(i=1;i<=25;i++)
cout<<"*";
}
main()
{
cout<<"CCIT"<<endl;
star();
cout<<endl<<"Amravati"<<endl;
star();
}

CCIT
*************************
Amravati
*************************

10
C++ Programming | CCIT

Design a function display which will print all nos from 1 to 100 .
#include<iostream> Number is 1 2 3 4 5 6 4
using namespace std; 5 6 7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22
void display() 23 24 25 26 27 28 29 30
{ 31 32 33 34 35 36 37 38
39 40 41 42 43 44 45 46
int i;
47 48 49 50 51 52 53 54
for(i=1;i<=100;i++) 55 56 57 58 59 60 61 62
cout<<i<<" "; 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78
}
79 80 81 82 83 84 85 86
main() 87 88 89 90 91 92 93 94
{
95 96 97 98 99 100

cout<<"Number is ";
display ();
}
Design a function even which will print all even nos in range 1 to 100
#include<iostream> Even nos. are 2 4 6 8 10
using namespace std; 12 14 16 18 20 22 24 26
28 30 32 34 36 38 40 42
void even() 44 46 48 50 52 54 56 58
{ 60 62 64 66 68 70 72 74
76 78 80 82 84 86 88 90
for(int i=2;i<=100;i=i+2) 92 94 96 98 100
cout<<i<<" ";
}
main()
{
cout<<"Even nos are ";
even();
}

11
C++ Programming | CCIT

Design a function odd which will print all odd nos in range 1 to 100
#include<iostream> Odd nos. are 1 3 5 7 9 11
13 15 17 19 21 23 25 27
using namespace std;
29 31 33 35 37 39 41 43
void odd() 45 47 49 51 53 55 57 59
{ 61 63 65 67 69 71 73 75
77 79 81 83 85 87 89 91
int i; 93 95 97 99
for(i=1;i<=100;i=i+2)
cout<<i<<" ";
}
main()
{
cout<<"Odd nos. are ";
odd();
}

Functions Arguments
formal parameters
 While calling a function
we can pass some data returntype functionName(datatype args1,...)
(actual parameters). {
actual parameters Statements
----------
 For ex: even( 25 ) ;
return value ;
 This data is passed on }
to function as
arguments (formal parameters).
 According to argument value received the function can perform different task.
Note:
 No of actual parameters and formal parameters must be same.
 Datatype of actual parameters and formal parameters must be same.
 Sequence of actual parameters and formal parameters must be same.
12
C++ Programming | CCIT

Example
#include<iostream>
using namespace std;
void star(int n)
{
int i;
for(i=0;i<=n;i++)
cout<<"*";
cout<<endl;
}
main()
{
cout<<"CCIT"<<endl;
star(15);
cout<<endl<<"Amravati"<<endl;
star(25);
}

CCIT
***************
Amravati
*************************

13
C++ Programming | CCIT

Design a function display which will print all nos from 1 to specified no which is
passed as argument.
#include<iostream> 1 2 3 4 5 6 4 5 6 7 8 9 10 11 12 13 14
using namespace std; 1 2 3 4 5 6 4 5 6 7
void display(int n)
{
for(int i=1;i<=n;i++)
cout<<i<<" ";
}
main()
{
display(14);
cout<<endl;
display(7);
}

Design a function odd which will print all odd nos in range 1 to specified no which
is passed as argument.
#include<iostream> 1 3 5 7 9 11 13 15 17 19 21 23 25
using namespace std;
1 3 5 7
void odd(int n)
{
for(int i=1;i<=n;i=i+2)
cout<<i<<" ";
}
main()
{
odd(25);
cout<<endl;
odd(7);
}

14
C++ Programming | CCIT

Design a function even which will print all even nos in range 1 to specified no
which is passed as argument.
#include<iostream> 2 4 6 8 10 12 14 16 18 20 22 24
using namespace std; 2 4 6
void even(int n)
{
for(int i=2;i<=n;i=i+2)
cout<<i<<" ";
}
main()
{
even(25);
cout<<endl;
even(7);
}
Design a function display which will print all nos from 1st argument to 2nd
argument. (1st arg < 2nd arg)
#include<iostream> 10 11 12 13 14 15 16 17 18 19 20
using namespace std;
void display(int a,int b)
{
int i;
for(i=a;i<=b;i++)
cout<<i<<" ";
}
main()
{
display(10,20);
} 15
C++ Programming | CCIT

Design a function interest which will calculate and print simple interest form 3
arguments p,r,t (si=(p*r*t)/100)
#include<iostream> Simple interest is 183.75
using namespace std;
void interest(int p,float r,int t)
{
float si=(p*r*t)/100;
cout<<"Simple interest is
"<<si<<endl;
}
main()
{
interest (5000,12.25,3);
}

Design a function volume which will calculate and print volume of box form 3
arguments l,b,h V=L*B*H
#include<iostream> Volume of Box is 40
using namespace std;
void volume(int l,int b,int h)
{
int v=l*b*h;
cout<<"Volume of Box is "<<v<<endl;
}
main()
{
volume(2,4,5);
}
16
C++ Programming | CCIT

Design a function volume which will calculate and print volume of sphere when
radius is pass as argument.
v=4/3*3.14*r3

#include<iostream>
using namespace std;

void volume(float r)
{
float v=4/3.0*3.14*r*r*r;
cout<<"Volume of sphere is "<<v<<endl;
}

main()
{
volume(2.5);
}

Volume is 65.4167

17
C++ Programming | CCIT

Design 2 functions area and circumference which will calculate and print area and
circumference of circle when radius is pass as argument .
A=3.14*r*r
C=2*3.14*r

#include<iostream> Enter radius 5


using namespace std; Area is 78.5
void circumference(float r) Circumference is 31.4
{
float c=2*3.14*r;
cout<<"Circumference is "<<c<<endl;
}

void area(float r)
{
float a=3.14*r*r;
cout<<"Area is "<<a<<endl;
}
main()
{
float n;
cout<<"Enter Radius ";
cin>>n;
area(n);
circumference(n);
}

18
C++ Programming | CCIT

Design 2 functions even and odd which will print all even / odd nos from 1 to
specified no.

#include<iostream> 1 3 5 7 9 11 13 15 17 19 21 23
using namespace std; 1 3 5 7

void odd(int n)
{
for(int i=1;i<=n;i=i+2)
cout<<i<<" ";
}

void even(int n)
{
for(int i=2;i<=n;i=i+2)
cout<<i<<" ";
}

main()
{
even(25);
cout<<endl;
odd(100);
cout<<endl;
odd(10);
}

19
C++ Programming | CCIT

Design a function sum which will calculate and print sum of all nos form 1 to a no.
which is pass as argument.
#include<iostream> Sum is 15
using namespace std;
void sum(int n)
{
int s=0;
for(int i=1;i<=n;i++)
s=s+i;
cout<<"Sum is "<<s<<endl;
}
main()
{
sum(5);
}

Design a faction area which will print area of rectangle when length and breadth is
passed as argument.
#include<iostream> Area is 35
using namespace std;
void area(int l,int b)
{
int a=l*b;
cout<<"Area is "<<a<<endl;
}
main()
{
area(5,7);
}
20
C++ Programming | CCIT

Design a function fact which will calculate and print factorial of all nos form 1 to a
no. which is pass as argument.
#include<iostream> Factorial is 120

using namespace std;


void fact(int n)
{
int s=1;
for(int i=1;i<=n;i++)
s=s*i;
cout<<"Factorial is "<<s<<endl;
}
main()
{
fact(5);
}

Functions returning value


 If we want to use our returntype functionName(datatype args1,...)
function in an {
expression. Statements
For ex: ----------
return value;
z = 100 + fact( 5 ) ;
}
 Then such function
must return a value by using return statement.
Syntax:
return value;
 The return statement returns value at the point from where function is called.
 The return value and returntype of function must be compatible.

21
C++ Programming | CCIT

Design a faction area which will print area of rectangle when length and breadth is
passed as argument.
#include<iostream>
using namespace std;

int area(int l,int b)


{
int a=l*b;
return a;
}

main()
{
int z=area(5,7)+area(10,20);
cout<<"Total area is "<<z<<endl;
}

Total area is 235

22
C++ Programming | CCIT

Design a function fact which will calculate and return factorial of a no. which is
pass as argument.
#include<iostream>
using namespace std;

int fact(int n){


int f=1;
for(int i=1;i<=n;i++)
f=f*i;
return f;
}

main()
{
int z=fact(5)-fact(4);
cout<<"Result is "<<z<<endl;
}

Result is 96

23
C++ Programming | CCIT

Design a function sum which will calculate and return sum of all nos form 1 to a
no. which is pass as argument.
#include<iostream>
using namespace std;

int sum(int n)
{
int s=0;
for(int i=1;i<=n;i++)
s=s+i;
return s;
}

main()
{
int z=sum(5);
cout<<"Result is "<<z<<endl;
}

Result is 15

24
C++ Programming | CCIT

Design a function interest which will calculate and return simple interest from 3
arguments p,r,n.
si=p*r*n/100
#include<iostream>
using namespace std;

float interest(int p,float r,int n)


{
float si=p*r*n/100;
return si;
}

main()
{
float z=interest(5000,10.25,3);
cout<<"Result is "<<z<<endl;
}

Result is 1537.5

25
C++ Programming | CCIT

Design a function volume which will calculate and return volume of box from 3
arguments l,b,h
V=l*b*h
#include<iostream>
using namespace std;

int volume(int l,int b,int h)


{
int v=l*b*h;
return v;
}

main()
{
int z=volume(3,4,5);
cout<<"Result is "<<z<<endl;
}

Result is 60

26
C++ Programming | CCIT

Design a function volume which will calculate and return volume of sphere when
radius is pass as argument. ( V=4/3*3.14*r3 )
#include<iostream>
using namespace std;

float volume(int r)
{
float v=4/3.0*3.14*r*r*r;
return v;
}

main()
{
int n;
cout<<"Enter Radius ";
cin>>n;
float z=volume(n);
cout<<"Volume of sphere is "<<z<<endl;
}

Enter Radius 2
Volume of sphere is 33.4933

27
C++ Programming | CCIT

Design 2 functions area and circumference which will calculate and return area
and circumference of circle when radius is pass as argument.
#include<iostream> Enter Radius 5
using namespace std; Area is 78.5
float circumference(int r) Circumference is 31.4
{
return 2*3.14*r;
}
float area(int r)
{
return 3.14*r*r;
}
main()
{
int n;
cout<<"Enter Radius ";
cin>>n;
float p=area(n);
float q=circumference(n);
cout<<"Area is "<<p<<endl;
cout<<"Circumference is "<<q<<endl;
}

28
C++ Programming | CCIT

Function declaration/prototype
 Function declaration is used to provide information about function in advance
to compiler.
 If a function is called before its definition then such function has to be declared
before function call.
 Syntax:
returntype functionName(data type of args…);
 In function declaration argument name are optional.
Example:-
#include<iostream>
using namespace std;
float interest(int,float,int);
main()
{
float z;
z=interest(5000,10.25,3);
cout<<"Simple intrest is "<<z<<endl;
}
float interest(int p,float r,int n)
{
float si;
si=p*r*n/100;
return si;
}

Simple intrest is 1537.5

29
C++ Programming | CCIT

Scope of Variables
 In C++ we can define variables at any point within our program.
 Scope of variable depends on block or the function in which the variable is
defined.

Example:
int a ;
void demo( int x )
{
int y ;
………….
}
void main()
{
int b ;
………
for( int i = 1 ; i <= 10 ; i++ )
{
int j ;
…………..
}
int k ;
………………
}

30
C++ Programming | CCIT

Scope Resolution Operator (:: )


 This operator is used in C++ to perform different types of operations.
 One such operation is to access a variable which is gone outside the scope due
to presence of local variable having same name.
 Syntax:
:: VaraibleName

int a=5; 10
void main() 5
{
int a=10;
cout<<a<<endl;
cout<<::a<<endl;
}

Reference Variables
 A reference is an alias name for a variable i.e. when we create a reference, we
assign a new name to a variable.
 Syntax:

datatype &referenceName = variable ;


 It will create a new reference for the specified variable.
 Note: Anything you do to the reference is really done to the target variable.

31
C++ Programming | CCIT

Example:
#include<iostream> 5 5 5
using namespace std ; 8 8 8

main()
{
int a=5;
int &b=a;
int &c=b;
cout<<a<<b<<c<<endl;
a++;
b++;
c++;
cout<<a<<b<<c<<endl;
}

Example:
#include<iostream> 5
using namespace std; 5

void change(int a)
{
a++;
}
main()
{
int a=5;
cout<<a;
change(a);
cout<<a;
32
}
C++ Programming | CCIT

Reference Arguments
 If a function argument is defined of type reference argument.
 Syntax:
datatype &argname
 Then the reference argument refers to the variable which is pass as argument.
 Due to this the operations performed by the function by using that reference
are actually performed on original variable.

Design a function swap which will exchange values of 2 variables which are passed
as arguments.
#include<iostream> A=5 B=7
A=7 B=5
 using namespace std;

void swap( int &x ,int &y)

{

int temp=x;
x=y;
y=temp;
}
main()
{
int a=5,b=7;
cout<<"A="<<a<<"B="<<b<<endl;
swap(a,b);
cout<< "A="<< a << " B=" << b << endl;
}

33
C++ Programming | CCIT

Function Overloading
 Defining multiple functions in a program having same name is called as function
overloading.
 Only precaution to be taken is that
1. No of arguments or
2. Type of arguments or
3. Sequence of arguments must be different.
 Compiler will decide which function to call depending on no of arguments or
type of arguments or sequence of arguments that are passed while calling the
function.
Example:
#include<iostream> RollNo is 4117

 using namespace std; Name is Amit jain

void display(int rn ,string nm) RollNo is 3012
{ Name is Gopal Pandey
cout<<"RollNo is "<<rn<<endl; Branch is CS
cout<<"Name is "<<nm<<endl;
}
void display(int rn,string nm,string br)
{
cout<<"RollNo is "<<rn<<endl;
cout<<"Name is "<<nm<<endl;
cout<<"Branch is "<<br<<endl;
}
main( )
{
display(4117,"Amit jain");
display(3012,"Gopal Pandey","CS");
34
}
C++ Programming | CCIT

Example:
#include<iostream>

 using namespace std;

void display(int rn, string nm)
{
cout<<"RollNo is "<<rn<<endl;
cout<<"Name is "<<nm<<endl;
}
void display(string nm, int rn)
{
cout<<"RollNo is "<<rn<<endl;
cout<<"Name is "<<nm<<endl;
}
main()
{
display(4117,"Amit jain");
display("Gopal Pandey",3012);
}

RollNo is 4117
Name is Amit jain
RollNo is 3012
Name is Gopal Pandey

35
C++ Programming | CCIT

Example:
#include<iostream>

 using namespace std;

void display(int rn)
{
cout<<"RollNo is "<<rn<<endl;
}
void display(string nm)
{
cout<<"Name is "<<nm<<endl;
}
main()
{
display(4117);
display("Gopal Pandey");
}

RollNo is 4117
Name is Gopal Pandey

36
C++ Programming | CCIT

Design 2 function volume which will calculate volume of box and cube.

#include<iostream>
using namespace std;
int volume(int L, int B, int H)
{
int v=L*B*H;
return v;
}
int volume(int n)
{
int v=n*n*n;
return v;
}
int main()
{
int a=volume(5,7,8);
cout<<"Volume of box is "<<a<<endl;
int b=volume(5);
cout<<"Volume of cube is "<<b<<endl;
}

Volume of box is 280


Volume of cube is 125

37
C++ Programming | CCIT

Design 2 functions interest which can be called by passing 3 arguments (p,r,n) or 2


(p,r)arguments.
#include<iostream>
using namespace std;
float interest(int p,float r,int n)
{
float si=p*r*n/100;
return si;
}
float interest(int p,float r)
{
float si=p*r*1/100;
return si;
}
main()
{
float z1=interest(5000,10.25,3);
cout<<"Interest for 3 years is "<<z1<<endl;
float z2=interest(5000,10.25);
cout<<"Interest for 1 years is "<<z2<<endl;
}

Interest for 3 years is 1537.5


Interest for 1 years is 512.5

38
C++ Programming | CCIT

Design a functions sum which can be called by passing 4 or 3 or 2 arguments.


#include<iostream> Sum is 1400 9000 50
using namespace std;
int sum(int a, int b, int c, int d)
{
int s=a+b+c+d;
return s;
}
int sum(int a, int b, int c)
{
int s=a+b+c;
return s;
}
int sum(int a, int b)
{
int s=a+b;
return s;
}
main()
{
int s1=sum(100,200,500,600);
int s2=sum(1000,5000,3000);
int s3=sum(27,23);
cout <<"Sum is "<<s1<<" "<<s2<<" "<<s3;
}

39
C++ Programming | CCIT

Default arguments
 While defining a function, we can assign default values for each of the last
arguments.
 Syntax :

datatype argname = value


 Only arguments from the right side can be assigned default value.
 The default value must be a constant literal.
 We cannot use a variable or argument name or an expression as value to
argument.
 The default value will only be used when less no of arguments are passed to the
function then actually required.
Example:
# include<iostream> Interest is 1500

using namespace std; Interest is 500

float interest(int p, int r, int n=1)
{
float I;
I = p * r * n /100.0;
return I;
}
main()
{
float s;
s= interest (5000, 10.25, 3);
cout << "Interest is " << s<<endl;
s= interest (5000, 10.25);
cout << "Interest is " << s<<endl;
} 40
C++ Programming | CCIT

Design a function sum which can be called by passing 4 or 3 or 2 arguments


#include<iostream> Sum is 950
using namespace std; Sum is 2200
void sum(int a,int b,int c=0,int d=0) Sum is 90

{

int s = a + b + c + d ;
cout<<"Sum is "<<s<<endl;
}
main()
{
sum(100,400,200,250);
sum(1000,1200);
sum(10,50,30);
}

41

You might also like