Computer Programming For Engineering: (Course Code: EECP 1290)
Computer Programming For Engineering: (Course Code: EECP 1290)
Today Topics
1. Introduction to Arrays:
char v[0]=a, v[1]=e ,v[2]=i ,v[3]=o ,v[4]=u ; The same will be represented easily as char v[5]={ a , e , i , o, u} ; char grade[6]={ A, B , , D , E , F} char fruits[4]= {apple, banana, cherry, mango } char subject[4]= { C++, Physics,Chemistry, Maths} int marks[11]={100, 90, 80 , 70, 60, 50,40,30,20,10,0} int speed[5]={80, 60, 40,20,0} In the above lines vowel[5], grade[6], fruits[4], subject[4], marks[11], speed[5] are ARRAYS.
What is Array ?
Here c[0], c[1] ,c[2] . . . ., c[10], c[11] are Variable names. Now let us define array ? An array is a sequence of variable names that shares same name c for variables c[0], c[1] ,c[2] . . . ., c[10], c[11] . A single dimensional array is just like one row of MATRIX. A multi dimensional array is just like MATRIX with rows and columns.
Name of array is c
c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] -45 6 0 72 1543 -89 0 62 -3 1 6453 78
int c[12]={-45,6,0,72,1543,-89,0,62,-3,1,6453,78}
Arrays are useful to store a large number of values of the same data type. Where Array are useful? (Applications) To create students data base with marks, details Bank Applications, Industry &Scientific applications , Air Ticket Reservations, . In all these applications a large data will be stored
{int i; M[0] M[1] M[2] M[3] M[4] int Marks[5] = {96, 85, 59, 90, 76}; for(i=0 ; i <= 4 ; i=i+1 ) { cout<<Marks[i]<<" ";Marks[4]=90 Marks[0]=96 96 85 59 90 76 Marks[1]=85 Marks[2]=59 Marks[3]=90 } getch(); return 0; } Program Ends
1 4 0 <=4 2 3 5 <= 4YES YES i= 4+1=5 NO 0+1=1 1+1=2 2+1=3 3+1=4
Program to Print Elements of Array in REVERSE ORDER int _tmain(int argc, _TCHAR* argv[])
{int i; M[0] M[1] M[2] M[3] M[4] int Marks[5] = {96, 85, 59, 90, 76}; for(i=4 ; i >= 0 ; i=i-1 ) { cout<<Marks[i]<<" ";Marks[0]=96 Marks[4]=76 76 90 59 85 96 Marks[3]=90 Marks[2]=59 Marks[1]=85 } getch(); return 0; } Program Ends
-1 NO 4 >=0 3 2 1 0 >= 0YES YES i= 0-1=-1 4-1=3 3-1=2 2-1=1 1-1=0