Sunday, 21 January 2018

Arrays With In The Structures


Arrays With In The Structures


Arrays With In The Structures
 C permits the use of arrays as structure members.we can use single or multidimensional arrays of type int or float.
Ex:   struct marks
      {
        int number;
        float subject[3];
        student[2];
      }
  here,the member subject contains 3 elements,subject[0],subject[1] and subject[2].These elements can be accessed using appropriate subscripts. For example ,the name student[1].subject[2]; would refer to the marks obtained in the third subject by the second student.

 We can use arrays inside the structures. We can use single or multidimensional arrays of type int or float.For example,the following structure declaration is valid.
   struct marks
   {
    int number;
    float subject[3];
   }student[3];

   Here,the member subject contains 3 elements,subject[0],subject[1] and subject[2].These elements can be accessed using subscripts like student[1].subject[2];would refer to the marks obtained in the third subject by the second student.
/*Arrays Within The Structures*/
main()
{
struct marks
{
int sub[3];
int total;
};
static struct marks student[3]={45,67,81,0,75,53,
                                69,0,57,36,71,0};
static struct marks total;
int i,j;
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
student[i].total += student[i].sub[j];
total.sub[j] +=student[i].sub[j];
}
total.total +=student[i].total;
}
printf("STUDENT TOTAL \n\n");
for(i=0;i<=2;i++)
printf("student[%d] %d\n",i+1,student[i].total);
printf("SUBJECT TOTAL \n\n");
for(j=0;j<=2;j++)
printf("subject-%d %d\n",j+1,total.sub[j]);
printf("\n Grand Total = %d\n",total.total);
}
OUTPUT:
STUDENT              TOTAL
student[1]        193
student[1] 197
student[1] 164
SUBJECT  TOTAL
subject-1 177
subject-2 156
subject-3 221
Grand Total          =554

Related Posts:

  • Defining And Opening A File Defining And Opening A File  If we want to store data in a file in the secondary memory, we must specify certain things about the file , to the operating system. They include:  1.File name.  2.Data Structu… Read More
  • Closing A File & File Functions Closing A File & File Functions When we have finished all operations the file must be closed . This ensures that all outstanding information associated with the file is flushed out from the buffers and all links to… Read More
  • Difference Between The Macros And Functions Difference Between The Macros And Functions Macros are like functions but there is difference between those two . 1. In macro call the processor replaces the macro template with its macro expansion. Where as in functi… Read More
  • Inroduction To PreProcessors Inroduction To PreProcessors INTRODUCTION:     The C preprocessor is a program that processes our source program before it is passed to the compiler.This is a  capability that does not exist in many other … Read More
  • Enumerated Data Type Enumerated Data Type     This is one of the user defined datatypes. The use of this datatype is to make programs more readable.It allows you to create your own datatype with predefined values. Though its form … Read More

0 comments:

Post a Comment