400 likes | 549 Views
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;
E N D
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; void main( int argc, char **argv ) { cout << “Hello World!” << endl; cin.get( ); return 0; } The purpose of the program is ……
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 }
#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; }
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; }
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**
{ …. } • 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; }
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; }
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.
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 ;
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; }
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 ];
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?
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”); }
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].
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)?
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?
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?
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!
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.
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];
Example ThreeUsing C++ class Student_Record { public: string name; int score; Student_Record ( ) { // constructor name.clear( ); score = 0;} };
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?
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
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
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: };
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; };
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; } }
Example Threevoid computeAverage( ) void ManagementSystem:: computeAverage( ) { int totalScore = 0; for ( int i = 0; i < mCurNumOfStudents ; ++i ) { totalScore += sr[ i ].score; } mAverageScore = totalScore / mCurNumOfStudents ; }
Example Threevoid computeAverage( ) void ManagementSystem:: computeAverage( ) { int totalScore = 0; for ( int i = 0; i < mCurNumOfStudents ; ++i ) { totalScore += sr[ i ].score; } // BUG? mAverageScore = totalScore / mCurNumOfStudents ; }
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; }
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 ; }
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 ; }
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 ; }
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 ; }
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 ; }