Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. /*
  2.  * This code has issues in the sizes and usage of its arrays
  3.  */
  4. #include <stdio.h>
  5.  
  6. #define MAX_ROW 10
  7. # define MAX_COL 10
  8. //Function prototypes
  9. void print1DArray(int[]);
  10. void print2DFLAArray(int[][MAX_COL]);
  11. void print2DVLAArray(int[MAX_ROW][MAX_COL]);
  12. int main() {
  13.   //Local declarations
  14.  
  15.   int ary1[0];
  16.   int ary2[] = {
  17.     1,
  18.     2,
  19.     3,
  20.     4,
  21.     5,
  22.     6,
  23.     7,
  24.     8,
  25.     9
  26.   };
  27.   int x = 10;
  28.   int y = 10;
  29.   int ary3[x][y]; //VLA
  30.   int ary4[][]; //FLA
  31.  
  32.   //Local Statements
  33.   ary1[1] = 20;
  34.   ary2[11] = 20;
  35.  
  36.   return 0;
  37. } //end main
  38. void print1DArray(int aryIn[]) {
  39.   printf("Array: ");
  40.   for (int i = 0; i < MAX_COL; i++) {
  41.     printf("%d ", aryIn[i]);
  42.   }
  43.   printf("\n");
  44.   return;
  45. } //end function
  46. //Note how the FLA 2d Array needs to know the size of columns
  47. void print2DFLAArray(int aryIn[][MAX_COL]) {
  48.   printf("2dArray: \n");
  49.   for (int i = 0; i < MAX_ROW; i++) {
  50.     for (int j = 0; j < MAX_COL; j++) {
  51.       printf(" %d ", aryIn[i][j]);
  52.     } //end inner for
  53.     printf("\n");
  54.   } //end outer for
  55.   printf("\n");
  56.   return;
  57. }
  58. //Note how the VLA 2d Array needs to know both sizes of rows and columns
  59. void print2DVLAArray(int aryIn[MAX_ROW][MAX_COL]) {
  60.   printf("2dArray: \n");
  61.   for (int i = 0; i < MAX_ROW; i++) {
  62.     for (int j = 0; j < MAX_COL; j++) {
  63.       printf(" %d ", aryIn[i][j]);
  64.     } //end inner for
  65.     printf("\n");
  66.   } //end outer for
  67.   printf("\n");
  68.   return;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement