Lanka Developers Community

    Lanka Developers

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Shop

    C Programming මුල සිට ඉගෙනගනිමු (part 13 - 2D-arrays)

    Blogs
    c programming
    2
    2
    368
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Kalana
      Kalana last edited by

      සුබ දවසක් වේවා ඔයාලට. අද මන් මේ ලිපිය මඟින් කතා කිරීමට බලාපොරොත්තු වන්නේ C Programming වල එන 2D-arrays ගැනයි. 2D කිව්වාම ඔයාලා දන්නවා මාන 2ක් පමණක් ඇති වස්තූන් වලට තමා 2D කියලා කියන්නේ. එතකොට මේකෙදි බැලුවොත් arrays හා 2D-arrays වල වෙනස වෙන්නේ arrays වල values output කරන්නේ හරහට 2D-arrays වල values output හරහට හා
      පහලට යන දෙපැත්තටමය.

      උදාහරණ -:

      array = 1 2 3 4 5 6 7 8 9 (දත්ත ගලන්නේ එක් දිශාවකට පමණි)
      
      2D-array = 1 2 3   (දත්ත දිශාවන් දෙකකට ගලයි)
      	   4 5 6
                 7 8 9
      

      2D-arrays බොහෝ විට යොදාගන්නා අවස්තාවලින් කිහිපයක් වන්නේ ගණිත විශයේ එන න්‍යාස නිරූපනය හා එහි ගැටළු විසදීම, වගු නිර්මාණය කිරීම වගේ දේවල් වලට හා තවත් සංකීර්ණ ගැටළු සඳහාත් මෙය යොදා ගනී.

      අපි Program එකක් ලියන්න කලින් 2D-arrays වල එන වැදගත් කරුණු දැන ගනිමු.

      • array එකක සේම 2D-array එකකත් data type වර්ග variable data type වර්ග වලට සමාන වේ. (int, float, char, short,...,etc).
      • 2D-array එකක මූලික ආකෘතිය වන්නේ <data type> <array name>[][]. (උදා -: int number[][], float marks[][], char words[][],....,etc)
      <data type> <array name>[පේලි හෙවත් raws ගණන][තීරු හෙවත් column ගණන]
      
      • 2D-array එකක් භාවිතා කරන සම්මත ආකාර වන්නේ int numbers[2][5] = {{1, 2, 3, 4 ,5},{6, 7, 8, 9, 10}}; හෝ
        int numbers[2][5] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; අකාරවලින් වේ.
      • 2D-array එකක් int numbers[2][5] ලෙස භාවිතා කලොත් එයින් අදහස් වන්නේ එය තුල 0, 1, 2 ලෙස පේලි 3කුත්, 0, 1, 2, 3, 4, 5 ලෙස තීරු 5කුත් ඇති බවයි. මන් එය උදාහරණයක් මඟින් පැහැදිලි කරන්නම්.
      int numbers[2][5] = {
          {1, 2, 3, 4, 5}, 
          {6, 7, 8, 9, 10}
      };
      

      ඔබට පෙනෙනවා ඇති ඉහත සටහනේ පේලි 2ක් හා තීරු 5ක් ඇති බව. අපි දැන් එහි එක් එක් ඉලක්කමේ ඛණ්ඩාංක ගනිමු.

      int numbers[0][0] = 1
      int numbers[0][1] = 2
      int numbers[0][2] = 3
      int numbers[0][3] = 4
      int numbers[0][4] = 5
      int numbers[0][5] // අපට 2D-array වලදී සෑම විටම භාවිතා කල හැක්කේ numbers[n - 1][n - 1] දක්වා පමණි. එමනිසා අපට numbers[0][5] යන්නට සංඛ්‍යාවක් ආදේශ කිරීම සදහා භාවිතා කල නොහැක.
      
      int numbers[1][0] = 6
      int numbers[1][1] = 7
      int numbers[1][2] = 8
      int numbers[1][3] = 9
      int numbers[1][4] = 10
      int numbers[1][5] // අපට 2D-array වලදී සෑම විටම භාවිතා කල හැක්කේ numbers[n - 1][n - 1] දක්වා පමණි. එමනිසා අපට numbers[1][5] යන්නට සංඛ්‍යාවක් ආදේශ කිරීම සදහා භාවිතා කල නොහැක.
      
      int numbers[2][0] //අපට 2D-array වලදී සෑම විටම භාවිතා කල හැක්කේ numbers[n - 1][n - 1] දක්වා පමණි. එමනිසා අපට numbers[2][0] යන්නට සංඛ්‍යාවක් ආදේශ කිරීම සදහා භාවිතා කල නොහැක.
      

      මෙය සරලව දැක්වුවහොත්

         [0][1][2][3][4]
      [0] 1  2  3  4  5
      [1] 6  7  8  9  10
      

      2D-array එකක් නිවැරදිව ලිවිය හැකි ආකාර අපි බලමු.

      • int abc[2][2] = {1, 2, 3 ,4 }
      • int abc[][2] = {1, 2, 3 ,4 }
      • int abc[2][2] = {{1, 2}, {3 ,4}}

      2D-array එකක් මෙසේ ලියා තිබුනොත් වැරදි අවස්තාවක් ලෙස සලකයි.

      • int abc[][] = {1, 2, 3 ,4 } //තීරු ගණන හා පේලි ගණන දක්වා නැත.
      • int abc[2][] = {1, 2, 3 ,4 } //තීරු ගණන දක්වා නැත.

      අපි දැන් මෙම දැනුම භාවිතා කර සරල C Program එකක් ලියමු.

      Question -: Output the following matrix using 2D-array.

      1 2 
      3 4
      5 6
      7 8
      

      අපට මෙහි කිරීමට ඇත්තේ ඉහත සඳහන් න්‍යාසය C Program එකක් මඟින් නිරූපණය කිරීමටයි.

      • මෙම දී ඇති න්‍යාසයේ පේලි 4ක් හා තීරු 2 ඇති බැවින් අප අපගේ 2D-array එක සාදාගත යුත්තේ int numbers[4][2] ලෙසය.
      #include <stdio.h>
      
      int main()
      {
          int numbers[4][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}; //අපගේ 2D-array එක
          int raw; // අපගේ පේලි 4 නිරූපණය කරන variable එක
          int column; // අපගේ තීරු 2 නිරූපණය කරන variable එක
          
          for(raw = 0; raw < 4; raw++) // අපගේ පේලි 4 count කරන for loop එක
          {
              for(column = 0 ; column < 2 ; column++) // අපගේ තීරු 2 count කරන for loop එක
              {
                  printf("%d ", numbers[raw][column]);
              }
              printf("\n");
          }
      
          return 0;
      }
      

      Output -:

      1 2 
      3 4 
      5 6 
      7 8 
      

      අපි ඉහත Program එක ක්‍රියා කරන ආකාරය පියවර ආකාරයෙන් බලමු.

      පලමු පියවර

      for(raw = 0; raw < 4; raw++) // raw = 0 බව දී ඇති නිසා 0 සෑම විටම 4 ට වඩා කුඩා නිසාත් loop එක ඇතුලත codes run වේ.
      {
          for(column = 0 ; column < 2 ; column++) // column = 0 බව දී ඇති නිසා 0 සෑම විටම 2ට වඩා කුඩා නිසාත් loop එක ඇතුලත codes run වේ.
          {
              printf("%d ", numbers[raw][column]); // raw = 0 හා column = 0 බැවින් [0][0] ස්තානයේ ඇති ඉලක්කම output වේ. [0][0] ස්තානයේ ඇති ඉලක්කම වන්නේ 1යි.
          }
          printf("\n");
      }
      

      දෙවන පියවර

      for(column = 1 ; column < 2 ; column++) // column++ මඟින් column වල අගය එකකින් වැඩිවන නිසා දැන් column වල අගය වන්නේ 1 වේ. 1 < 2 නිසා loop එක ඇතුලත codes run වේ.
      {
          printf("%d ", numbers[raw][column]); // raw = 0 හා column = 1 බැවින් [0][1] ස්තානයේ ඇති ඉලක්කම output වේ. [0][1] ස්තානයේ ඇති ඉලක්කම වන්නේ 2යි.
      }
      

      තුන්වන පියවර

      //column++ මඟින් column වල අගය එකකින් වැඩිවන නිසා දැන් column වල අගය වන්නේ 2 වේ. 2 < 2 අසත්‍ය වන නිසා එම loop එකෙන් එලියට පැමිනේ.
      
      for(raw = 0; raw < 4; raw++) // raw++ මගින්  raw වල අගය එකකින් වැඩිවන නිසා දැන් raw වල අගය වන්නේ 1 වේ. 1 < 4 නිසා loop එක ඇතුලත codes run වේ.
      {
          for(column = 0 ; column < 2 ; column++) // column = 0 බව දී ඇති නිසා 0 සෑම විටම 2ට වඩා කුඩා නිසාත් loop එක ඇතුලත codes run වේ.
          {
              printf("%d ", numbers[raw][column]); // raw = 1 හා column = 0 බැවින් [1][0] ස්තානයේ ඇති ඉලක්කම output වේ. [1][0] ස්තානයේ ඇති ඉලක්කම වන්නේ 3යි.
          }
          printf("\n");
      }
      

      මෙසේ දිගටම මේ ආකරයෙන් පියවර සිදු වෙමින් අපට අවශ්‍ය Output එක මෙම Program එකෙන් ලබා දීම සිදු කරයි.

      මන් ඔයාලට ප්‍රශ්නෙකුත් දෙන්නම් කරන්න මේකෙන්. උත්තරෙත් දෙන්නම් ඒ එක්ක.

      Question 1 -: Write a C program to find total of between these two matrix.

      3  7  8  1       2  5  1  9
      2  4  6  5   +   3  8  4  6   =    ?
      5  1  4  2       1  3  3  2
      

      Answer -:

      #include <stdio.h>
      
      int main()
      {
          int numbers1[3][4] = {{3, 7, 8, 1}, {2, 4, 6, 5}, {5, 1, 4, 2}};
          int numbers2[3][4] = {{2, 5, 1, 9}, {3, 8, 4, 6}, {1, 3, 3, 2}};
          int total[3][4];
          int raw;
          int column;
          
          for(raw = 0; raw < 3; raw++) 
          {
              for(column = 0 ; column < 4 ; column++) 
              {
                  total[raw][column] = numbers1[raw][column] + numbers2[raw][column];
              }
          }
          
          for(raw = 0; raw < 3; raw++) 
          {
              for(column = 0 ; column < 4 ; column++) 
              {
                  printf("%4d", total[raw][column]); // %4d යොදාගත්තේ output වන සංඛ්‍යා වල නිශ්චිත දුරක් පවත්වා ගැනීමටයි.
              }
              printf("\n");
          }
      
          return 0;
      }
      

      Output -:

         5  12   9  10
         5  12  10  11
         6   4   7   4
      

      • C programm එකක් windows වල run කරන විදිහ --> https://bit.ly/2O6rLXR

      ඔබට අවශ්‍යනම් ඔබේ බ්‍රව්සර් එක හරහා online C programms run කරන්න පුලුවන්. එහෙම කරන්න පුලුවන් ලින්ක්ස් මන් පහතින් දාන්නම්

      • https://www.onlinegdb.com/online_c_compiler
      • https://www.tutorialspoint.com/compile_c_online.php
      • https://repl.it/languages/c

      සරලව මුල ඉදන් C programming ඉගෙන ගන්න පුලුවන් හොදම site කිහිපයක් මන් දාන්නම්

      • https://www.geeksforgeeks.org/c-programming-language/
      • https://www.tutorialspoint.com/cprogramming/index.htm
      • https://beginnersbook.com/2014/01/c-tutorial-for-beginners-with-examples/

      web url කෙටි කරන්න lankadevelopers ලා හදපු එයාලගෙම සයිට් එකක් තියෙනවා. -> https://link.lankadevelopers.com/

      මගේ කලින් ලිපි

      • C Programming මුල සිට ඉගෙනගනිමු(part 1 - Introduction) -: https://link.lankadevelopers.com/4WpH
      • C Programming මුල සිට ඉගෙනගනිමු (part 2 - Variables) -: https://link.lankadevelopers.com/mXio
      • C Programming මුල සිට ඉගෙනගනිමු (part 3 - Operators) -: https://link.lankadevelopers.com/SHNt
      • C Programming මුල සිට ඉගෙනගනිමු (part 4 - Input & Output functions) -: https://link.lankadevelopers.com/2MNku
      • C Programming මුල සිට ඉගෙනගනිමු (part 5 - create simple applications) -: https://link.lankadevelopers.com/KUF6
      • C Programming මුල සිට ඉගෙනගනිමු (part 6 - Decision making(if-else statement - part 1)) -: https://link.lankadevelopers.com/8Xe71
      • C Programming මුල සිට ඉගෙනගනිමු (part 7 - Format Specifiers in C) -: https://link.lankadevelopers.com/761PT
      • C Programming මුල සිට ඉගෙනගනිමු (part 8 - Switch Statement) -: https://link.lankadevelopers.com/7jncK
      • C Programming මුල සිට ඉගෙනගනිමු (part 9 - While loop) -: https://link.lankadevelopers.com/4TBV5
      • C Programming මුල සිට ඉගෙනගනිමු (part 10 - do-while loop) -: https://link.lankadevelopers.com/4WcNd
        C Programming මුල සිට ඉගෙනගනිමු (part 11 - for loop) -: https://link.lankadevelopers.com/8utoa
        C Programming මුල සිට ඉගෙනගනිමු (part 12 - arrays) -: https://link.lankadevelopers.com/46cyf

      මන් ඊළග ලිපියෙන් කියලා දෙන්නම් Strings ගැන . මගේ ලිපි වල අඩුපාඩු තියෙනවනම් දන්නේ නැති දේවල් තියෙනවනම් පහලින් කමෙන්ට් එකක් දාන්න.
      තව ලිපියකින් හම්බෙමු ජය වේවා.

      1 Reply Last reply Reply Quote 0
      • root
        root Linux Help last edited by

        Fatta bro. Thanks

        1 Reply Last reply Reply Quote 1
        • 1 / 1
        • First post
          Last post

        2
        Online

        3.7k
        Users

        1.3k
        Topics

        5.3k
        Posts

        • Privacy
        • Terms & Conditions
        • Donate

        © Copyrights and All right reserved Lanka Developers Community

        Powered by Axis Technologies (PVT) Ltd

        Made with in Sri Lanka

        | |