6 Replies - 20459 Views - Last Post: 09 December 2008 - 03:16 AM Rate Topic: -----

#1 Darkphoenix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-November 08

student marklist using struct

Posted 08 December 2008 - 08:38 AM

I have to write a program to read the details of students and create a marklist using struct.
Here's the code I wrote:
#include<stdio.h>
#include<conio.h>
#define STU 3
#define SUB 3
void main()
{
int k,i;
struct student
{
int rno;
char name[20];
int marks[2];
int total;
float avg;
}sk;
for(k=1;k<=STU;k++)
{
 printf("Enter the roll no and name of the student %d:",k);
 scanf("%d%s",&sk.rno,&sk.name);
 printf("Enter the marks:");
 for(i=0;i<SUB;i++)
  {
  scanf("%d",&sk.marks[i]);
  sk.total=sk.total+sk.marks[i];
  }
 sk.avg=sk.total/SUB;
}
printf("\nRollno\tName\tMarks\tTotal\tAvg\n");

for(k=1;k<=STU;k++)
 {
 printf("%d\t%s\t",sk.rno,sk.name);
 for(i=0;i<SUB;i++)
 printf("%d ",sk.marks[i]);
 printf("%d\t%.3f\n",sk.total,sk.avg);
 }
getch();
}


The program seems to be encountering an error while printing the details.
Please help!
Suggestions for improvement of the code are welcome!

Is This A Good Question/Topic? 0
  • +

Replies To: student marklist using struct

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,657
  • Joined: 23-August 08

Re: student marklist using struct

Posted 08 December 2008 - 08:55 AM

How about telling us what the errors are, rather than keeping us guessing?
Was This Post Helpful? 0
  • +
  • -

#3 Darkphoenix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-November 08

Re: student marklist using struct

Posted 08 December 2008 - 09:52 PM

Very sorry! Here are the errors I noticed:
1. The code doesn't print all the details read. It just prints only the last data read.
2.It doesn't read all the data.

This is my first program using struct so please go easy on me and my code.
Was This Post Helpful? 0
  • +
  • -

#4 sanle  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 30-September 08

Re: student marklist using struct

Posted 08 December 2008 - 11:09 PM

I think you have to declare an array

struct student{
...
...
}st[STU];




and every time you refer to one of the indexes of the array you use the index of the for loop


scanf("%d%s",&sk[k].rno,&sk[k].name);

 


hope it helps
Was This Post Helpful? 1

#5 Darkphoenix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-November 08

Re: student marklist using struct

Posted 09 December 2008 - 12:50 AM

I have modified the code a bit. But there are a few new errors:
There seems to be something wrong with "total"
Otherwise it seems to work.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define STU 3
#define SUB 3
#define A 90
#define B 80
#define C 70
#define D 60
#define E 50
#define F 49
void main()
{
int k,i;
struct student
{
int rno;
char name[20];
int marks[10];
int total;
float avg;
char grade;
char result[5];
}s[STU];

for(k=1;k<=STU;k++)
{
 printf("Enter the roll no and name of the student %d:",k);
 scanf("%d%s",&s[k].rno,&s[k].name);
 printf("Enter the marks:");

 for(i=0;i<SUB;i++)
  {
  scanf("%d",&s[k].marks[i]);
  s[k].total=s[k].total+s[k].marks[i];
  }
 s[k].avg=s[k].total/SUB;

 if(s[k].avg>E)
  {
  strcpy(s[k].result,"pass");
  if(s[k].avg>=E && s[k].avg<D)
  s[k].grade='E';
  else if(s[k].avg>=D && s[k].avg<C)
  s[k].grade='D';
  else if(s[k].avg>=C && s[k].avg<B)
  s[k].grade='C';
  else if(s[k].avg>=B && s[k].avg<A)
  s[k].grade='B';
  else
  s[k].grade='A';
  }

 else
  {
  strcpy(s[k].result,"fail");
  s[k].grade='F';
  }
}

printf("\nRollno\tName\tMarks\t\tTotal\tAvg\tGrade\tResult\n");

for(k=1;k<=STU;k++)
 {
 printf("%d\t%s\t",s[k].rno,s[k].name);
 for(i=0;i<SUB;i++)
 printf("%d ",s[k].marks[i]);
 printf("\t%d\t%.3f\t%c\t%s\n",s[k].total,s[k].avg,s[k].grade,s[k].result);
 }
getch();
}


Please help!
Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: student marklist using struct

Posted 09 December 2008 - 02:06 AM

Total is wrong because you are doing arithmetic on uninitialized data. You declare
	struct student
	{
		int rno;
		char name[20];
		int marks[10];
		int total;
		float avg;
		char grade;
		char result[5];
	}s[STU];


and then copy user input to marks which you then add to total. avg is wrong for the same reason.
		for(i=0;i<SUB;i++)
		{
			scanf("%d",&s[k].marks[i]);
			s[k].total=s[k].total+s[k].marks[i];
		}
		s[k].avg=s[k].total/SUB;


This can be easily fixed by adding this loop just before the main for() loop.
	for (int j = 0; j < STU; j++) {
		s[j].total = 0;
	}


The main for() loop starts the student array index at one, not zero. The value for total for the third student is wrong because the loop is addressing outside the array. Additionally, the size of the array is one more than the last legal index into it.
Use for(k = 0; k < STU; k++) not for(k = 1; k <= STU; k++)

The same thing is wrong with the printing loop
	printf("\nRollno\tName\tMarks\t\tTotal\tAvg\tGrade\tResult\n");

	for(k=1;k<=STU;k++)
	{
		...


This post has been edited by n8wxs: 09 December 2008 - 02:28 AM

Was This Post Helpful? 1
  • +
  • -

#7 Darkphoenix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-November 08

Re: student marklist using struct

Posted 09 December 2008 - 03:16 AM

My program almost works. The only thing that seems to be wrong is the 'rno'.
Here's the modified code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define STU 3
#define SUB 3
#define A 90
#define B 80
#define C 70
#define D 60
#define E 50
#define F 49
void main()
{
int k,i;
struct student
{
int rno;
char name[20];
int marks[10];
int total;
float avg;
char grade;
char result[5];
}s[STU-1];

for(int j=0;j<STU;j++)
{
s[j].total = 0;
}

for(k=0;k<STU;k++)
{
 printf("Enter the roll no and name of the student %d:",k+1);
 scanf("%d%s",&s[k].rno,&s[k].name);
 printf("Enter the marks:");

 for(i=0;i<SUB;i++)
  {
  scanf("%d",&s[k].marks[i]);
  s[k].total=s[k].total+s[k].marks[i];
  }
 s[k].avg=s[k].total/SUB;

 if(s[k].avg>E)
  {
  strcpy(s[k].result,"pass");
  if(s[k].avg>=E && s[k].avg<D)
  s[k].grade='E';
  else if(s[k].avg>=D && s[k].avg<C)
  s[k].grade='D';
  else if(s[k].avg>=C && s[k].avg<B)
  s[k].grade='C';
  else if(s[k].avg>=B && s[k].avg<A)
  s[k].grade='B';
  else
  s[k].grade='A';
  }

 else
  {
  strcpy(s[k].result,"fail");
  s[k].grade='F';
  }
}

printf("\nRollno\tName\tMarks\t\tTotal\tAvg\tGrade\tResult\n");

for(k=0;k<STU;k++)
 {
 printf("%d\t%s\t",s[k].rno,s[k].name);
 for(i=0;i<SUB;i++)
 printf("%d ",s[k].marks[i]);
 printf("\t%d\t%.2f\t%c\t%s\n",s[k].total,s[k].avg,s[k].grade,s[k].result);
 }
getch();
}

Initialisation of 'rno' doesn't help!

This post has been edited by Darkphoenix: 09 December 2008 - 03:16 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1