Showing posts with label Array of Structures. Show all posts
Showing posts with label Array of Structures. Show all posts

Saturday, 30 July 2016

Array Within the Structure

Array Within the Structure


#include<stdio.h>
struct student
{
            char name[20];           
            int rollno;
            float mark[3];
};
main()
{
            struct student S[10];
            int i, n, j;
            printf("\nEnter the number of students: ");
            scanf("%d", &n);
            for(i=0; i<n; i++)
            {
                        printf("\nEnter Student %d details: ", i+1);
                        printf("\nEnter name rollno: ");
                        scanf("%s %d", S[i].name, &S[i].rollno);
                        printf("\nEnter 3 marks: ");
                        for(j=0; j<3; j++)
                                                scanf("%f", &S[i].mark[j]);
            }
            for(i = 0;i<n;i++)
            {
                        printf("\nStudent %d details: ", i+1);
                        printf("\n\tName is %s \n\tRollno is %d", S[i].name, S[i].rollno);
                        printf("\n\tMarks are: ");
                        for(j=0; j<3; j++)
                                                printf("%f  ", S[i].mark[j]);
            }

}

Output:
Enter the number of students: 2

Enter Student 1 details:
Enter name rollno: aaa 11
Enter 3 marks: 50 60 70

Enter Student 2 details:
Enter name rollno: bbb 22
Enter 3 marks: 60 70 80

Student 1 details:
        Name is aaa
        Rollno is 11
        Marks are: 50.000000    60.000000       70.000000
Student 2 details:
        Name is bbb
        Rollno is 22
        Marks are: 60.000000    70.000000       80.000000

Goto Main Content:
https://tejaswinihbhat.blogspot.in/2016/07/data-structures-15CS33-Module1.html
https://tejaswinihbhat.blogspot.in/2016/07/structures.html

Array of Structures

Array of Structures

#include<stdio.h>
struct student
{
            char name[20];          
            int rollno;           
            float mark;
};
main()
{
            struct student S[10];
            int i, n;
            printf("\nEnter the number of students: ");
            scanf("%d", &n);
            for(i=0; i<n; i++)
            {
                    printf("\nEnter name rollno and mark of a student %d: ", i+1);
                        scanf("%s %d %f", S[i].name, &S[i].rollno, &S[i].mark );
            }
            for(i=0; i<n; i++)
            {
                        printf("\nStudent %d details: ", i+1);
                        printf("\n \tName is %s \n\tRollno is %d \n\tMark is %f", S[i].name, S[i].rollno, S[i].mark );
            }
}

Output:
Enter the number of students: 3


Enter name rollno and mark of a student 1: aaa       10        55
Enter name rollno and mark of a student 2: bbb       20        66
Enter name rollno and mark of a student 3: ccc        30        77
Student 1 details:
    Name is aaa
    Rollno is 10
    Mark is 55.000000
Student 2 details:
    Name is bbb
    Rollno is 20
    Mark is 66.000000
Student 3 details:
    Name is ccc
    Rollno is 30
    Mark is 77.000000


Goto Main Content:
https://tejaswinihbhat.blogspot.in/2016/07/data-structures-15CS33-Module1.html
https://tejaswinihbhat.blogspot.in/2016/07/structures.html