Thread: *need help in array issue*

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    *need help in array issue*

    Hi everyone, I'm trying to do an array with size 100 initialized with values
    Code:
    {0}
    that's telling me how many students get the same grade by increasing everytime value of the given grade's index, for example : lets say I have inputted grade :80, then it must increase 1 in the countA[80]+=1; and if I have entered the grade 70..then it must increase 1 to the value of the countA[70]+=1....etc!, in other words the indexes of the array resembling the given grades and whenever there's a specific grade, then the code goes to the count[grade] and increasing it by 1.
    how many grades I'm entering is related to the variable size that's decided by the user itself!

    what I have done is that:
    Code:
    #define NUM_OF_GRADES 100
    int main()
    {   
     int countA[NUM_OF_GRADES]={0};
     printf("Please enter the class size:\n");  
    scanf("%d",&size);    
    printf("Please enter first class grades:\n");   
     for (int i=0 ; i<size  ; r++)    
    {        
    countA[scanf("%d\n",&grade)]++ ;
     // here I'm going to the given grade as it's the index of the array countA and increasing the value of it by 1 then it means that this grade has been entered..etc.//    }
     printf("the class's grades") ;
    Code:
    for (int i=0 ; i<size  ; r++)    
    {       
    printf("%d", countA[i]);   
     } 
     return 0;
    }
    


    I tried to print the array after I've inputted some grades but unfortunately it showed wrong printf, conversely to what expected!
    can anyone please help me and suggest me why my code isn't working correctly????

    thanks!
    Last edited by RyanC; 11-26-2015 at 03:41 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > countA[scanf("%d\n",&grade)]++ ;
    Yes, you should stop trying to compress code.

    Knowing for example, that scanf does NOT return grade, like you seem to imply that it does.

    Write
    scanf("%d\n",&grade) ;
    countA[grade]++ ;


    Then you might have a chance.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Array Sort Issue
    By atrackbrown in forum C Programming
    Replies: 1
    Last Post: 03-06-2014, 05:07 PM
  2. Array Issue.
    By mcertini in forum C Programming
    Replies: 7
    Last Post: 02-16-2011, 10:56 AM
  3. character array issue
    By deepcode in forum C Programming
    Replies: 4
    Last Post: 01-27-2011, 07:52 PM
  4. Array of Struct issue
    By B1acksun in forum C Programming
    Replies: 9
    Last Post: 11-30-2005, 03:06 PM
  5. array issue
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 07-10-2003, 08:37 AM