Printing average score and the count

This is a discussion on Printing average score and the count within the C Programming forums, part of the General Programming Boards category; Hello am new in the game and am somehow stuck, I cant get the "avgScore" and the "ctr" right. can ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Topeka, Kansas, United States
    Posts
    2

    Question Printing average score and the count

    Hello am new in the game and am somehow stuck, I cant get the "avgScore" and the "ctr" right. can someone pliz tell me where am going wrong.
    Code:
        int IdNum,J1,J2,J3,ctr,e;
        float totalScore,avgScore,total;
        FILE *payInFile, *payOutFile;
        payInFile=fopen("dataIn.txt","r");
        payOutFile=fopen("dataOut.txt","w");
        fprintf(payOutFile,"\n Candidate Results\n");
        fprintf(payOutFile,"IdNum J1   J2    J3    totalScore \n");
        ctr=0;
        total=0;
        e=fscanf(payInFile, "%1d %1d %1d %1d", &IdNum, &J1, &J2, &J3);
        while(e==5)
      
                {  
                 totalScore = J1+J2+J3;
                 fprintf(payOutFile,"%d     %d     %d     %d    %3.1f\n", 
                 IdNum ,J1 ,J2 ,J3 ,totalScore);
                 ctr=ctr+1;  
                 total = total + totalScore;    
                 
                 e=fscanf(payInFile, "%1d %1d %1d %1d", &IdNum, &J1, &J2, &J3);
                 
                }
        avgScore = totalScore / ctr; 
        fprintf(payOutFile, "ctr = %2f and avgScore is %2.2f \n",ctr,avgScore);
    
        fclose(payInFile);
        fclose(payOutFile);
              system("pause");
      } //end program

    You can use this Data

    IdNum J1 J2 J3
    6 3 2 4
    5 2 3 3
    4 5 2 4
    3 4 5 4
    2 3 2 3
    1 2 3 2

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    593
    You need to display ctr as an int, not a float. For starters fix line 24 then see if there are still problems.
    Last edited by camel-man; 02-13-2012 at 05:28 PM.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Location
    Topeka, Kansas, United States
    Posts
    2
    thanks, av fixed the problem n its now working perfectly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-27-2011, 10:56 PM
  2. Uppercase/Lowercase/Word COunt/Char count
    By Charak in forum C Programming
    Replies: 7
    Last Post: 02-23-2011, 07:16 AM
  3. Calculating Average and high/lowest test score
    By aexi in forum C++ Programming
    Replies: 4
    Last Post: 07-26-2010, 01:54 AM
  4. Average score
    By wiz23 in forum C++ Programming
    Replies: 22
    Last Post: 05-05-2005, 05:38 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM

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