-
debug please
Tring to arrange name of N students according to their marks obtained......
I marked a certain field in this portion stating "PROBLEM ARISES WHEN THIS SECTION OF CODE EXECUTED"
Yes,If I separately run the program eliminating the part of code I get the Original Data (Ie.Name and marks of n students as inputed by the user)
Please debug it at your convinience!
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct stud
{
char *name;
int marks;
}STUDENT;
int main()
{
STUDENT *A;
int n,i,j,T,i1,i2;
clrscr();
printf("Number of students :");
scanf("%d",&n);
A=(STUDENT *)malloc(n*sizeof(STUDENT));
for(i2=0;i2<n;i2++)
{
printf("\nEnter details of student %d\n",i2+1);
printf("Enter name :");
scanf("%s",A[i2].name);
printf("\nEnter Marks :");
scanf("%d",&A[i2].marks);
}
printf("Original DATA :\n");
for(i1=0;i1<n;i1++)
{
printf("\nSTUDENT[%d]\nNAME :%s\n MARKS %d\n___\n",i1+1,A[i1].name,A[i1].marks);
}
//PROBLEM ARISES IF THIS SECTION OF CODE EXECUTED!!!!!!
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(A[j].marks>A[j+1].marks)
{
T=A[j].marks;
A[j].marks=A[j+1].marks;
A[j+1].marks=T;
}
}
}
printf("\n\nSorted DATA :\n");
for(i=0;i<n;i++)
{
printf("\nSTUDENT[%d]\nNAME :%s\n MARKS %d\n___\n",i+1,A[i].name,A[i].marks);
}
//UPTO HERE
getch();
return 0;
}
-
Looking at the code looks like you want to rearrange the marks and not sure what you intended to do here. Also for sorting you can just use one loop instead 2 loops. You have just swapped the marks. name is a pointer which is not allocated before getting user input. You have to write better code.
-Yogi
yogindar.com/news/
-
no no just testing if the marks get reflected in the output....finally got it dine by myself...thank you anyway :)