1 / 15

Review of basic structured programming

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;

tivona
Télécharger la présentation

Review of basic structured programming

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. Review of basic structured programming M Taimoor Khan taimoorkhan@ciit-attock.edu.pk

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

  3. 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]; } }

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

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

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

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

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

  9. C-String Constant #include<iostream.h> void main() { char str = “This is a constant string”; cout << str <<endl; }

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

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

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

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

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

  15. Standard C++ String Class

More Related