470 likes | 612 Views
Functions and Arrays. Local and global functions. Local:- declared inside the main function or inside any other function. They can be called only in that function in which they are declared Global:- declared the out side the main functions can be called by any function.
E N D
Local and global functions • Local:- • declared inside the main function or inside any other function. • They can be called only in that function in which they are declared • Global:- • declared the out side the main functions • can be called by any function
Reference parameters • There are two ways to pass arguments to a functions these are a • Arguments pass by value • new variable of the data type of the argument is created and the data is copied into it • The function access the value in the newly created variable and the data in the original variable in the calling function is not changed
Arguments passed by reference • The data can also be passed to function by reference of a variable name that contains data. the reference provides the second name for a variable name • no new copy of the variable is created only the address of the variable is passed to the function • The original variable is accessed in the function with reference to its second name • Both variables use the same memory location thus any change in the ref variable also changes the value in the original variable • The reference parameters are indicated by an ampersand(&) and sign after the data type. both in the protoype and in the function definition
the function “exch” is declared two int type reference parameters • The ampersand sign is used with each data type. • it indicates that both the parameters of the function exch are reference parameters • When the function exch is called no ampersand sign is used. • The ampersand sign is also used with the data type of “x” and y variables used in the declarator of function definition. • The x and y variables are the second name of a and b variables. • The memory location of x and a is the same and similarly memory location of y and b is same.
Assignment • Find factorial by reference parameters
Built in function • Already been define • Part of the language • Can be used in any program
Accessing built in function • Accessed through header file • Defined in its related header file • If a built in function is to be used in a program its header file must be included in the program
Example • clrscr function is defined in conio.h header file • If this function is to used in a program the conio .h header file must be included in the program • Commonly used mathamitical function are defined in the math.h header file • When a mathematical function is to be used math.h file is included in the program
Conio.h header file • Stands for console input/output • Header file contains basic input and output function used to get data from the keyboard and to send results from the memory to the output screen • Breifdesc
Clscrfunc • Stands for clear screen • Used to clr the screen of the computer • When this is executed the screen is cleared and the cursor shifts to upper left corner • Its syntax is clrscr();
Getchfunc • Stands for get character • Used to get a single character drom the keyboard during program execution • When a key is pressed the entered charater is not displayed on the screen • The control shifts to the next statement as soon as any key is presses • The function is normally used to pass the execution of program.its syntax is • getch();
Built in functions of math.h • Used to perform mathmatical calculations are defined in math.h header file • The commonly used math library function are below
The power function • Used to calculte the exponential power of a given integer number • Syntax is pow(x,y); • Example • If the value of 2^3 is to be calculated the function is written as pow(2,3);
Sqrt function • For +ve number • Syntax is sqrt(x); • X is of double type or variable • Return double type • If x=9.0 then • Sqrt(x) returns 3.0
Cos Function • Cos(x); • X is angle in radian • If x=0.0, it returns 1.0
Sin function • Sin(x); • X is angle or value in radians • If x=0.0, it returns 0 • Same as tan, log etc
What is • #include<iostream.h>? • What is main()
Take 10 ages • Calculate avergaes • How many Variables you need?
Like • Int age1, age2, age3……age10; • What if you have to calculate 100 ages • So this means we want some technique to handle this
Arrays • It reduces the size of a program • Sequence of objects(elemets) of same data types. • Represented in Memory by a consecutive group • These locations are referenced by a single called array name • Each element is referenced by its position • The position is an index from 0 to n-1
Types of Arrays • One dimensional Arrays • Also known as list or a linear array • Consist of only one column or one row • Multi-dimensional Arrays
Name memory C[0] 24 C[1] 59 C[2] 35 C[3] ... C[4] ... C[5] ... C[6] ... C[7] ... C[8] ... C[9] ... Storage of an array in memory Index
Declaring one-dimensional Arrays • In C each array has • name • data type • size • They occupy continuous area of memory
Declaration of Arrays arrayTypearrayName[numberOfElements ]; For example , int age [ 10 ] ; double temp[24]; intabc[5]; • More than one array can be declared on a line int age [10] , height [10] , names [20] ; • Mix declaration of variables with declaration of arrays inti , j , age [10] ;
Referring to Array Elements Array name e.g. age index number age [ 4 ]
.write a program to input values into invidual elements of an arry during program execution .display the values of array. no loop statement should be used in the program . main() { int temp[5]; clrscr(); cout<<"enter value 1st element of temp.? "; cin>>temp[0]; cin>>"enter value in 2nd element of temp .?"; cin>>temp[1]; cin<<"enter value in 3rd element of temp .?"; cin>>temp[2]; cin>>"enter value in 4rth element of temp.?"; cin>> temp[3]; cin<<" enter value in 5th element of temp.?"; cin >>temp [4]; cout<<"values in aray temp are :'<<end1; cout << temp [0] << end1; cout<< temp [1]<< end1; cout << temp [2]<<end1; cout<< temp [3]<<end1; cout<< temp [4]<<end1; }
Program which initialize an Array and then prints the elements on screen. • #include<iostream.h> • void main() • { • float a[5]; • a[0]=9.9; • a[1]=12.0; • a[2]=13.1; • a[3]=8.3; • a[4]=10.5; • for(inti=0; i<5; i++) • { • cout<<”Value in a[“<<i<<”] = ”<<a[i]<<endl; • } • }
Program takes input in an Array then print out it on screen. void main() { Intabc[5]; for(inti=0; i<5; i++) { cout<<”Enter the Value in element ”<<i; cin>>abc[i]; } cout<<”Output in reverse order: ”<<endl; for (inti=4; i>=1; i--) { cout<<”Value in a[“<<i<<”] = ”<<abc[i]<<endl; } }
Program calculate Sum of all entries of Array. { floatabc[5], sum, avr; sum=avr=0.0; for (inti=0; i<=4; i++) { cout<<”Enter the Value in element ”<<i; cin>>abc[i]; } for (inti=0; i<=4; i++) { sum=sum+abc[i]; } avr=sum/5.0; cout<<”Sum of Array Value= ”<<sum<<endl; cout<<”Average of Array Value= ”<<avr<<endl; }
.write a program to enter temperatures for seven days into a one _dimensional array "temp".compute the avarege temperature and display of array and calculated average temperature on screen main() { float temp[7],sum=0; int1=0; clrscr(); while (1<=6) { cout <<"enter temp.of day "<<i+1<<"?"; cin >>temp[i]; sum=sum+temp[i]; i++; } cout<<"naveragetepmerature:"<<sum/ 7. 0 ; }
Program finds out the Max value from Array. { floatabc[5], max; sum=avr=0.0; for (inti=0; i<=4; i++) { cout<<”Enter the Value in element ”<<i; cin>>abc[i]; } max=abc[0]; for (inti=i; i<=4; i++) { if(abc[i] > max) { max=abc[i]; } } cout<<”Maximum Value is= ”<<max<<endl; }
Program to input in two Arrays then calculate their Sum and store that in a third Array. float ar1[5],ar2[5], sum[5]; sum=avr=0.0; cout<<”First Array: ”<<endl; for (inti=0; i<=4; i++) { cout<<”Enter the Value in element ”<<i; cin>>abc[i]; } cout<<”Second Array: ”<<endl; for (inti=0; i<=4; i++) { cout<<”Enter the Value in element ”<<i; cin>>abc[i]; } for (inti=1; i<=4; i++) { sum[i]=ar1[i]+ar2[i]; cout<<ar1[i]<<” + ”<<ar2[i]<<” = ”<<sum[i]<<endl; } }
Program to input integers in an Array then take any integer’s input and find out the location of entered integer in the Array. { floatabc[5], p, n; for (inti=0; i<=4; i++) { cout<<”Enter the Value in element ”<<i; cin>>abc[i]; } p=0; cout<<”Enter any Number: ”; cin>>n; for (inti=0; i<=4; i++) { if(n==abc[i]) { p=i+1; break; } }
if(p==0) • cout<<”Number not Found”; • else • cout<<”Number found at Position= ”<<p<<endl; • }
Example1: Using Arrays for ( i = 0 ; i < 10 ; i ++ ) { cin >> age [ i ] ; }
Initializing an Array int age [ 10 ] = { 0,0,0,0,0,0,0,0,0,0 } ; int age[ 10 ] = { 0 } ;
Initializing an Array int age [ ] = { 1,2,3,4,5,6,7,8,9,10 } ; for ( i = 0 ; i < 10 ; i ++ ) ‘ i ‘ will have value from 0 to 9
Program which initialize the values in an Array then Print them out. • #include<iostream.h> • void main() • { • float temp[5] = {66.2, 23.6, 67.9, 55.4, 102.0}; • for(inti=0; i<=4; i++) • { • cout<<”temp[“<<i<<”] = ”<<temp[i]<<endl; • } • }
Copying Arrays • Data types should be identical • Size should be same int a [ 10 ] ; int b [ 10 ] ;
Copying Arrays To copy from array “ a ” to array “ b ” : b [ 0 ] = a [ 0 ] ; b [ 1 ] = a [ 1 ] ; b [ 2 ] = a [ 2 ] ; b [ 3 ] = a [ 3 ] ; … … … … … … b [ 10 ] = a [ 10 ] ;
Copying Arrays for ( i =0 ; i < 10 ; i ++ ) b [ i ] = a [ i ] ;
Array Declaration data type name size