Thread: changing data output from whole number to float two decimal places

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    22

    changing data output from whole number to float two decimal places

    Code:
    #include<stdio.h>
    //Write a function, invoked from the main function which will allow scanning data into the array.
    void input(int examscores[5][4])
    {
     int i,j;
     for(i=0;i<5;i++)
     {
      for(j=0;j<4;j++)
      {
       printf("input grades of student %d for exam %d\n: ",i+1,j+1);
       scanf("%d",&examscores[i][j]);
      }
      printf("\n\n");
     }
    }
    //Write a function that will calculate and output the average (to 2 decimal places) 
    //for each student appropriately labeled.
    void average(int examscores[5][4])
    {
     int i,j;
     int sum=0;
     double avg_student;
     for(i=0;i<5;i++)
     {
      printf("Student %d's average score is\n ",i+1);
      sum=0;
      for(j=0;j<4;j++)
      {
       sum=sum+examscores[i][j];
      }
      avg_student=sum/4;
      printf("%.2f\n",avg_student);
     }
    }
    //4) Write a function that will calculate and output the average for each of the 4 exams 
    //appropriately labeled.
    void overall_average(int examscores[5][4])
    {
     int i,j;
     int sum;
     double avg;
     for(j=0;j<4;j++)
     {
      printf("\nExam %d Average: ",j+1);
      sum=0;
      for(i=0;i<5;i++)
      {
       sum=sum+examscores[i][j];
      }
      avg=sum/5;
      printf("%2f",avg);
     }
    }
     //Write a function that will output the original entry data appropriately labeled. 
    //There should be column headings for the exam number and row labels for the student number as shown:
    void output(int examscores[5][4])
    {
     int i,j;
     printf("\n  Exam 1 Exam 2 Exam 3 Exam 4");
     for(i=0;i<5;i++)
     {
      printf("\n%d   ",i+1);
      for(j=0;j<4;j++)
      {
       printf("%d \t",examscores[i][j]);
      }
     }
    }
    int main(void)
    {
            //Create a 2 dimensional array (5X4 (int type)) which can hold 4 exam scores for each of 5 students.
            int examscores[5][4];
            input(examscores);
            average(examscores);
            printf("\n");
            overall_average(examscores);
            printf("\n");
            output(examscores);
            printf("\n");
            getchar();
            return 0;
    }
    my output for these scores comes out in whole numbers. How do I get it to read out 2 decimal places to the left?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You have to use floating point numbers.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    like %2f rather than %d's or float rather and int

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yes that's half of it.
    Hint on the other half of it: sum/4 is an int, but sum/4.0f is a float
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-24-2011, 03:01 PM
  2. Printing specified number of decimal places?
    By 843 in forum C Programming
    Replies: 4
    Last Post: 11-26-2010, 03:47 AM
  3. Round float to 2 decimal places
    By tesla in forum C Programming
    Replies: 8
    Last Post: 09-15-2009, 12:51 AM
  4. Float Decimal Places
    By TyrioN in forum C Programming
    Replies: 1
    Last Post: 08-15-2004, 02:30 AM
  5. Replies: 10
    Last Post: 06-12-2002, 03:15 PM