8.01.2554

เนื้อหาจากการประชุมครั้งที่ 8

การทำโจทย์ Lab4
ข้อ 1. โปรแกรมเกี่ยวกับการใช้คำสั่ง while
#include
int main (void)
{
int j;
j=2;
while(j<0)
{
printf("%d\n",j);
j=j+1;
}
printf("exit\n");
printf("j=%d",j);
return 0;
}
การทำงานของโปรแกรม
1. ประกาศตัวแปร j มีค่าเท่ากับ 2 เป็นตัวแปรชนิด int
2. คำสั่ง while(j<0) คือการทำงานภายใต้คำสั่งนี้จะทำงานเมื่อ ค่า j มีค่าน้อยกว่า 0
3. เมื่อค่า j<0 จะ print ค่าตัวเลขออกมา โดยที่ค่า j เพิ่มขึ้นทีละ 1
4. เมื่อค่า j>0 โปรแกรมจะ print คำว่า exit\n ออกมา
5. และ print j= ค่า j ที่นับได้
6. จบการทำงาน

ข้อ 2. โปรแกรมเกี่ยวกับการใช้คำสั่ง do...while
#include
int main(void)
{
int j;
j=2;
do
{
printf("%d\n",j);
j=j+1;
}
while(j<0);

printf("exit\n");
return 0;
}
การทำงานของโปรแกรม
1. ประกาศค่า j=2 เป็นตัวแปรชนิด int
2. เงื่อนไขของคำสั่ง do...while คือ j<0
3. โปรแกรมจะ print ค่า j ก่อน แล้วเพิ่มค่า j ขึ้นทีละ 1
4. เมื่อทำงานในวงเล็บของ do แล้วจึงจะเช็คเงื่อนไข ในบรรทัด while(j<0)
5. เมื่อค่า j เข้าเงื่อนไขก็จะวนกลับไปทำงานในวงเล็บคำสั่ง do พร้อมทั้งเพิ่มค่า j ขึ้นทีละ 1
6. เมื่อค่า j>0 แล้ว โปรแกรมจะ print คำว่า exit\n
7. จบการทำงาน

ข้อ 3. โปรแกรมที่แสดงค่าตัวเลขลดลงทีละ1
#include
int main(void)
{
int num,count;
printf("Enter Positive Number => ");
scanf("%d",&num);
count=num;

while(count>0)
{
num=num-1;
printf("%d\n",num);
count=count-1;
}

return 0 ;
}
การทำงานของโปรแกรม
1. print ว่า Enter Positive Number =>
2. รับค่า num ที่เป็นค่าชนิด int
3. กำหนดให้ count=num
4. เช็คว่าค่าที่รับเข้ามาว่าตรงกับเงื่อนไขคำสั่ง while(count > 0) หรือไม่
5. ถ้าค่าที่รับเข้ามาตรงกับเงื่อนไข ให้ค่า num ลดลงทีละ 1
6. แล้ว print ค่า num (ค่าตัวเลขที่ลดลงทีละ1)
7. ลดค่ารอบการนับ(count) ลงทีละ1
8. วนรอบการทำงาน เมื่อค่า num ยังตรงกับเงื่อนไขของคำสั่ง while
9. จบการทำงาน

ข้อ 4. โปรแกรมที่ใช้คำสั่ง while ในการวนรอบ
#include
int main(void)
{
int testnum =10;

while(testnum<=100)
{
printf("Put value over 100 or 50 to stop\n");
scanf("%d",&testnum);

if(testnum==50) break;
printf("Hello ,you did\'t enter value 50\n");
}

while(1)
{
printf("Enter 0 or 50 to stop\n");
scanf("%d",&testnum);
if(testnum == 0 testnum == 50) break;
}

return 0;
}
การทำงานของโปรแกรม
1. เช็คค่าตัวแปร testnum ว่ามีค่าน้อยกว่าหรือเท่ากับ 100 หรือไม่
2. ถ้าตรงตามเงื่อนไข โปรแกรมจะ print ว่า Put value over 100 or 50 to stop\n
3. รับค่าของตัวแปร testnum ที่เป็นตัวแปรชนิด int
4. เช็คว่าตัวแปร testnum เท่ากับ 50 หรือไม่
ถ้า testnum=50 จะไปทำงานคำสั่งwhile(1)ต่อ
แต่ถ้า testnum ไม่เท่ากับ 50 โปรแกรมจะ print ว่า Hello ,you did\'t enter value 50\n
5. เมื่อใส่ค่า testnum=50 แล้วจะเข้ามาทำงานภายใต้คำสั่ง while(1)
6. โดยจะ print ว่า Enter 0 or 50 to stop\n
7. รับค่าตัวแปร testnum
8. เช็คเงื่อนไขว่า...
ถ้าตัวแปร testnum = 0
หรือตัวแปร testnum = 50 โปรแกรมจะจบการทำงานทันที
แต่ถ้าไม่ตรงตามเงื่อนไขดังกล่าว โปรแกรมจะวนกลับไปทำงานจนกว่าโปรแกรมจะได้รับค่าตัวแปร testnum ที่ตรงตามเงื่อนไข
9. จบการทำงาน

9 ความคิดเห็น:

  1. วีรศักดิ์ ดังชนกนันท์1 สิงหาคม 2554 เวลา 22:01

    #inlcude
    int main (void)
    {
    int iRow;
    int iLimit;
    int iCol;

    printf("Input number interger btween 1 and 9 : ");
    scanf("%d",&iLimit);

    for(iRow=1;iRow<=iLimit;iRow++)
    {
    for(iCol=1;iCol<=iLimit;iCol++)
    {
    if(iRow > iCol)
    printf("*");
    else
    printf("%d",iCol);
    }

    }
    }


    แสดงผล
    Input number interger between 1 and 9 : 7
    1234567
    *234567
    **34567
    ***4567
    ****567
    *****67
    ******7

    ตอบลบ
  2. ไม่ระบุชื่อ2 สิงหาคม 2554 เวลา 23:19

    โปรแกรมรับค่าตัว เลขเข้ามา แล้วหาค่าแฟลก ของตัวเลขนั้น


    #include"stdio.h"
    int main(void)
    {
    int i,a,sum;
    i=1;
    printf("NUMBER : ");
    scanf("%d",&a);
    sum = a;
    while(i>0)
    {
    if(a>1)
    {
    sum = sum*(a-1);
    a--;
    }
    else
    i=-1;
    }
    printf("sum = %d",sum);
    return 0;
    }

    ผลลัพธ์
    NUMBER : 5
    sum = 120

    (น.ส.หัสรา วัจนะรัตน์ 5401011620542)

    ตอบลบ
  3. #include
    void main (void)
    {
    char out = 'n';
    do
    {
    printf ("\n Do you want to exit [Y]es or [N]o :");
    scanf ("%c",&out);
    }

    while (out != y);

    printf("End");

    return 0;
    }
    การทำงาน
    Do you want to exit [Y]es or [N]o:n
    Do you want to exit [Y]es or [N]o:n
    Do you want to exit [Y]es or [N]o:y
    End

    นายธน สุทธิธรรม 5401011630149

    ตอบลบ
  4. #include

    int main (void)
    {
    int count, sum, n;

    printf("Enter integer : ");
    scanf("%d",&n);

    sum = 0;

    for(count = 1; count <= n; count++)
    {
    sum += count;
    }
    printf("Sum = %d",sum);
    }

    ผลที่ได้คือ

    Enter integer : 9
    Sum = 45
    (นาย เบญจพล ศรีสันติธรรม 54-010116-2059-3)

    ตอบลบ
  5. ไม่ระบุชื่อ4 สิงหาคม 2554 เวลา 21:51

    โปรแกรม หาผลรวมคะแนน 10 คน
    #include"stdio.h"
    int main (void)
    {
    int sum;
    int i;
    i = 1;
    while(i <= 10)
    {
    int score; //ถ้าประกาศตัวแปรใน { } ข้างนอกจะไม่เห็น
    printf("score %d : ",i);
    scanf("%d",score);
    sum = sum+score;
    i++;
    }
    printf("sum = %d",sum);
    }

    ผล
    score 1 : 23
    score 2 : 85
    score 3 : 78
    score 4 : 12
    score 5 : 70
    score 6 : 33
    score 7 : 28
    score 8 : 77
    score 9 : 45
    score 10 : 36
    sum = 487

    นายประวีร์ แสงทอง 54-010116-2032-1

    ตอบลบ
  6. ไม่ระบุชื่อ5 สิงหาคม 2554 เวลา 10:56

    #include
    int main ()
    {
    int cost,wage;
    char fruit;
    printf("Apple 40 bath / kilogram\n");
    printf("Grape 200 bath / kilogram\n");
    printf("Orange 50 bath / kilogram\n");
    printf("Durian 60 bath / kilogram\n");
    printf("What fruit your want :");
    scanf("%s",&fruit);
    printf("how many fruit your want :");
    scanf("%d",&wage);
    if (wage<0)
    printf ("Error");
    else if (fruit=='a'||fruit=='A')
    printf ("cost is: %d",40*wage);
    else if (fruit=='g'||fruit=='G')
    printf ("cost is: %d",200*wage);
    else if (fruit=='o'||fruit=='O')
    printf ("cost is: %d",50*wage);
    else if (fruit=='d'||fruit=='D')
    printf ("cost is: %d",60*wage);
    else
    printf ("Error");

    }
    ผลเมื่อป้อน a,3
    จะได้ cost is :120

    ตอบลบ
  7. ไม่ระบุชื่อ5 สิงหาคม 2554 เวลา 10:58

    (นาย ปลวัชร เปรมสิรอำไพ 54-010116-2033-0)

    ตอบลบ
  8. #include"stdio.h"
    int main(void)
    {
    int x,i,sum;
    i=1;
    printf("Enter number : ");
    scanf("%d",&x);
    sum = x;
    while(i>0)
    {
    if(x>1)
    {
    sum = sum*(x-1);
    x=x-1;
    }
    else
    i=-1;
    }
    printf("sum = %d",sum);
    return 0;
    }

    นายศราวุฒิ คำเมือง 54-010116-2049-6

    ตอบลบ
  9. #include
    int main ()
    {
    int cost,wage;
    char fruit;
    printf("Apple 40 bath / kilogram\n");
    printf("Grape 200 bath / kilogram\n");
    printf("Orange 50 bath / kilogram\n");
    printf("Durian 60 bath / kilogram\n");
    printf("What fruit your want :");
    scanf("%s",&fruit);
    printf("how many fruit your want :");
    scanf("%d",&wage);
    if (wage<0)
    printf ("Error");
    else if (fruit=='a'||fruit=='A')
    printf ("cost is: %d",40*wage);
    else if (fruit=='g'||fruit=='G')
    printf ("cost is: %d",200*wage);
    else if (fruit=='o'||fruit=='O')
    printf ("cost is: %d",50*wage);
    else if (fruit=='d'||fruit=='D')
    printf ("cost is: %d",60*wage);
    else
    printf ("Error");

    }
    ผลเมื่อป้อน a,3
    จะได้ cost is :120
    (นายอาภากร กัณหา 54-010116-2062-3)

    ตอบลบ