198110 Fundamentals of Computer Programming

2/2556

Lab 1: Fundamental C++

วัตถุประสงค์


1. เพื่อทำความเข้าใจการรับเข้าและส่งออกข้อมูลโดยใช้ภาษา C++

2. เพื่อทำความเข้าใจชนิดของข้อมูล ตัวแปร และชนิดของตัวแปรในภาษา C++

3. เพื่อทำความเข้าใจการให้ค่าและการคำนวณแบบง่ายโดยใช้ภาษา C++

ทบทวนเนื้อหา


1) Printing values to a console (monitor)

cout << exp1 << exp2 << … << expn;

Example:        cout << 2 << “ ” << “Hello World” << endl;

2) Data Types

Data type

Contents

Size (bytes)

Example

char

Character

1

 ‘A’ ‘b’ ‘+’ ‘\n’ ‘\t’

string

Characters

0 - 4294967295

“” “Hello”

“Hi, my name is Namtarn\n”

int, long

Integer

4(depend on OS.), 8

4, 5, 6, 999, 0, -2, -8

float, double

Real number

4, 8

9.06, 99.11, -8.9

หาความยาวของ String โดยใช้คำสั่่ง strlen(“Hello\n”) มีค่าเท่ากับ 6

3) Getting values from a user

declare variables;

cin >> var1 >> var2 >> … >> varn;


Example:         
int x, y;

float z;

cin >> x >> y >> z;


4) Assigning values

varName = expression;

Example:         int x = 5;

int y;

y = 6;

x = x + (y % 2) + 30 * 6 - 9;

5) Operation order (without parentheses)

Top -> bottom

If in the same level, left -> right

6) (Extra) Useful math functions

#include <cmath>         // Put this before int main()

float z = pow(x,y);        // Calculate x to the power of y

float a = sqrt(x);         // Calculate square root x


In-Class Problems


P1) จงเขียนโปรแกรมภาษา C++ แสดงชื่อ รหัสนักศึกษา และภาควิชา ตามตัวอย่างผลรันต่อไปนี้

Hello World!

        Kornchawal Chaipah

        55045678-9

        Computer Engineering

Good Bye~

P2) จงเขียนโปรแกรมภาษา C++ โดยกำหนดให้มีจำนวนเต็มสองตัว x และ y ให้มีค่า 3 และ 4 ตามลำดับ จากนั้นให้แสดงผลลัพธ์ทางคณิตศาสตร์ (+ - * / % และค่าเฉลี่ย) ของเลขทั้งสองตัวนี้ตามตัวอย่างต่อไปนี้

x = 3, y = 4

x + y = 7

x - y = -1

x * y = 12

x / y = 0

x % y = 3

Average = 3.5

P3) จงเขียนโปรแกรมภาษา C++ โดยให้ใช้คำสั่งเหมือนในข้อ P2) ยกเว้นกำหนดให้ x และ y เป็นจำนวนจริง ให้เปรียบเทียบผลลัพธ์และวิธีทำของ P2) และ P3) ว่าเหมือนหรือต่างกันอย่างไร เหตุใดจึงเป็นเช่นนั้น

x = 3, y = 4

x + y = 7

x - y = -1

x * y = 12

x / y = 0.75

Average = 3.5

P4) จงเขียนโปรแกรมภาษา C++ เพื่อคำนวนหาค่าจำนวนจริง y ต่างๆดังนี้

y1 = x * 2 + 3 - 8 / 9

y2 =  x * (2 + 3) - 8 / 9

y3 = x * 2 + 3 - (8 / 9)

โดยให้ x เป็นจำนวนจริง มีค่าเท่ากับ 9 ให้เปรียบเทียบค่า y1, y2 และ y3 ว่าเหมือนหรือต่างกันอย่างไร เพราะเหตุใดจึงเป็นเช่นนั้น

P5) จงเขียนโปรแกรมภาษา C++ เพื่อรับข้อมูลตัวอักษรนำหน้าชื่อและอายุของผู้ใช้เข้ามา และแสดงออกทางหน้าจอ โดยให้แสดงออกมาใน 2 รูปแบบดังตัวอย่างข้างล่างนี้ โดยตัวหนาเป็นค่าที่ผู้ใช้กรอกเข้ามา

รูปแบบที่ 1

Please enter your initial: K

Please enter your age: 19

You are K. You are 19 years old.

รูปแบบที่ 2

Please enter your initial and age: K 19

You are K. You are 19 years old.

P6) Midterm Exam 2007-2

P7) Midterm Exam 2011-1

Homework Problems


P8) Midterm Exam 2007-1

P9) Midterm Exam 2008-1


P10) Midterm Exam 2010-1

หนังสือเล่มส้ม :

P11) ข้อ 7 หน้า 142

P12) ข้อ 8 หน้า 142

P13) ข้อ 9 หน้า 142

P14) ข้อ 15 หน้า 144

CORPAS : N/A

 /

By Aj. Jiradej Ponsawat and Aj.Kornchawal Chaipah