1 / 45

Functions and Arrays

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.

yuma
Download Presentation

Functions and Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions and Arrays

  2. 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

  3. 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

  4. 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

  5. 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.

  6. Assignment • Find factorial by reference parameters

  7. Built in function • Already been define • Part of the language • Can be used in any program

  8. 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

  9. 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

  10. 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

  11. 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();

  12. 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();

  13. 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

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

  15. 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

  16. Cos Function • Cos(x); • X is angle in radian • If x=0.0, it returns 1.0

  17. Sin function • Sin(x); • X is angle or value in radians • If x=0.0, it returns 0 • Same as tan, log etc

  18. What is • #include<iostream.h>? • What is main()

  19. ARRAYS

  20. Take 10 ages • Calculate avergaes • How many Variables you need?

  21. Like • Int age1, age2, age3……age10; • What if you have to calculate 100 ages • So this means we want some technique to handle this

  22. 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

  23. 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

  24. 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

  25. Declaring one-dimensional Arrays • In C each array has • name • data type • size • They occupy continuous area of memory

  26. 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] ;

  27. Referring to Array Elements Array name e.g. age index number age [ 4 ]

  28. .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; }

  29. 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; • } • }

  30. 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; } }

  31. 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; }

  32. .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 ; }

  33. 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; }

  34. 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; } }

  35. 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; } }

  36. if(p==0) • cout<<”Number not Found”; • else • cout<<”Number found at Position= ”<<p<<endl; • }

  37. Example1: Using Arrays for ( i = 0 ; i < 10 ; i ++ ) { cin >> age [ i ] ; }

  38. Initializing an Array int age [ 10 ] = { 0,0,0,0,0,0,0,0,0,0 } ; int age[ 10 ] = { 0 } ;

  39. 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

  40. 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; • } • }

  41. Copying Arrays • Data types should be identical • Size should be same int a [ 10 ] ; int b [ 10 ] ;

  42. 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 ] ;

  43. Copying Arrays for ( i =0 ; i < 10 ; i ++ ) b [ i ] = a [ i ] ;

  44. Array Declaration data type name size

More Related