Like Tree3Likes
  • 1 Post By grumpy
  • 1 Post By iMalc
  • 1 Post By Salem

Can Any one Explain What is Wrong code?

This is a discussion on Can Any one Explain What is Wrong code? within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> char gradecal(double x) { if(x>=90) printf("A"); else if(x>=80) printf("B"); else if(x>=70) printf("C"); else if(x>=60) printf("D"); else printf("F"); } ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    1

    Can Any one Explain What is Wrong code?

    Code:
    #include<stdio.h>
    char gradecal(double x)
    {
         if(x>=90)
                  printf("A");
         else if(x>=80)
                  printf("B");
         else if(x>=70)
                  printf("C");
         else if(x>=60)
                  printf("D");
         else
                  printf("F");     
     }
    
    
    int main()
    {
        
    int rollno[30],sub1[30],sub2[30],n,i,j,number,number2,number3,total[30];
    float avg[30],grade[30];
    printf("enter The Limit ");
    scanf("%d",&n);
    printf("Enter Marks Student id\n");
    for( i =1;i<n;i++)
    {
        scanf("%d \t %d \t %d \t",&number,&number2,&number3);
        rollno[i] = number;
        sub1[i] = number2;
        sub2[i] = number3;x`
        total[i] = sub1[i]+sub2[i];
        avg[i] = (total[i] / 2) * 100 ;
        grade[i] = gradecal(avg[i]);
        
    }
    printf("Priting the REsults \t\n");
    for(j=1;j<n;j++)
    {
        printf("%d\t %d\t %d\t %c\t ",rollno[j],sub1[j],sub2[j],grade[j]);
    }
        return 0;
    }
    When i'm running the above code getting output but it's not printing the GRADE

    and it's printing only 1 results out of 2 input on array

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    C++ array indexing starts at 0, so:

    Code:
    for( i =1;i<n;i++)
    {
        scanf("%d \t %d \t %d \t",&number,&number2,&number3);
        rollno[i] = number;
        sub1[i] = number2;
        sub2[i] = number3;x`
        total[i] = sub1[i]+sub2[i];
        avg[i] = (total[i] / 2) * 100 ;
        grade[i] = gradecal(avg[i]);
         
    }
    Is going to skip the first entry in all your arrays.

  3. #3
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    (1) Array offsets (indices) start at 0 in C.
    (2) Stop trying to be cute with `scanf()`, it never ends well. I doubt that you're actually giving the input in form "n1_>_n2_>_n3_>", where `_` is space and `>` is tab.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,300
    In addition to previous responses ....

    1) when printing the results (second loop) grade[j] is of type float. Using the %c format to print it gives undefined behaviour. In the first loop "grade[i] = gradecal(avg[i])" does not magically turn grade[i] into something that can be printed using %c.
    2) if you enter a "limit" that exceeds 30, both loops fall off the end of the arrays. That also yields undefined behaviour.
    Salem likes this.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Turn up your warning levels, pay attention to the warnings, and fix them. Your gradecal function is not returning anything.
    Salem likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > I doubt that you're actually giving the input in form "n1_>_n2_>_n3_>", where `_` is space and `>` is tab.
    All white space is equivalent to scanf -> man page scanf section 3
    Each successive pointer argument must correspond properly with each suc-
    cessive conversion specifier (but see the * conversion below). All con-
    versions are introduced by the % (percent sign) character. The format
    string may also contain other characters. White space (such as blanks,
    tabs, or newlines) in the format string match any amount of white space,
    including none
    , in the input. Everything else matches only itself.
    Scanning stops when an input character does not match such a format char-
    acter. Scanning also stops when an input conversion cannot be made (see
    below).
    msh likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Screw `scanf()`. x_x
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain what can go wrong with the following C code
    By luckyluke in forum C Programming
    Replies: 8
    Last Post: 07-26-2010, 07:03 AM
  2. Replies: 2
    Last Post: 03-16-2010, 10:49 AM
  3. Could someone explain what I did wrong with this?
    By Paroxysm in forum C++ Programming
    Replies: 7
    Last Post: 01-07-2008, 02:08 PM
  4. Could somebody please explain to me where I went wrong?
    By tsvguy in forum C++ Programming
    Replies: 15
    Last Post: 11-23-2005, 06:25 AM
  5. can someone explain what is wrong here...
    By Lynux-Penguin in forum C Programming
    Replies: 2
    Last Post: 05-13-2002, 11:39 AM

Tags for this Thread


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