ADVERTISEMENT
ADVERTISEMENT

C program to demonstrate example of structure of array

In this program, we will learn how to declare an array of some data type's variables within the structure, which is known as structure of array?

In this example, there is a structure named student having two arrays name and marks, name is the character array of 30 characters and marks is the integer array of 5 integers. name will store the name of the student and marks will store the marks of 5 subjects.

/*C program to demonstrate example of structure of array.*/

#include <stdio.h>

struct student
{
	char name [30];
	int  marks[ 5];
	int  total;
	float perc;
};

int main()
{
	struct student std;
	int i;
	
	printf("Enter name: ");
	gets(std.name);

	printf("Enter marks:\n");
	std.total=0;
	for(i=0;i< 5;i++){
		printf("Marks in subject %d ?: ",i+1);
		scanf("%d",&std.marks[i]);
		std.total+=std.marks[i];
	}
	std.perc=(float)((float)std.total/(float)500)*100;

	printf("\nName: %s \nTotal: %d \nPercentage: %.2f",std.name,std.total,std.perc);
	
	return 0;
}

Output

Enter name: Mike
Enter marks:
Marks in subject 1? : 88
Marks in subject 2? : 98
Marks in subject 3? : 98
Marks in subject 4? : 78
Marks in subject 5? : 99

Name: Mike 
Total: 461 
Percentage: 92.20

C Structure & Union Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.