เขียนโปรแกรมภาษาซี รับค่าตัวเลข

ในบทนี้ คุณจะได้เรียนรู้การเขียนโปรแกรมเพื่อหาค่าน้อยที่สุดและมากที่สุดในภาษา C และ C++ เราจะเริ่มต้นจากการเปรียบเท่ียบจากตัวเลขสองตัว จากนั้นเป็นการค้นหาค่าที่น้อยและมากที่สุดจากเซ็ตของตัวเลขที่เก็บในอาเรย์ นี่เป็นเนื้อหาในบทนี้

  • การหาค่าน้อยและมากที่สุด
  • การหาค่าน้อยและมากที่สุดในอาเรย์

การหาค่าน้อยและมากที่สุด

การหาค่าน้อยที่สุดและมากที่สุดสามารถใช้ตัวดำเนินการเปรียบเทียบในภาษา C และภาษา C++ สำหรับเปรียบเทียบค่า และตัวดำเนินการที่เราจะใช้ในบทเรียนนี้คือตัวดำเนินการ

#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int b = 2;
    if (a < b) {
        cout << "a less than b" << endl;
    } else {
        cout << "a is not less than b\n" << endl;
    }
    return 0;
}
2,
#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int b = 2;
    if (a < b) {
        cout << "a less than b" << endl;
    } else {
        cout << "a is not less than b\n" << endl;
    }
    return 0;
}
3 และ
#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int b = 2;
    if (a < b) {
        cout << "a less than b" << endl;
    } else {
        cout << "a is not less than b\n" << endl;
    }
    return 0;
}
4 ซึ่งใช้สำหรับเปรียบเทียบความน้อยกว่า มากกว่า และเท่ากัน ตามลำดับ มาดูตัวอย่างการใช้งาน

finding_min.c

#include <stdio.h>

int main()
{
    int a = 1;
    int b = 2;
    if (a < b) {
        printf("a less than b\n");
    } else {
        printf("a is not less than b\n");
    }
    return 0;
}

finding_min.cpp

#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int b = 2;
    if (a < b) {
        cout << "a less than b" << endl;
    } else {
        cout << "a is not less than b\n" << endl;
    }
    return 0;
}

นี่เป็นผลลัพธ์การทำงานของโปรแกรม

a less than b

นี่จะแสดงข้อความที่บอกว่าค่าในตัวแปร

#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int b = 2;
    if (a < b) {
        cout << "a less than b" << endl;
    } else {
        cout << "a is not less than b\n" << endl;
    }
    return 0;
}
5 น้อยกว่า
#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int b = 2;
    if (a < b) {
        cout << "a less than b" << endl;
    } else {
        cout << "a is not less than b\n" << endl;
    }
    return 0;
}
6 และนั่นก็ชัดเจน เนื่องจากตัวดำเนินการเปรียบเทียบน้อยกว่า (
#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int b = 2;
    if (a < b) {
        cout << "a less than b" << endl;
    } else {
        cout << "a is not less than b\n" << endl;
    }
    return 0;
}
2) จะส่งค่ากลับเป็นจริง ถ้าหากค่าทางซ้ายน้อยกว่าค่าทางขวา นั่นทำให้เงื่อนไขเป็นจริง และโปรแกรมทำงานในบล็อคของคำสั่ง if

แต่ถ้าหากเราเปลี่ยนค่าในตัวแปร

#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int b = 2;
    if (a < b) {
        cout << "a less than b" << endl;
    } else {
        cout << "a is not less than b\n" << endl;
    }
    return 0;
}
5 ให้มากกว่าหรือเท่ากับตัวแปร
#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int b = 2;
    if (a < b) {
        cout << "a less than b" << endl;
    } else {
        cout << "a is not less than b\n" << endl;
    }
    return 0;
}
6 แน่นอนมันจะทำงานในบล็อคของคำสั่ง else แต่เมื่อมันทำ เราจะไม่ทราบว่าตัวแปร
#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int b = 2;
    if (a < b) {
        cout << "a less than b" << endl;
    } else {
        cout << "a is not less than b\n" << endl;
    }
    return 0;
}
5 นั้นมากกว่าหรือเท่ากับตัวแปร
#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int b = 2;
    if (a < b) {
        cout << "a less than b" << endl;
    } else {
        cout << "a is not less than b\n" << endl;
    }
    return 0;
}
6 เนื่องจากการทำงานของโปรแกรมนี้มีเพียงสองทางเลือกคือ น้อยกว่าหรือในทางตรงกันข้าม ซึ่งสามารถเป็นได้ทั้งมากกว่าหรือเท่ากัน

และเพื่อแก้ไขปัญหานี้ เราจะต้องทำให้โปรแกรมสามารถตรวจสอบค่าได้แบบสามทางเลือก นั่นคือว่าค่าในตัวแปรทั้งสองนั้นน้อยกว่า มากกว่า หรือเท่ากันนั่นเอง และในตัวอย่างนี้ เป็นโปรแกรมที่ทำงานในรูปแบบดังกล่าว

compare_values.c

#include <stdio.h>

int main()
{
    int a = 5;
    int b = 2;
    if (a < b) {
        printf("a less than b\n");
    } else if (a > b) {
        printf("a greater than b\n");
    } else {
        printf("a is equal to b\n");
    }
    return 0;
}

compare_values.cpp

#include <iostream>

using namespace std;

int main()
{
    int a = 5;
    int b = 2;
    if (a < b) {
        cout << "a less than b" << endl;
    } else if (a > b) {
        cout << "a greater than b" << endl;
    } else {
        cout << "a is equal to b" << endl;
    }
    return 0;
}

นี่เป็นผลลัพธ์การทำงานของโปรแกรม

a greater than b

ในตัวอย่างนี้ เราได้เพิ่มเงื่อนไขสำหรับตรวจสอบว่าค่าในตัวแปร

#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int b = 2;
    if (a < b) {
        cout << "a less than b" << endl;
    } else {
        cout << "a is not less than b\n" << endl;
    }
    return 0;
}
5 มากกว่าตัวแปร
#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int b = 2;
    if (a < b) {
        cout << "a less than b" << endl;
    } else {
        cout << "a is not less than b\n" << endl;
    }
    return 0;
}
6 หรือไม่ด้วยคำสั่ง else if แต่ถ้าหากการตรวจสอบในรอบนี้ยังไม่เป็นจริง โปรแกรมทำงานในบล็อคของคำสั่ง else ซึ่งหมายความว่าค่าในตัวแปรทั้งสองเท่ากัน

และนี่เป็นโปรแกรมเพื่อหาว่าค่าของสองตัวแปรน้อยกว่า มากกว่า หรือเท่ากัน ซึ่งเป็นการทำงานที่สามารถรับมือได้กับทุกความเป็นไปได้ของข้อมูล จะเห็นว่ามันเป็นโปรแกรมที่เรียบง่ายและพื้นฐานมาก เนื่องจากการเปรียบเทียบนั้นเกิดขึ้นกับตัวเลขเพียงแค่สองค่า

แต่ในชีวิตจริงข้อมูลอาจไม่ได้มีเพียงแค่สองค่า ยกตัวอย่างเช่น เรามีคะแนนของนักเรียนจำนวน 10 คน หรือบริษัทแห่งหนึ่งมีรายงานผลกำไรสรุปในแต่ละเดือน และเราอาจต้องการหาค่าที่น้อยที่สุดและมากที่สุดจากเซ็ตของข้อมูลเหล่านี้

// Score of ten students
int scores[] = {16, 3, 96, 49, 92, 41, 38, 24, 13, 55};

// Profit of the company from January - June (millions)
int profits[] = {61, 123, 68, 114, 178, 102};

เนื่องจากในกรณีนี้ ข้อมูลที่ต้องการนำมาหาค่าที่น้อยที่สุดและมากที่สุดนั้นมีหลายค่า ซึ่งถือว่าเป็นกลุ่มของข้อมูล ซึ่งในภาษา C และภาษา C++ นั่นข้อมูลในรูปแบบนี้สามารถเก็บได้อยู่ในรูปแบบของอาเรย์ และเราสามารถเขียนโปรแกรมเพื่อค้นหาค่าจากในอาเรย์

การหาค่าน้อยและมากที่สุดในอาเรย์

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

finding_min_max.c

#include <stdio.h>

int main()
{
    int scores[] = {16, 3, 96, 49, 92, 41, 38, 24, 13, 55};
    int n = sizeof(scores) / sizeof(int);

    // find minimum score
    int min = scores[0];
    for (int i = 1; i < n; i++) {
        if (scores[i] < min) {
            min = scores[i];
        }
    }

    // find maximum score
    int max = scores[0];
    for (int i = 1; i < n; i++) {
        if (scores[i] > max) {
            max = scores[i];
        }
    }

    printf("Min: %d\n", min);
    printf("Max: %d\n", max);
    return 0;
}

finding_min_max.cpp

#include <iostream>

using namespace std;

int main()
{
    int scores[] = {16, 3, 96, 49, 92, 41, 38, 24, 13, 55};
    int n = sizeof(scores) / sizeof(int);

    // find minimum score
    int min = scores[0];
    for (int i = 1; i < n; i++) {
        if (scores[i] < min) {
            min = scores[i];
        }
    }

    // find maximum score
    int max = scores[0];
    for (int i = 1; i < n; i++) {
        if (scores[i] > max) {
            max = scores[i];
        }
    }

    cout << "Min: " << min << endl;
    cout << "Max: " << max << endl;
    return 0;
}

นี่เป็นผลลัพธ์การทำงานของโปรแกรม

Min: 3
Max: 96

ในตัวอย่างนี้ เรามีอาเรย์

a less than b
4 ที่เก็บคะแนนของนักเรียน 10 คน คุณสามารถเขียนโปรแกรมเพื่อรับค่าจากทางคีย์บอร์ดมาเก็บในอาเรย์ได้ แต่เรากำหนดค่าโดยตรงเพื่อความเรียบง่าย จากนั้นเป็นการนับจำนวนสมาชิกทั้งหมดในอาเรย์เก็บในตัวแปร
a less than b
5 เพื่อใช้เป็นเงื่อนไขในการวนค่าในอาเรย์ด้วยคำสั่ง for loop

#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int b = 2;
    if (a < b) {
        cout << "a less than b" << endl;
    } else {
        cout << "a is not less than b\n" << endl;
    }
    return 0;
}
0

นี่เป็นการหาค่าน้อยที่สุดในอาเรย์ ในตอนแรกกำหนดค่าที่น้อยที่สุดเริ่มต้นเป็นค่าแรกในอาเรย์ จากนั้นวนรอบตั้งแต่ตำแหน่งที่สองเป็นต้นไปและนำมาเปรียบเทียบกับค่าในตัวแปร

a less than b
6 ถ้าหากมันน้อยกว่าค่านี้ จะเปลี่ยนค่าที่น้อยที่สุดเป็นค่าดังกล่าวแทน และเมื่อทำแบบนี้ไปจนครบจะได้ค่าที่น้อยสุดเก็บในตัวแปร
a less than b
6

#include <iostream>

using namespace std;

int main()
{
    int a = 1;
    int b = 2;
    if (a < b) {
        cout << "a less than b" << endl;
    } else {
        cout << "a is not less than b\n" << endl;
    }
    return 0;
}
1

ในการค้นหาค่ามากที่สุดก็ทำเช่นเดียวกัน แต่เปลี่ยนการเปรียบเทียบเป็นมากกว่ากับตัวแปร

a less than b
8 แทน ถ้าหากค่าที่นำมาเปรียบเทียบมากกว่าค่าในตัวแปร
a less than b
8 เราทำการเปลี่ยนแปลงค่านี้ให้เป็นค่าที่มากที่สุดแทน

สำหรับตัวอย่างนี้แสดงให้เห็นว่าในกรณีที่ข้อมูลที่ต้องการนำมาหามีเป็นจำนวนมาก เราสามารถเก็บข้อมูลเหล่านั้นไว้ในอาเรย์และเขียนโปรแกรมเพื่อตรวจสอบค่าในอาเรย์ได้ ซึ่งในการเขียนโปรแกรมจริงแล้วข้อมูลที่ต้องการนำมาหามักจะมีหลายค่า

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