260 likes | 379 Vues
This guide explores the fundamental concept of arrays in programming, focusing on their declaration, usage, and operations in the C language. It details how to declare one-dimensional and multi-dimensional arrays, initialize them, and perform computations such as calculating averages and finding maximum values. The document includes practical examples of inputting values into arrays, performing various operations without using loops, and displaying results effectively. Learn how arrays help in managing large datasets by simplifying code structure and memory management.
E N D
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(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
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
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
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