1 / 26

ARRAYS

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

barid
Télécharger la présentation

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

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

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

  4. Arrays • It reduces the size of a program • Sequence of objects(elemnts) 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  26. Array Declaration data type name size

More Related