Wednesday, November 25, 2015

Write a C program to maintain a record of “n” student details using an array of structures with four fields (Roll number, Name, Marks, and Grade). Each field is of an appropriate data type. Print the marks of the student given student name as input

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
struct student{
char name[14];
int rollNo;
int marks[3];
float percentage;
};

int main(){
struct student *stuArray;
int n,i,j;
int tempTotal=0,flag=0,foundIndex;
char tempName[14];
clrscr();
printf("\n No of Students: ");
scanf("%d",&n);
stuArray = (struct student*)malloc(n*sizeof(struct student));
for(i=0;i<n;i++){
printf("\n %d.Name :",(i+1));
scanf("%s",&stuArray[i].name);
printf("\n Roll Number :",(i+1));
scanf("%d",&stuArray[i].rollNo);
tempTotal=0;
for(j=0;j<3;j++)
{
printf("\n Mark %d :",(j+1));
scanf("%d",&stuArray[i].marks[j]);
tempTotal+=stuArray[i].marks[j];
}
stuArray[i].percentage=tempTotal/3;
}

printf("\n Enter the Name to be Searched: ");
scanf("%s",&tempName);
for(i=0;i<n;i++){
if(strcmp(tempName,stuArray[i].name)==0)
{
foundIndex=i;
flag=1;
break;
}
}
if(flag==0)
{
printf("Details Not Found");
}
else
{
printf("\n Mark of %s are given below...",tempName);
for(i=0;i<3;i++)
{
printf("\n Mark %d is %d",(i+1),stuArray[foundIndex].marks[i]);
}
}
getch();
return 0;
}

To Compile Press Alt+f9 
To Run Press Ctrl+f9

Output:


No comments:

Post a Comment