C Programming: How to improve C to support more Array N dimensions


Objective of this post: To change array 2 dimensions to support more requirements by using structure

Syntax:
      Please find its well-explained syntax at below link:
      https://www.tutorialspoint.com/cprogramming/c_structures.htm

Sample from C community: A guy from C community asked me to write another code. Here is his code snippet
/* This code from C community N students with math and algo marks.
    If math > 70 and algo > 50 then pass, otherwise fail. This simple C program with 2D ARRAY. */
   // Compiler version gcc 6.3.0

#include <stdio.h>
int main(void)
{
      int n,i,j;
      int mark[n][2];       

      printf("enter the no of students");
      scanf("%d",&n);  
      printf("\n enter math and algo marks");
      for(i=0;i<n;i++)

      {
          for(j=0;j<2;j++)

          {
             scanf("%d",&mark[i][j]);

           }
       }
       for(i=0;i<n;i++)
       {
            if(mark[i][0]>70 && mark[i][1]>50)
            {
                  printf("\n pass");
             }
             else
             {
                   printf("\n fail");
             }
       }
       return 0;
}

I cannot find GCC 6.3.0 then I compiled it with DevC++
Suggestion from my experience
       I suggested him to change Array to Structure in case he might have to add more subjects to support more than Math mark and Algo mark. I mean, array 10 dimensions(subjects) or something like that.

== Here is my experience ==
++ Suggestion No.1 ++

#include <stdio.h>
typedef struct studentMark
{
      int math;
      int algo;
      /* You may add more subject mark without changing anything in function(s) argument since we pass to
          parameter by structure to support many array dimensions */

}stStudentMark;
void vdPass_or_fail(int argNoOfStudent, stStudentMark *argStudent)
{
      printf("Criteria; Math>70 and Algo>50 then PASS\n\n");
      for(int i=0 ; i<argNoOfStudent ; i++) /* To declare variable while using is feature of C11 C99 */
      {
          if(argStudent[i].math > 70 && argStudent[i].algo > 50)
              printf("student %d, math=%d, algo=%d, PASS\n",i+1,argStudent[i].math, argStudent[i].algo);
          else
              printf("student %d, math=%d, algo=%d FAIL\n",i+1, argStudent[i].math, argStudent[i].algo);
       }
}
int main(int argc, char *argv[])
{
        stStudentMark student[3]={{30, 90},{75, 60},{70,50}};
        /* 3 students, student1 math=30 marks, algo=90 marks ordered by member in structure stStudentMark */
        int iNumberOfStudent=sizeof(student)/sizeof(stStudentMark);
        /* Size of iNumberOfStudent magically varies on size of students and size of structure so no need to change it when requirement changed */
        vdPass_or_fail(iNumberOfStudent, student);
        return 0;
}
Compile in DevC++ but error about define int i in for loop
because the compiler is not C11 or C99
It's my intention to make it error.



You may try compile with C11 or C99 at https://ideone.com/
It is completely compiled.

++ Suggestion No. 2 ++
     If we add more subjects and display student's name not student no.

#include <stdio.h>
#define NUMBER_OF_STUDENTS 3   // define number of students
typedef struct studentMark
{
     char name[30+1]; /* Add for show name instead student number +1 for end of string 0x00 */
 
   int math;

     int algo;
     int physics; /* Add more subject */
}stStudentMark; void vdPass_or_fail(int argNoOfStudent, stStudentMark *argStudent)
{
      system("mode con: cols=150 lines=50"); /* set console size to display all result */
      printf("Criteria; Math>70 and Algo>50 and Physics>50 then PASS\n\n");
      int i;
      for(i=0 ; i<argNoOfStudent ; i++)
     {
             if(argStudent[i].math > 70 && argStudent[i].algo > 50 && argStudent[i].physics > 50)
            {
                printf("Name=%s \tmath=%d,\talgo=%d,\tphysics=%d\tresult = PASS\n",argStudent[i].name, argStudent[i].math, argStudent[i].algo, argStudent[i].physics);
             } else {
                printf("Name=%s \tmath=%d,\talgo=%d,\tphysics=%d\tresult = FAIL\n",argStudent[i].name, argStudent[i].math, argStudent[i].algo, argStudent[i].physics);
            }
      }
} int main(int argc, char *argv[])
{
       stStudentMark student[NUMBER_OF_STUDENTS]={{"Edward Snowden",30,90,71},  
                               {"Mats Engstorm",75,60,90},{"Chattrawit",70,50,50}};
        int iNumberOfStudent=sizeof(student)/sizeof(stStudentMark); 
        /* iNumberOfStudent will be automatically changed when requirement changed */
       vdPass_or_fail(iNumberOfStudent, student);
       return 0;
}

Add more structure members =  array more dimensions
REMARK: Please see comment, it clearly tells  you












You may leave comment to improve it.




 Thank you so much from THAILAND

Popular posts from this blog

C Programming: Superb E-Book from beginner to expert [C career semi retired]

C Programming: Understand For loop syntax in deep detail