1 / 39

Introduction to C/C++

Introduction to C/C++. Hello World. #include <iostream> using namespace std; void main( int argc, char **argv ) { cout << “Hello World!” << endl; cin.get( ); return 0; }. What is the purpose of the program?. #include <iostream> using namespace std;

jack
Download Presentation

Introduction to C/C++

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. Introduction to C/C++

  2. Hello World #include <iostream> using namespace std; void main( int argc, char **argv ) { cout << “Hello World!” << endl; cin.get( ); return 0; }

  3. What is the purpose of the program? #include <iostream> using namespace std; void main( int argc, char **argv ) { cout << “Hello World!” << endl; cin.get( ); return 0; } The purpose of the program is ……

  4. Hello World #include <iostream> // header file <- comment using namespace std; //namespace /* <- comment Entry function. There is only one main in the entire project. */ int main( int argc, char **argv ) // parameter list { //output to console window cout << “Hello World!” << endl; cin.get( ); //read a key return 0; //return }

  5. #include <iostream> • Take everything in the header and copy it into the program. #include <iostream> using namespace std; void main( int argc, char **argv ) { cout << “Hello World!” << endl; cin.get( ); return 0; }

  6. using namespace std; • Inform the compiler to use a group of functions which are defined in the standard library, i.e. std. #include <iostream> using namespace std; void main( int argc, char **argv ) { cout << “Hello World!” << endl; cin.get( ); return 0; }

  7. int main( int argc, char **argv ) #include <iostream> using namespace std; void main( int argc, char **argv ) { cout << “Hello World!” << endl; cin.get( ); return 0; } • int : the return data type • main: the function name • ( …… ) : parameter list • int argc : argc is a parameter. • Its data type is int • char **argv : argv is a parameter. • Its datatype is char**

  8. { …. } • A structure block e.g., { int a; a = 1 + 2; } #include <iostream> using namespace std; void main( int argc, char **argv ) { cout << “Hello World!” << endl; cin.get( ); return 0; }

  9. cout << “Hello World!” << endl; • cout : a functionl output on the console window • << : an operator • “Hello World!” : a string • endl : a function; output a newline • ; : end of a statement. #include <iostream> using namespace std; void main( int argc, char **argv ) { cout << “Hello World!” << endl; cin.get( ); return 0; }

  10. Primitive Data Type • int : integer ( 4 bytes ) • float : single precision floating point number ( 4 bytes ) • double : double precision floating point number ( 8 bytes ) • char : character (1 byte) • unsigned int : unsigned integer ( 4 bytes ) • int * : a pointer to integer ( 4 bytes ) • char * : a pointer to integer ( 4 bytes ) • char ** : a pointer to an integer pointer ( 4 bytes ) A size of a pointer is 4 bytes.

  11. Example OneFixing the bugs //Find the bugs and fix them. include <iostream Using namespace standard; void main( int argc, char ** argv ) { int a = 2 + 3; cout << a << endl return ;

  12. An empty page

  13. Example OneFixing the bugs #include <iostream> using namespace std; // case sensitive int main( int argc, char ** argv ) { int a = 2 + 3; cout << a << endl; return 0; }

  14. Array …… An array is a container object that store a fixed number of values of a single type. e.g. int a[16]; // a[0], a[1], …, a[15] double b[32]; // b[0], b[1], …, b[31] const int numOfElements = 1000; char messageStr[numOfElements ];

  15. Example Two • Write a program to read a string and then output the characters of the string one by one at each new line. #include <iostream> #include <string> using namespace std; void main ( int argc, char **argv ) { string message; cin >> message; int n = message.size( ); for ( int i = 0; i < n; ++i ) { cout << message[i] << endl;} cin.get( ); } // build and run the program. What do you see? How to stop it?

  16. Stop the program #include <iostream> #include <string> using namespace std; void main ( int argc, char **argv ) { string message; cin >> message; int n = message.size( ); for ( int i = 0; i < n; ++i ) { cout << message[i] << endl;} system(“pause”); }

  17. Example Three • Write a program to enter the information of students. • The information of a student includes: • name • score Show the average score of the students. Show the score range, i.e., [min, max].

  18. Example Three • Write a program to enter the information of students. • The information of a student includes: • name • score What is(are) the data structure(s)?

  19. Example Three • Write a program to enter the information of students. • The information of a student includes: • name • score What is(are) the data structure(s)? name? score?

  20. Example Three • Write a program to enter the information of students. • The information of a student includes: • name • score What is(are) the data structure(s)? name? score? Number of students?

  21. Example Three • Write a program to enter the information of students. • The information of a student includes: • name • Score What is(are) the data structure(s)? name? score? Number of students? int n; // n is the number of students string name[n]; int score[n]; But ERROR!

  22. Example Three • Write a program to enter the information of students. • The information of a student includes: • name • Score What is(are) the data structure(s)? name? score? Number of students? int n; // n is the number of students string name[n]; int score[n]; But ERROR! n is not a constant.

  23. Example Three • Write a program to enter the information of students. • The information of a student includes: • name • Score What is(are) the data structure(s)? name? score? Number of students? #define max_students 1024 int n; // n is the number of students string name[max_students]; int score[max_students];

  24. Example ThreeUsing C++ class Student_Record { public: string name; int score; Student_Record ( ) { // constructor name.clear( ); score = 0;} };

  25. Example ThreeUsing C++ • Write a program to enter the information of students. • The information of a student includes: • name • score Show the average score of the students. Show the score range, i.e., [min, max]. • Algorithm?

  26. Example ThreeUsing C++ • Algorithm Show a message “Please enter #students” For each student Enter name and score End for Compute avarage score Compute score range Show the average score Show the score range

  27. Example ThreeUsing C++ • Algorithm Show a message “Please enter #students” For each student Enter name and score End for Compute avarage score Compute score range Show the average score Show the score range Management system

  28. Example Three: Using C++ #define MAX_STUDENTS 1024 class ManagementSystem { protected: int mCurNumOfStudents; //number of students Student_Record sr[MAX_STUDENTS]; float mAverageScore; int mMinScore, mMaxScore; public: };

  29. Example Three: Using C++ #define MAX_STUDENTS 1024 class ManagementSystem { public: ManagementSystem( ) { mCurNumOfStudents = 0; } void inputRecords( ); void computeAverage( ); void computeSoreRange( ); float getAverageScore( ) const; void getScoreRange( int &min, int &max ) const; };

  30. Example Threevoid inputRecords( ) void ManagementSystem:: inputRecords( ) { int n; cout << “Number of students:”; cin >> n; if ( n <= 0 ) return; else mCurNumOfStudents = n; for ( int i = 0; i < n; ++i ) { cout << “student:” << i << “\t” << “name:”; cin >> sr[ i ].name; cout << “score:”; cin >> sr[ i ].score; } }

  31. Example Threevoid computeAverage( ) void ManagementSystem:: computeAverage( ) { int totalScore = 0; for ( int i = 0; i < mCurNumOfStudents ; ++i ) { totalScore += sr[ i ].score; } mAverageScore = totalScore / mCurNumOfStudents ; }

  32. Example Threevoid computeAverage( ) void ManagementSystem:: computeAverage( ) { int totalScore = 0; for ( int i = 0; i < mCurNumOfStudents ; ++i ) { totalScore += sr[ i ].score; } // BUG? mAverageScore = totalScore / mCurNumOfStudents ; }

  33. Example Threevoid computeAverage( ) void ManagementSystem:: computeAverage( ) { int totalScore = 0; for ( int i = 0; i < n; ++i ) { totalScore += sr[ i ].score; } // mCurNumOfStudents = 0? Division by zero!!! mAverageScore = totalScore / mCurNumOfStudents; }

  34. Example Threevoid computeAverage( ) void ManagementSystem:: computeAverage( ) { if ( mCurNumOfStudents = 0 ) { mAverageScore = 0; return; } int totalScore = 0; for ( int i = 0; i < mCurNumOfStudents ; ++i ) { totalScore += sr[ i ].score; } // There is still a BUG!!!!!!! mAverageScore = totalScore / mCurNumOfStudents ; }

  35. Example Threevoid computeAverage( ) void ManagementSystem:: computeAverage( ) { if (mCurNumOfStudents = 0 ) { mAverageScore = 0; return; } int totalScore = 0; for ( int i = 0; i < mCurNumOfStudents ; ++i ) { totalScore += sr[ i ].score; } // integer division mAverageScore = totalScore / mCurNumOfStudents ; }

  36. Example Threevoid computeAverage( ) void ManagementSystem:: computeAverage( ) { if (mCurNumOfStudents = 0 ) { mAverageScore = 0; return; } int totalScore = 0; for ( int i = 0; i < mCurNumOfStudents ; ++i ) { totalScore += sr[ i ].score; } mAverageScore = totalScore / (float) mCurNumOfStudents ; }

  37. Example Threevoid computeAverage( ) void ManagementSystem:: computeAverage( ) { if (mCurNumOfStudents = 0 ) { mAverageScore = 0; return; } int totalScore = 0; for ( int i = 0; i < mCurNumOfStudents ; ++i ) { totalScore += sr[ i ].score; } // There is still a BUG!!!!!!!!!!!!!!! mAverageScore = totalScore / (float) mCurNumOfStudents ; }

  38. Example Threevoid computeAverage( ) void ManagementSystem:: computeAverage( ) { if (mCurNumOfStudents = 0 ) { mAverageScore = 0; return; } int totalScore = 0; for ( int i = 0; i < mCurNumOfStudents ; ++i ) { totalScore += sr[ i ].score; } // Truncation error! The value mAverageScore may not be correct. // e.g., 973/10 = 97.3, but it cannot be represented as binary representation mAverageScore = totalScore / (float) mCurNumOfStudents ; }

  39. Please refer to the tutorial.

More Related