debug please

This is a discussion on debug please within the C Programming forums, part of the General Programming Boards category; Tring to arrange name of N students according to their marks obtained...... I marked a certain field in this portion ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    73

    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;
    }

  2. #2
    Registered User
    Join Date
    Mar 2013
    Location
    Bangalore
    Posts
    4
    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/

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    73
    no no just testing if the marks get reflected in the output....finally got it dine by myself...thank you anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ms-dos debug
    By llinocoe in forum Windows Programming
    Replies: 1
    Last Post: 09-30-2008, 07:24 AM
  2. using DEBUG - hopefully
    By darfader in forum C Programming
    Replies: 1
    Last Post: 08-12-2003, 05:21 AM
  3. Please help debug
    By Wexy in forum C Programming
    Replies: 2
    Last Post: 11-10-2002, 11:40 PM
  4. gets debug
    By jerryvtts in forum C Programming
    Replies: 1
    Last Post: 07-12-2002, 01:29 AM
  5. Dev-C++ DEBUG
    By Paninaro in forum C Programming
    Replies: 2
    Last Post: 06-28-2002, 04:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21