Thuta Learning
ရှာဖွေရန်
IntermediateProgrammingbeginner

Arrays

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

Array ဆိုတာ type တူတဲ့ value များစွာကို name တစ်ခုအောက်မှာ သိမ်းတဲ့ collection ပါ။ Student score ၅ ခု၊ product price list, daily temperature list စတာတွေကို array နဲ့သိမ်းလို့ကောင်းပါတယ်။

c
#include <stdio.h>

int main() {
  int scores[] = {80, 75, 90, 60};
  int total = 0;

  for (int i = 0; i < 4; i++) {
    total += scores[i];
  }

  printf("Total: %d
", total);
  printf("Average: %.2f", total / 4.0);
  return 0;
}

scores[0] က ပထမ item ဖြစ်ပါတယ်။ Loop က index 0 မှ 3 အထိပတ်ပြီး score တွေကို total ထဲပေါင်းပါတယ်။

You should see
Total: 305 Average: 76.25

Info

Array index က 0 ကစပါတယ်။ Item 4 ခုရှိရင် last index က 3 ပါ။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • scores[4] ကိုဖတ်မိရင် array အပြင်ဘက် memory ကိုသွားဖတ်တာဖြစ်ပြီး unexpected result ဖြစ်နိုင်ပါတယ်။ C က ဒီအမှားကို အမြဲတမ်းကာကွယ်မပေးပါ။
Arrays | Thuta Learning