150 likes | 260 Views
Review of basic structured programming. M Taimoor Khan taimoorkhan@ciit-attock.edu.pk. Arrays. #include< iostream.h > void main() { const int SIZE = 2; int age[3]; for( int i =0; i <3; i ++) cin >>ages[ i ]; int courseCode [3] = {4342, 3423, 3341}; int num[SIZE]; num[0] = 4;
E N D
Review of basic structured programming M Taimoor Khan taimoorkhan@ciit-attock.edu.pk
Arrays #include<iostream.h> void main() { const intSIZE = 2; int age[3]; for(inti=0; i<3; i++) cin>>ages[i]; intcourseCode[3] = {4342, 3423, 3341}; int num[SIZE]; num[0] = 4; num[1] = 5; for(int j = 0; j<SIZE; j++) cout<< num[j] <<endl; }
2D Arrays #include<iostream.h> Void main() { const int HORIZONTAL = 3; const int VERTICAL = 3; float point[HORIZONTAL][VERTICAL]; for(inti=0; i<HORIZONTAL; i++) { for(int j=0; j<VERTICAL; j++) { cout << “Please Enter the X and Y value of the point”; cin>>point[i][j]; } } for(int k=0; k<HORIZONTAL; k++) for(int l=0; l<VERTICAL; k++) { cout << “Please Enter the X and Y value of the point”; cin>>point[i][j]; } }
2D Array Initialization #include<iostream.h> void main() { const int DISTRICT = 3; const int MONTH = 2; double sales[DISTRICT][MONTH] = { { 5.66, 6.44 }, { 3.24, 2.55 }, { 9.77, 43.3 } }; }
Passing Array to a function #include<iostream.h> const int DISTRICT = 3; const int MONTH = 2; void display( double[DISTRICT][MONTH]) void main() { double sales[DISTRICT][MONTH] = { { 5.66, 6.44 }, { 3.24, 2.55 }, { 9.77, 43.3 } }; display(sales); } void display(double funcSales[DISTRICT][MONTH]) { for(inti=0;i<DISTRICT; i++) for(int j=0;j<MONTH; j++) cout<<funcSales[DISTRICT][MONTH] <<endl; }
Strings • C-Strings • Inherited from C language • The only kind of strings available in C • They are also called pointers to char arrays • Objects of String class • Improved version of the traditional C-String • You no longer need to worry about creating an array of the light size to hold string variables • The string class assumes all the responsibility of memory management • It allows the use of overloaded operators
C-String Variables #include<iostream.h> void main() { const int MAX = 80; char str[MAX]; cout << “Enter a String”; cin >> str; cout << “You Entered \n”; cout <<str; }
Avoiding Buffer Overflow #include<iostream.h> void main() { const int MAX = 80; char str[MAX]; cout << “Enter a String”; cin >> setw(MAX) >> str; cout << “You Entered \n”; cout <<str; }
C-String Constant #include<iostream.h> void main() { char str = “This is a constant string”; cout << str <<endl; }
Reading Embedded Blanks #include<iostream.h> void main() { const int MAX = 80; char str[MAX]; cout << “Enter a String”; cin.get(str, MAX); cout << “You Entered \n”; cout <<str; }
Reading Multiple Lines #include<iostream.h> void main() { const int MAX = 2000; char str[MAX]; cout << “Enter a String of multiple lines”; cin.get(str, MAX, ‘$’); cout << “You Entered \n”; cout <<str; }
Reading C-String by Characters #include<iostream.h> #include<string.h> void main() { const int MAX = 80; char str[MAX]; cout << “Enter a String”; cin >> str; for(inti=0; i<strlen(str); i++) cout << str[i] << “\t”; }
C-String Copy #include<iostream.h> #include<string.h> void main() { char str1[] = “This is a constant string”; const int MAX = 100; char str2[MAX]; strcpy(str2, str1); cout << str2 <<endl; }
Arrays of String #include<iostream.h> void main() { const int DAYS = 7; const int MAX = 10; char weekDays[DAYS][MAX] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}; for(inti=0; i<DAYS; i++) cout << weekDays[i] << endl; }