1 / 13

Programming assignments

Programming assignments. ชื่องาน (subject) : program เขียนด้วยภาษา C หรือ C++ มีทั้งหมด 7 ข้อ กำหนดส่ง 29 กรกฎาคม 2554. ข้อ 1. สร้างฟังก์ชันจำนวน 3 ฟังก์ชัน โดยให้แต่ละฟังก์ชันมีการประกาศตัวแปร array ขนาดใหญ่ แต่ต่างกันที่พื้นที่เก็บข้อมูลดังนี้

ida
Download Presentation

Programming assignments

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. Programming assignments • ชื่องาน (subject) : program • เขียนด้วยภาษา C หรือ C++ • มีทั้งหมด 7 ข้อ • กำหนดส่ง 29 กรกฎาคม 2554

  2. ข้อ 1 • สร้างฟังก์ชันจำนวน 3 ฟังก์ชัน โดยให้แต่ละฟังก์ชันมีการประกาศตัวแปร array ขนาดใหญ่ แต่ต่างกันที่พื้นที่เก็บข้อมูลดังนี้ • ฟังก์ชัน first ใช้ตัวแปรแบบ static • ฟังก์ชัน second ใช้ตัวแปรแบบ stack-dynamic • ฟังก์ชัน third ใช้ตัวแปรแบบ heap-dynamic • ในฟังก์ชั่น main ให้มีการเรียกใช้ฟังก์ชันทั้ง 3 เป็นจำนวนหลายครั้ง(>100,000) แสดงเวลาสำหรับการเรียกใช้แต่ละฟังก์ชัน เปรียบเทียบเวลาและอธิบายผลที่ได้ พร้อมเหตุผล

  3. ข้อ 2 • กำหนดให้สร้าง array 2 มิติที่มีขนาดใหญ่ (>100x100) • ทำการบวกค่าของสมาชิกทุกตัวใน array แล้วแต่ผล • โดยเปรียบเทียบเวลาของ 2 วิธีต่อไปนี้ • เข้าถึงโดยใช้ subscript • เข้าถึงโดยใช้ pointer และใช้การคำนวณหาตำแหน่งใน array โดยใช้ การคำนวณ mapping function • วิธีใดมีประสิทธิภาพในแง่เวลาดีกว่า • นศ.คิดว่าวิธีใดมี reliability ดีกว่า เพราะเหตุใด

  4. ข้อ 3 • ทดสอบโปรแกรมในภาษา C ต่อไปนี้ โดยกำหนดให้ • a และ b เป็น non-local variable • ฟังก์ชัน fun ทำการเพิ่มค่า a อีก 10 และ return ค่า a a = 10; b = a + fun(); printf(“function call on the right, b=%d\n",b); a = 10; b = fun()+a; printf(" function call on the right, b= %d\n",b); • อภิปรายผลที่ได้

  5. ข้อ 4 • สร้าง overloaded operator ในภาษา C++ โดย • กำหนดชนิดข้อมูลขึ้นใหม่สำหรับ string • นำ string 2 สายมาเชื่อมต่อกันด้วยการใช้เครื่องหมาย “+” • ทดสอบโดยการสร้าง string และเรียกใช้ operator นี้

  6. ข้อ 5 • จาก pseudocode ต่อไปนี้ จงเขียนโปรแกรมโดยคำสั่งทำซ้ำ k = (j+13)/27 loop: if k>10 then goto out k = k+1 i = 3*k-1 goto loop out: …..

  7. ข้อ 6 • จาก pseudocode ที่เขียนด้วย syntax ภาษา C ต่อไปนี้ จงเขียนฟังก์ชันที่ทำการส่งผ่าน parameter โดยใช้วิธี • Passed by value • Passed by reference • ระบุค่าที่ได้ของตัวแปร list หลังจากการทำงาน void fun(int first, in second) { first += first; second += second; } void main() { int list[2] = {1,3}; fun(list[0], list[1]); }

  8. ข้อ 7 • สร้าง generic function สำหรับการหาค่า max ของตัวแปร 2 ตัว • โดยสร้างเป็น template เพื่อทำการเปรียบเทียบและคืนค่าที่มากที่สุด

  9. 1. int list[5] = { 1, 2, 3, 4, 5 } ; ถ้าสั่งพิมพ์ list[3] จะแสดงค่าอะไร 4 2. int list[5] = { 1 } ; ถ้าสั่งพิมพ์ list[0] และ list[4] จะแสดงค่าอะไร 1 และ 0 3. ถ้า intergerใช้เนื้อที่ 2 ไบต์ในการเก็บข้อมูล ตัวแปร list จะใช้ เนื้อที่เก็บข้อมูลกี่ไบต์ 10 ไบต์ 4. ถ้าอยากทราบจำนวนสมาชิกของตัวแปร list จะใช้คำสั่งอะไร คำแนะนำ ใช้คำสั่ง sizeofsizeof(list)/sizeof(int)

  10. 5. char s[ ] = “517321”; 7 ไบต์ตัวอักษรใช้เนื้อที่ 1 ไบต์ ตัวแปร s ใช้เนื้อที่กี่ไบต์ในการเก็บข้อมูล 6. enum Day {Sun,Mon,Tue,Wed,Thu,Fri,Sat} ; Day d = Sun; ถ้าใช้คำสั่ง printf(“%d”, d); ในภาษา C จะแสดงค่าอะไร 0 7. ถ้าใช้คำสั่ง d++; ในภาษา C จะคอมไพล์ผ่านหรือไม่ ถ้าคอมไพล์ผ่านตัวแปร d มีค่าอะไร ผ่าน d มีค่า 1 หรือ Mon 8. ถ้าใช้คำสั่ง d++; ในภาษา C++ จะคอมไพล์ผ่านหรือไม่ ถ้าคอมไพล์ผ่านตัวแปร d มีค่าอะไร ไม่ผ่านเพราะไม่มี coercion

  11. 9. union un{int x; float r; char c; } u; ถ้าตัวอักษรใช้เนื้อที่ 1 ไบต์ Integer ใช้เนื้อที่ 2 ไบต์ Float ใช้เนื้อที่ 4 ไบต์ 4 ไบต์ ตัวแปร U ใช้เนื้อที่กี่ไบต์ในการเก็บข้อมูล 10. union u_color{char r; char g; char b; } rgb; rgb.r = 'r'; rgb.g = 'g'; rgb.b = 'b'; printf("%c %c %c", rgb.r, rgb.g, rgb.b); แสดงผลที่ได้จากคำสั่ง printfb bb

  12. จงอธิบายความหมายของคำต่อไปนี้ พร้อมยกตัวอย่าง 1. Coercion 2. Casting 3. Operator overloading

  13. จงอธิบายความหมายของคำต่อไปนี้ พร้อมยกตัวอย่าง 4. Precedence rules 5. Associativity rules 6. Functional side effect

More Related