Docsity

Chapter - 08 - Programming fundamental - Arrays and Strings, Slides of Programming Languages

Partial preview of the text

Download Chapter - 08 - Programming fundamental - Arrays and Strings and more Slides Programming Languages in PDF only on Docsity!

โ‘ I n everyday life we commonly group similar

objects into units.

โ‘ In computer languages we also need to group

together data items of the same type. The most

basic mechanism that accomplishes this in C++ is

the array.

โ‘ Arrays are like structures in that they both group

a number of items into a larger unit.

โ‘ structure usually groups items of different types,

an array groups items of the same type.

#include #include<stdlib.h> using namespace std; int main() { int age[ 4 ]; //array โ€˜ageโ€™ of 4 ints for(int j= 0 ;j< 4 ;j++) { cout<<"Enter an age : "; cin>>age[j]; } for(int j= 0 ;j< 4 ;j++) cout<<"You entered "<<age[j]<<endl; system("PAUSE"); return 0 ; }

#include #include<stdlib.h> using namespace std; int main() { int month, day, total_days; int days_per_month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; cout << "\nEnter month (1 to 12): "; //get date cin >> month; cout << "Enter day (1 to 31): "; cin >> day; total_days = day; //separate days for(int j=0; j<month-1; j++) //add days each month total_days += days_per_month[j]; cout << "Total days from start of year is: " << total_days<< endl; system("PAUSE"); return 0; }

โ‘ Arrays in C++ can have multiple subscripts.

โ‘ Its common use is to represent tables of values

consisting of information arrange in rows and

columns.

โ‘ To identify particular element we must specify

two subscripts โ€“ first identifies the rows and

second identifies the columns.

#include #include<stdlib.h> #include //for setprecision, etc. using namespace std; const int DISTRICTS = 4; //array dimensions const int MONTHS = 3; int main() { int d, m; double sales[DISTRICTS][MONTHS]; //two-dimensional array //definition cout << endl; for(d=0; d<DISTRICTS; d++) //get array values for(m=0; m<MONTHS; m++) { cout << "Enter sales for district " << d+1; cout << ", month " << m+1 << ": "; cin >> sales[d][m]; //put number in array }

cout << "\n\n"; cout << " Month\n"; cout << " 1 2 3"; for(d=0; d<DISTRICTS; d++) { cout <<"\nDistrict " << d+1; for(m=0; m<MONTHS; m++) //display array values cout << setiosflags(ios::fixed) //not exponential << setiosflags(ios::showpoint) //always use point << setprecision(2) //digits to right << setw(10) //field width << sales[d][m]; //get number from array } //end for(d) cout << endl; system("PAUSE"); return 0; } //end main

#include #include //for setprecision, etc. using namespace std; const int DISTRICTS = 4; //array dimensions const int MONTHS = 3; int main() { int d, m; //initialize array elements double sales[DISTRICTS][MONTHS]= { { 1432.07, 234.50, 654.01 }, { 322.00, 13838.32, 17589.88 }, { 9328.34, 934.00, 4492.30 }, { 12838.29, 2332.63, 32.93 } }; cout << "\n\n"; cout << " Month\n"; cout << "1 2 3"; for(d=0; d<DISTRICTS; d++) { cout <<"\nDistrict " << d+1; for(m=0; m<MONTHS; m++) cout << setw(10) << setiosflags(ios::fixed) << setiosflags(ios::showpoint) << setprecision(2) << sales[d][m]; //access array element } cout << endl; return 0;