សូមស្វាគមន៍មកកាន់ប្លុកយើងខ្ញុំ តោះរៀន!!!

អ្នកចងក្រង

ខ្ញុំបាទឈ្មោះ ចាន់និត ជានិសិត្សទូរគមនាគមន៍​ និងអេឡិចត្រូនិច្ច។ សព្វថ្ងៃជាអ្នកស្រាវជ្រាវ ពី

  • បណ្ដាញអុីនធឺណេត
  • សរសេរcode, script
  • Machine Learning។

ទំនាក់ទំនង

https://rean-c.readthedocs.io

មេរៀនកូដCមូលដ្ថាន

5. Function នៅក្នុង C

5.1 សេចក្ដីផ្ដើម

Function គឺជាក្រុមនៃstatementដែលរួមគ្នា ក្នុងការធ្វើមុខងារអ្វីមួយ។ រាល់កម្មវិធីC មាន យ៉ាងតិចfunctionមួយ នោះគឺ main() ហើយកម្មវិធីអាចបង្កើតបន្ថែមfunctions ផ្សេងៗទៀត។

អ្នកអាចបែងចែកកូដរបស់អ្នកទៅជាfunctionតូចៗដាច់ដោយឡែក។ របៀបដែលអ្នកបែង ចែកកូដ ទៅជាfunctionខុសៗគ្នា គឺអាស្រ័យទៅលើអ្នក ប៉ុន្តែការបែងចែកនោះ គឺថាfunction និមួយៗអនុវត្តន៍មុខងារជាក់លាក់មួយ។

Note

ដោយសានៅក្នុងភាសខ្មែរ មានពាក្យខ្លះបកពីភាសាគេ ដែលភាកច្រើនជាពាក្យបច្ចេកកទេសនោះ គឺមានការពិបាកនឹងយល់ជាងនឹងយកពាក្យ (ឃ្លា) របស់គេមកប្រើ។ ដូច្នេះខ្ញុំសូមប្រើពាក្យនោះតែម្ដង ជាជាងបកប្រែ។ ហើយខ្ញុំក៏សូមអភ័យទោសផងបើមានការខុសឆ្គង! យើងខ្ញុំនឹងទទួលពាក្យទូន្មាន និងមតិយោបល់ពីប្រិយមិត្តអ្នកអាន តាមរយៈ amcnith@gmail.com

គំរូលំហាត់

Note

លំហាត់ទាំងនេះ យើងចែកជា…

១. ប្រើប្រាស់function (printf, scanf)

​​លំហាត់ទី១

ប្រើប្រាស់function: printf, scanf ដើម្បីបញ្ចូល និងបង្ហាញលើscreen។

Source
#include <stdio.h>

int main(){

    int a;
    printf("Enter value of a = ");
    scanf("%d", &a);

    printf("The value of a is %d\n\n", a);
}

Note

function printf(), scanf()ជាfunctionរបស់stdio.h។ ដូច្នេះយើងត្រូវinclude វាមក ទើបកូដដំណើរការត្រូវ។

Output
_images/ex01.png

Note

មជ្ឈដ្ថានខ្ញុំសរសេកូដ គឺUbuntuដើម្បីសរសេកូដ និងrun។

gcc ex01.c -o ex01

Commandនេះនឹងប្រើ GNU C compiler ដើម្បីcompile ex01.c និងoutput (-o)។ ជាលទ្ធផលកើតfile executable ex01។

Execute file: ./ex01 ជាការស្រេច។

បើអ្នកប្រើប្រាស់ windows អ្នកគួដំឡើងdev c ដើម្បីប្រើ ព្រោះវាមានភាពងាយស្រួល។

២. ប្រើប្រាស់function (printf, scanf) ជាមួយchar

​​លំហាត់ទី២

ប្រើប្រាស់function: printf, scanf ដើម្បីបញ្ចូលcharacter និងបង្ហាញលើscreen។

Source
#include <stdio.h>

int main(){

    char ch;
    printf("Enter a character c=");
    scanf("%c", &ch);
    printf("A character you entered is: %c\n", ch);
}

៣. រកដំលៃធំបំផុតរវាងពីរចំនួន

លំហាត់ទី៣

បញ្ចូលពីរចំនួន a និងb។ រកដំលៃធំបំផុតរវាងពីរចំនួននោះ?

Source
#include <stdio.h>

int main(int argc, char const *argv[])
{
    float a,b, max;
    
    printf("\n a="); scanf("%f", &a);
    printf("\n b="); scanf("%f", &b);

    max = a;

    if(max < b)
        max = b;

    printf("Maximum number of (%6.2f, %6.2f) is %6.2f\n", a, b, max);
    return 0;
}

៤. រកតម្លៃនៃF

លំហាត់ទី៤

  • សរសេកម្មវិធីអនុវត្តន៍កិច្ចការខាងក្រោម៖
    • ១.​ បញ្ចូលបីចំនួនពិត x, y, z នៅតម្លៃណាមួយពីkeyboard។

    • ២. គណនាដំលៃខាងក្រោម៖

      \[F = \frac{x + y + z}{x^{2} + y^{2} + 1} - | x - zcos(y)|\]
    • ៣. បង្ហាញដម្លៃទើបរកបាននៅលើscreen។

Source
#include <stdio.h>
#include <math.h>

int main(){
    float x, y, z, F=0;

    printf("Enter value of x, y, z: ");
    scanf("%f %f %f", &x, &y, &z);
    
    F =  (x+y+z)/(pow(x, 2) + pow(y, 2) + 1) - fabs(x-z*cos(y));

    printf("Value of F = %6.4f\n", F);
}

៥. ស្វែងរកចំនួនធំបំផុត និងតូចបំផុតចំនោម៣ចំនួន

លំហាត់ទី៥

  • ស្វែងរកចំនួនធំបំផុត និងតូចបំផុតចំនោម៣ចំនួន?
Source
#include <stdio.h>

int main(){

    float x, y, z, max, min;
    printf("\nEnter 3 number x, y, z: ");
    scanf("%f %f %f", &x, &y, &z);

    max = (x>y)?x:y;
    max = (max>z)?max:z;

    min = (x<y)?x:y;
    min = (min < z)?min:z;

    printf("Max number of %.2f, %.2f and %.2f is: %.2f", x, y, z, max);
    printf("\nMin number of %.2f, %.2f and %.2f is %.2f\n\n", x, y, z, min);
    return 0;
}
Output
Enter 3 number x, y, z: 4 5 0.99
Max number of 4.00, 5.00 and 0.99 is: 5.00
Min number of 4.00, 5.00 and 0.99 is 0.99

៦. បញ្ចូល និងវាយដំលៃចំនាត់ថ្នាក់សិស្ស

លំហាត់ទី៦

  • សរសេរកម្មវិធី បញ្ចូលពិន្ទុសិស្ស​ និងបង្ហាញលទ្ធផលលើscreen។
    • ខ្សោយណាស់ ពី ០​ទៅ ៣
    • ខ្សោយ ៤
    • មធ្យម ពី៥ ទៅ៦
    • បង្គួរ ពី ៧ទៅ ៨
    • ពូកែ ពី ៩ទៅ ១០
    • វាយខុស បើដំលៃខុសពីនឹង។
  • ផ្ដល់ជម្រើស វាយឡើងវិញ ឬបញ្ឈប់។
Source
#include <stdio.h>
#include <stdlib.h>


int main(){
    float score;

    tt : printf("Enter data:\n");
    printf("Enter the student's score: ");
    scanf("%f", &score);

    if((score >= 0) && (score <= 3)) printf("Very poor");
    else if( score == 4) printf("Poor");
    else if((score  >= 5) && (score <= 6)) printf("Faire");
    else if((score >=7) && (score <= 8)) printf("Pretty good");
    else if((score >=9) && (score <= 10)) printf("Very good");
    else printf("Entered wrong!!!\n");

    printf("\nContinue type 1 / stop type 0: ");
    scanf("%f", &score);
    if(score == 1) goto tt;
    else
        printf("Bye!\n");


}
Output
Enter data:
Enter the student's score: 5
Faire
Continue type 1 / stop type 0: 1
Enter data:
Enter the student's score: 9
Very good
Continue type 1 / stop type 0: 1
Enter data:
Enter the student's score: 4
Poor
Continue type 1 / stop type 0: 1
Enter data:
Enter the student's score: 2.5
Very poor
Continue type 1 / stop type 0: 0
Bye!

៧. បញ្ចូល និងបង្ហាញចំនាត់ថ្នាក់សិស្ស(switch, case)

លំហាត់ទី៧

  • សរសេរកម្មវិធី បញ្ចូលពិន្ទុសិស្ស​ និងបង្ហាញលទ្ធផលលើscreen។
    • ខ្សោយណាស់ ពី ០​ទៅ ៣
    • ខ្សោយ ៤
    • មធ្យម ពី៥ ទៅ៦
    • បង្គួរ ពី ៧ទៅ ៨
    • ពូកែ ពី ៩ទៅ ១០
    • វាយខុស បើដំលៃខុសពីនឹង។
  • ផ្ដល់ជម្រើស វាយឡើងវិញ ឬបញ្ឈប់។
Source
#include <stdio.h>

int main(){
    int score;

    tt: printf("Enter student's data\n");
    printf("Score: "); scanf("%d", &score);

    switch(score){
        case 0:
        case 1:
        case 2:
        case 3: printf("Very poor");
                break;
        case 4: printf("Poor");
                break;
        case 5:
        case 6: printf("Fair");
                break;
        case 7:
        case 8: printf("Good");
                break;
        case 9:
        case 10: printf("Very good");
                break;
        default: printf("Number not valid!!!");
    }

    printf("\nTo be continue, type 1 /stop, type 0: ");
    scanf("%d", &score);

    if(score == 1) goto tt;
    else printf("\nBye!\n");

    return 0;
}
Output
Enter student's data
Score: 5
Fair
To be continue, type 1 /stop, type 0: 1
Enter student's data
Score: 4
Poor
To be continue, type 1 /stop, type 0: 1
Enter student's data
Score: 8
Good
To be continue, type 1 /stop, type 0: 1
Enter student's data
Score: 9
Very good
To be continue, type 1 /stop, type 0: 0

Bye!

១៤៦. គណនាចម្ងាយ feet, inch (struct)

លំហាត់ទី១៤៦

ប្រើប្រាស់struct គណនាផលបូកចម្ងាយ​ ខ្នាតfeet និងinch។

Source
// Program to add two distaces which is in feet and inches

#include <stdio.h>

struct Distance{
    int feet;
    float inch;
}dist1, dist2, sum;

int main(){
    printf("1st distatce\n");
    printf("Enter feet: ");
    scanf("%d", &dist1.feet);
    printf("Enter inch: ");
    scanf("%f", &dist1.inch);

    printf("\n\n2nd distance\n");
    printf("Enter feet: ");
    scanf("%d", &dist2.feet);
    printf("Enter inch: ");
    scanf("%f", &dist2.inch);

    // adding feet
    sum.feet = dist1.feet + dist2.feet;
    // adding inch
    sum.inch = dist1.inch + dist2.inch;

    // Changing feet if inch is greater than 12.
    while(sum.inch >=12){
        ++sum.feet;
        sum.inch = sum.inch - 12;
    }

    printf("\nDistance 1: %d feet, %4.2f inch", dist1.feet, dist1.inch);
    printf("\nDistance 2: %d feet, %4.2f inch", dist2.feet, dist2.inch);
    printf("\nSum of distance = %d\'-%.1f\"\n", sum.feet, sum.inch);
}
Output
1st distatce
Enter feet: 3
Enter inch: 22

2nd distance
Enter feet: 5
Enter inch: 9

Distance 1: 3 feet, 22.00 inch
Distance 2: 5 feet, 9.00 inch
Sum of distance = 10'-7.0"

#.. literalinclude:: code/ex20-out.txt

១៤៧. បញ្ចូល និងបង្ហាញពត័មានសិស្សលើscreen (struct)

លំហាត់ទី១៤៧

សរសេកម្មវិធី ដោយប្រើប្រាស់strct បញ្ចូលពត័មានសិស្សម្នាក់(name, roll, marks) រួចបង្ហាញលើ screen។

Source
// Stroe information and display of a student using structure
/**
 * This program stores the information (name, roll and marks) of
 * a student and display it on the screen using structure
 * 
*/

#include <stdio.h>

struct Student{
    char name[50];
    int roll;
    float marks;
};

int main(int argc, char const *argv[])
{
    struct Student s;

    printf("Enter information:\n");
    printf("Enter name: ");
    scanf("%s", s.name);

    printf("Enter roll number: ");
    scanf("%d", &s.roll);

    printf("Enter marks: ");
    scanf("%f", &s.marks);

    printf("\n\nDisplaying information:\n");
    printf("Name: ");
    puts(s.name);
    printf("Roll number: %d\n", s.roll);
    printf("Marks: %.1f\n", s.marks);
    return 0;
}
Output
Enter information:
Enter name: Channith
Enter roll number: 1001
Enter marks: 9.5


Displaying information:
Name: Channith
Roll number: 1001
Marks: 9.5

១៤៨. បង្ហាញពត័មានសៀវភៅ (struct)

លំហាត់ទី១៤៨

សរសេរកម្មវិធីដោយប្រើstruct បង្ហាញពត័មានសៀវភៅ (title, author, subject, book_id)។

Source
#include <stdio.h>
#include <string.h>

struct Books{
    char title[50];
    char author[50];
    char subject[100];
    int book_id;
};

int main(){
    struct Books Book1; // Declare Book1 of type Book
    struct Books Book2; // Declare Book2 of type Book

    /* Book 1 specification */
    strcpy(Book1.title, "C Programming");
    strcpy(Book1.author, "Nuha Ali");
    strcpy(Book1.subject, "C Programming Tutorial");
    Book1.book_id = 34656763;

    /* Book 2 specification */
    strcpy(Book2.title, "Telecom Billing");
    strcpy(Book2.author, "Zara Ali");
    strcpy(Book2.subject, "Telecom Billing Tutorial");
    Book2.book_id = 45767443;

    /* Print Book1 ifno */
    printf("Book 1 title: %s\n", Book1.title);
    printf("Book 1 author: %s\n", Book1.title);
    printf("Book 1 subject: %s\n", Book1.subject);
    printf("Book 1 book_id: %d\n", Book1.book_id);

    /* Print Book2 info */
    printf("Book 2 title: %s\n", Book2.title);
    printf("Book 2 author: %s\n", Book2.author);
    printf("Book 2 subject: %s\n", Book2.subject);
    printf("Book 2 book_id: %d\n", Book2.book_id);

    return 0;
}
Output
Enter information:
Enter name: Channith
Enter roll number: 1001
Enter marks: 9.5


Displaying information:
Name: Channith
Roll number: 1001
Marks: 9.5

១៤៩. បូកចំនួនកំផ្លិច​ដោយប្រើfunction របស់struct

លំហាត់ទី១៤៩

សរសេរកម្មវិធី បញ្ចូលចំនួនកុំផ្លិចពីរ រួចហើយធ្វើប្រមានវិធីបូកដោយប្រើប្រាស់functionរបស់struct។

Source
#include <stdio.h>

typedef struct Complex_num{
    float real;
    float imag;
} complex;

complex add(complex n1, complex n2);

int main(){
    complex n1, n2, temp;

    printf("For 1st complex number\n");
    printf("Enter real and imaginary part respectively: \n");
    scanf("%f %f", &n1.real, &n1.imag);

    printf("\nFor 2nd complex number\n");
    printf("Enter real and imaginary part respectively:\n");
    scanf("%f %f", &n2.real, &n2.imag);

    temp = add(n1, n2);

    printf("Sum = %.2f + j%.2f\n", temp.real, temp.imag);

    return 0;
}

complex add(complex n1, complex n2){
    complex temp;

    temp.real = n1.real + n2.real;
    temp.imag = n1.imag + n2.imag;

    return(temp);
}
Output
For 1st complex number
Enter real and imaginary part respectively:
1 1

For 2nd complex number
Enter real and imaginary part respectively:
2 3
Sum = 3.00 + j4.00

១៥០. បញ្ចូល និងបង្ហាញពត័មានបុគ្គលិក

លំហាត់ទី១៥០

សរសេរកម្មវិធីភាសាC ក្នុងនោះ ត្រូវបញ្ចូលពត័មានពីបុគ្គលិក​ និងបង្ហាញពត័មានបុគ្គលិកនៅលើscreen ដោយប្រើstucture។ ពត័មានដូចជា id, name, designation, department, salary។

Source
#include <stdio.h>

struct employee{
    int e;
    char name[20];
    char designation[20];
    char depth[20];
    int sal;
};

int main(){
    struct employee a;
    printf("Enter employee details: \n");
    printf("-----------------------------------\n");
    printf("Enter employee-id: ");
    scanf("%d", &a.e);
    printf("Enter name  :");
    scanf("%s", a.name);
    printf("Enter designation : ");
    scanf("%s", a.designation);
    printf("Enter department: ");
    scanf("%s", a.depth);
    printf("Enter salary        : ");
    scanf("%d", &a.sal);

    printf("---------------------------------");
    printf("\nEmployee details:\n");
    printf("---------------------------------\n");
    printf("Name        : %s\n", a.name);
    printf("Designation : %s\n", a.designation);
    printf("Department  : %s\n", a.depth);
    printf("Salary      : %d\n", a.sal);

    return 0;
}
Output
Enter employee details:
-----------------------------------
Enter employee-id: 2313
Enter name  :Channith
Enter designation : Manager
Enter department: Production
Enter salary        : 30000
---------------------------------
Employee details:
---------------------------------
Name        : Channith
Designation : Manager
Department  : Production
Salary      : 30000

១៥១. រកប្រាក់ខែច្រើនបំផុតរបស់បុគ្គលិក

លំហាត់ទី១៥១

សរសេរកម្មវិធីC ដើម្បីបញ្ចលពត័មានបុគ្គលិក(employee number, name, salary) និងបង្ហាញអ្នក ដែលមានប្រាក់ខែខ្ពស់បំផុត ដោយប្រើប្រាស់structure។

Source
#include <stdio.h>

struct employee{
    int eno;
    char ename[20];
    int salary;
}emp[10];

int main(){
    int i, high, n;
    printf("/*How many employee info\nyou want to accepth : \n");
    printf("Enter limit: ");
    scanf("%d", &n);
    printf("\n---------------------------------\n");
    printf("Enter details for %d employees: \n",n);
    printf("---------------------------------\n");
    for(i=0; i<n; i++){
        printf("Employee Number: ");
        scanf("%d", &emp[i].eno);
        printf("Name           : ");
        scanf("%s", emp[i].ename);
        printf("Salary         : ");
        scanf("%d", &emp[i].salary);
        printf("-----------------------------\n");
    }

    high = emp[0].salary;
    for(i=0; i<n; i++){
        if(emp[i].salary > high)
            high = emp[i].salary;
    }
    
    printf("Highest salary employee details: ");
    printf("\n-------------------------------\n");
    printf("EMP_NO      NAME    SALARY\n");
    for(i=0; i<n; i++){
        if(emp[i].salary == high)
            printf("\n %d\t%s\t%d\n", emp[i].eno, emp[i].ename, emp[i].salary);

    }
    return 0;
}
Output
/*How many employee info
you want to accepth :
Enter limit: 2

---------------------------------
Enter details for 2 employees:
---------------------------------
Employee Number: 1
Name           : Dara
Salary         : 3900
-----------------------------
Employee Number: 2
Name           : Sana
Salary         : 3400
-----------------------------
Highest salary employee details:
-------------------------------
EMP_NO      NAME    SALARY

1   Dara    3900

១៥២. តម្រៀបឈ្មោះសិស្សតាមលំដាប់អក្ខរក្រម

សរសេរកម្មវិធីដើម្បីបញ្ចូលឈ្មោះ និងអាសយដ្ធានចំនួន n សិស្ស។ រួចតម្រៀបតាមលំដាប់អក្ខរក្រមabc។

Source

#include <stdio.h>
#include <string.h>

struct stud{
    char name[50];
    char add[50];
}s[100];

int main(){
    struct stud t;
    int i, j, n;
    
    printf("/*How many student records you want to enter?*/");
    printf("\n\nEnter limit : ");
    scanf("%d", &n);
    for(i=0; i<n; i++){
        printf("\\nEnter student- %d Details", i+1);
        printf("\n----------------------------------\n");
        printf("Enter name      : ");
        scanf("%s", s[i].name);
        printf("Address         : ");
        scanf("%s", s[i].add);
    }

    printf("\n\tData before rearrangement");
    printf("\n-----------------------------\n");
    printf("Student Name\tAddress\n");
    printf("--------------------------------\n");
    for(i=0; i<n; i++){
        printf("\n%-10s\t%3s\n", s[i].name, s[i].add);
    }

    for(i=0; i<n; i++){
        for(j=i+1; j<n; j++){
            if(strcmp(s[i].name, s[j].name) > 0){
                t = s[i];
                s[i] = s[j];
                s[j] = t;
            }
        }
    }
    printf("\n\tData after rearrangement");
    printf("\n-----------------------------\n");
    for(i=0; i<n; i++){
        printf("\n%-10s\t%3s\n", s[i].name, s[i].add);
    }

    return 0;
}

Output

/*How many student records you want to enter?*/

Enter limit : 2
\nEnter student- 1 Details
----------------------------------
Enter name      : Channith
Address         : Kompongspeu
\nEnter student- 2 Details
----------------------------------
Enter name      : Manich
Address         : Phnompenh

    Data before rearrangement
-----------------------------
Student Name        Address
--------------------------------

Channith    Kompongspeu

Manich      Phnompenh

    Data after rearrangement
-----------------------------

Channith    Kompongspeu

Manich      Phnompenh

១៥៣. គណានាប្រាក់បៀវត្សកម្មករ

សរសេរកម្មវិធី ដោយប្រើប្រាស់struct។ កម្មវិធីតម្រូវអោយបញ្ចូលឈ្មោះ ប្រាក់ខែ និងចំនួនថ្ងៃធ្វើការ។ រួចគណនាចំនួនទឹកប្រាក់របស់កម្មករនីីមួយៗ។

Source

#include <stdio.h>

typedef struct workers{
    char name[20];
    int wage;
    int wdays;
}worker;

int main(){
    worker a, b;
    printf("Enter details of first worker\n");
    printf("--------------------------------");
    printf("\nEnter worker name: ");
    scanf("%s", a.name);
    printf("Enter wage         : ");
    scanf("%d", &a.wage);
    printf("Enter work days:   : ");
    scanf("%d", &a.wdays);
    printf("--------------------------------");
    printf("\nEnter details of second worker");
    printf("\n-------------------------------");
    printf("\nEnter worker name :");
    scanf("%s", b.name);
    printf("Enter wage          :");
    scanf("%d", &b.wage);
    printf("enter work days     :");
    scanf("%d", &b.wdays);
    printf("-------------------------\n");

    int p1 = a.wage * a.wdays;
    int p2 = b.wage * b.wdays;

    printf("Name of first worker: %s\nPayment of first worker: %d\n", a.name, p1);
    printf("\n--------------------------\n");
    printf("Name of second worker: %s\nPayment of second worker: %d\n", b.name, p2);

    return 0;
}

Output

Enter details of first worker
--------------------------------
Enter worker name: Dara
Enter wage         : 3000
Enter work days:   : 6
--------------------------------
Enter details of second worker
-------------------------------
Enter worker name :Sokha
Enter wage          :4500
enter work days     :5
-------------------------
Name of first worker: Dara
Payment of first worker: 18000

--------------------------
Name of second worker: Sokha
Payment of second worker: 22500

១៥៤. គណនាពិន្ទុរត់របស់កីឡាករ

សរសេរកម្មវិធី ដោយប្រើប្រាស់structure។ តម្រូវអោយអ្នកប្រើបញ្ចូលឈ្មោះ និងពិន្ទុកីឡាករនិយមួយ រួចបូកសរុបពិន្ទុរបស់កីឡាកររបស់ក្រុមនោះ?

Source

#include <stdio.h>

struct player{
    char name[50];
    int runs;
};

int main(int argc, char const *argv[])
{
    int i, s=0;
    struct player a[11]; // a[11] - number of players
    printf("Enter name of player runs scored\n");
    printf("===================================\n\t");
    for(i=0; i<11; i++){
        scanf("%s", a[i].name);
        scanf("%d", &a[i].runs);
        printf("\t");
    }

    for(i=0; i<11; i++)
        s = s + a[i].runs;
    printf("\n==================================\n");
    printf("Total runs scored by Team: %d\n", s);

    return 0;
}

Output

Enter name of player runs scored
===================================
    Dara 23
    Chantha 45
    Kilu    56
    Cluni   32
    Calay   23
    Kuli    23
    Sulo    28
    Laya    81
    Dany    89
    Kuma    45
    Hany    22

==================================
Total runs scored by Team: 467

Test - Lab

Lab - 1

សរសេរកម្មវិធីអនុវត្តន៍កិច្ចការខាងក្រោម៖

  1. បញ្ចូលarrayមួយមានចំនួនN ជាចំនួនគត់ (0 < N < 10 ) បញចូលពីkeyboard។ បង្ហាញចំនួនទាំងនោះលើscreen នូវចំនួនទើបតែបានបញ្ចូលនេះ។

  2. បញ្ចលលេខ x (x > 0) ណាមួយពីkeyboard។

    1. ប្រសិនបើ x <= N: គណនាផលបូកជាមធ្យម នៃចំនួនxលេខតម្បូង។
    2. ប្រសិនបើ x > N: គណនាផលបូកសរុបនៃចំនួនទាំងនោះ?

បង្ហាញចំនួនទាំងនោះលើscreen។ (ត្រូវត្រួតពិនិត្យលក្ខខណ្ឌinput)

Solution
#include <stdio.h>

int main(){
    int i, N, x, A[10];
    int sum=0;
    float m=0;

    do{
        printf("Enter value of N = ");
        scanf("%d", &N);
    }while(N<=0 || N>=10);

    printf("\nEnter element of array A[%d]\n", N);
    for(i=0; i<N; i++){
        printf("A[%d]: ", i+1);
        scanf("%d", &A[i]);
    }
    printf("Array of A is : ");
    for(i=0; i<N; i++) printf("%d\t", A[i]);

    do{
        printf("\nEnter value of x = ");
        scanf("%d", &x);
    }while(x <= 0);
    if(x <= N){
        for(i=0; i<N; i++)
            m = m + A[i];
        m = m/N;
        printf("Medium of array A is : %.2f\n", m);
    }else{
        for(i=0; i<N; i++)         sum = sum + A[i];
        printf("Sum of array A is : %d\n", sum);
    }
    return 0;
}
Output
Enter value of N = 5

Enter element of array A[5]
A[1]: 2
A[2]: 3
A[3]: 4
A[4]: 5
A[5]: 5
Array of A is : 2   3       4       5       5
Enter value of x = 8
Sum of array A is : 19

Lab - 2

សរសេរកម្មវិធីអនុវត្តន៍កិច្ចការខាងក្រោម៖

  1. បញ្ចូលarrayមួយ ជាលេខចំនួនគត់ណាក៏ដោយពីkeyboard។ ការបញ្ចូលត្រូវបានបញ្ឈប់នៅពេលដែលចំនួនធាតុមាន៧ ឫបញ្ចូលលេខ០ ដែលលេខ០មិនមែនជាធាតុក្នុងarray។
    1. បង្ហាញចំនួនទាំងនោះលើscreenទម្រង់ជាសេរី។
    2. តម្រៀបវាជាលំដាប់កើន និង​ បង្ហាញចំនួនទាំងនោះលើscreen។
Solution
#include <stdio.h>

int main(){
    int i, j, n=0, tmp, A[7];
    for(i=0; i<7; i++){
        printf("A[%d] : ", i+1);
        scanf("%d", &tmp);
        if(tmp == 0) break;
        else A[i] = tmp;
        n++;
    }
    if(n==0) printf("\nThe array A has no element!!!");
    else{
        printf("\nThe array A is : ");
        for(i=0; i<n; i++) printf("%d\t", A[i]);
        for(i=0; i<n; i++)
            for(j=i+1; j<n; j++){
                if(A[i] > A[j]){
                    tmp = A[i];
                    A[i] =  A[j];
                    A[j] = tmp;
                }
            }
        printf("\nArray after re-arange (increase): ");
        for(i=0; i<n; i++) printf("%d\t", A[i]);
    }
    printf("\n");
    return 0;
}
Output
A[1] : 3
A[2] : 5
A[3] : 1
A[4] : 9
A[5] : 4
A[6] : 0

The array A is : 3  5       1       9       4
Array after re-arange (increase): 1    3    4       5       9

Lab - 3

សរសេរកម្មវិធីអនុវត្តន៍កិច្ចការខាងក្រោម៖

  1. បញ្ចលលេខn ជាចំនួនគត់ ពីkeyboard(0<n<10)។
  2. បញ្ចូលarrayមួយមានចំនួនNធាតុ ជាចំនួនពិត។ បង្ហាញចំនួនទាំងនោះលើscreenទម្រង់ជាសេរី។
  3. ស្វែងរកចំនួនធំបំផុតនៅក្នុងarrayនោះ និងរាប់ចំនួនលេខដែលធំនៅក្នុងarray។ បង្ហាញចំនួនទាំងនោះលើscreen។
Solution
#include <stdio.h>

int main()
{
    int i, N;
    int j=0;
    float max;
    float A[10];
    do{
        printf("Enter N = ");
        scanf("%d", &N);
    }while(N<= 0 || N>=10);
    printf("\nEnter Array A: \n");
    for(i=0; i<N; i++){
        printf("A[%d] \t: ", i+1);
        scanf("%f", &A[i]);
    }
    printf("The array of A is : ");
    for(i=0; i<N; i++) printf("%.2f\t", A[i]);
    max = A[0];
    for(i=0; i<N; i++){
        if(max == A[i])
            j++;
        if(max < A[i])
            {max = A[i]; j=1;}
    }
    printf("\nThe maximum of Array is %.2f", max);
    printf("\nThe number equal to max are : %d", j);
    printf("\n\n");
    return 0;
}
Output
Enter N = 9

Enter Array A:
A[1]        : 11
A[2]        : 45
A[3]        : 0.34
A[4]        : 2
A[5]        : -34
A[6]        : 9
A[7]        : 45
A[8]        : 45
A[9]        : 6.7
The array of A is : 11.00   45.00   0.34    2.00    -34.00  9.00    45.00   45.00   6.70
The maximum of Array is 45.00
The number equal to max are : 3

Indices and tables