39
Introduction to C/C++

Introduction to C/C++

  • Upload
    jack

  • View
    39

  • Download
    10

Embed Size (px)

DESCRIPTION

Introduction to C/C++. Hello World. #include using namespace std; void main( int argc, char **argv ) { cout

Citation preview

Page 1: Introduction to C/C++

Introduction to C/C++

Page 2: 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;

}

Page 3: Introduction to C/C++

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 ……

Page 4: Introduction to C/C++

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 windowcout << “Hello World!” << endl;

cin.get( ); //read a keyreturn 0; //return

}

Page 5: Introduction to C/C++

#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;

}

Page 6: Introduction to C/C++

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;

}

Page 7: Introduction to C/C++

int main( int argc, char **argv )

• 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**

#include <iostream>using namespace std;void main( int argc, char **argv ) {

cout << “Hello World!” << endl;cin.get( );return 0;

}

Page 8: Introduction to C/C++

{ …. }

• 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;

}

Page 9: Introduction to C/C++

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;

}

Page 10: Introduction to C/C++

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.

Page 11: Introduction to C/C++

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 << endlreturn ;

Page 12: Introduction to C/C++

• An empty page

Page 13: Introduction to C/C++

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;

}

Page 14: Introduction to C/C++

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 ];

……

Page 15: Introduction to C/C++

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?

Page 16: Introduction to C/C++

#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”);

}

Stop the program

Page 17: Introduction to C/C++

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

Page 18: Introduction to C/C++

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)?

Page 19: Introduction to C/C++

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?

Page 20: Introduction to C/C++

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?

Page 21: Introduction to C/C++

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 studentsstring name[n];int score[n];But ERROR!

Page 22: Introduction to C/C++

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 studentsstring name[n];int score[n];

But ERROR! n is not a constant.

Page 23: Introduction to C/C++

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 1024int n; // n is the number of studentsstring name[max_students];int score[max_students];

Page 24: Introduction to C/C++

Example ThreeUsing C++

class Student_Record {public:

string name;int score; Student_Record ( ) { // constructor

name.clear( );score = 0;

}};

Page 25: Introduction to C/C++

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?

Page 26: Introduction to C/C++

Example ThreeUsing C++

• Algorithm

Show a message “Please enter #students”

For each studentEnter name and score

End for

Compute avarage scoreCompute score rangeShow the average scoreShow the score range

Page 27: Introduction to C/C++

Example ThreeUsing C++

• Algorithm

Show a message “Please enter #students”

For each studentEnter name and score

End for

Compute avarage scoreCompute score rangeShow the average scoreShow the score range

Management system

Page 28: Introduction to C/C++

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:

};

Page 29: Introduction to C/C++

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;

};

Page 30: Introduction to C/C++

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;

}

}

Page 31: Introduction to C/C++

Example Threevoid computeAverage( )

void ManagementSystem:: computeAverage( )

{

int totalScore = 0;

for ( int i = 0; i < mCurNumOfStudents ; ++i ) {

totalScore += sr[ i ].score;

}

mAverageScore = totalScore / mCurNumOfStudents ;

}

Page 32: Introduction to C/C++

Example Threevoid computeAverage( )

void ManagementSystem:: computeAverage( )

{

int totalScore = 0;

for ( int i = 0; i < mCurNumOfStudents ; ++i ) {

totalScore += sr[ i ].score;

}

// BUG?

mAverageScore = totalScore / mCurNumOfStudents ;

}

Page 33: Introduction to C/C++

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;

}

Page 34: Introduction to C/C++

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 ;

}

Page 35: Introduction to C/C++

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 divisionmAverageScore = totalScore / mCurNumOfStudents ;

}

Page 36: Introduction to C/C++

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 ;

}

Page 37: Introduction to C/C++

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 ;

}

Page 38: Introduction to C/C++

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 ;

}

Page 39: Introduction to C/C++

• Please refer to the tutorial.