Thread: New to C help with rounding

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    41

    New to C help with rounding

    Hi guys, I've been trying to round my output but I dont seem to be able to. I googled it but I dont understand float and ceiling or how to work them into my code. I did find that a roundf command should exsist but I dont have that option. I am using visual express studio so I wasnt sure if that just didnt have that command. Any help is greatly appreciated. Thank you.
    Code:
    #include
    <stdio.h>
    
    #include
    <stdlib.h>
    
    #include
    <math.h>
    
    
     
    
    // function declarations
    
    
    float
     schoolgrade(float x, float y, float z);
    
    
    float
     useroutput(float CG, float HG, float MG, float FG);
    
    
     
    
    int
     main ()
    
    {
    
        
    // local declarations
    
        
    float homework = 0;
    
        
    float midterm = 0;
    
        
    float finalexam = 0;
    
        
    float finalgrade = 0;
    
        
    int student = 0;
    
        
    int studentID = 1;
    
        
    
        printf(
    "Enter total number of students \n");
    
        scanf(
    "%d" ,&student);
    
        
    
        
    while(student > 0) 
    
        {
    
        printf (
    "Enter 3 grades for student %d\n", studentID);
    
        scanf  (
    "%f %f %f",&finalexam, &midterm, &homework);
    
            
    if (finalexam <= 100 && finalexam >= 0 && midterm <= 75 && midterm >= 0 && homework  <= 120 && homework >= 0)
    
                {
    
                finalgrade = schoolgrade(finalexam, midterm, homework);
    
                useroutput(finalexam, midterm, homework, finalgrade);
    
                system (
    "pause");
    
                student--;
    
                studentID++;
    
                }
    
            
    else
    
            {
    
            printf(
    "Incorrect value entered \n");
    
            }
    
        }
    
    
     
    
    }
    
    
    float
     schoolgrade(floatx, floaty, floatz)
    
    {
    
    
    float
     CG = 0;
    
    float
     HG = 0;
    
    float
     MG = 0;
    
    float
     FG = 0;
    
    
        
    
            FG = (
    x/100) * .30;        
    
            MG = (
    y/75) * .20;
    
            HG = (
    z/120) * .50;
    
                
    
                
    
    CG = (FG + MG + HG) * 100;
    
    return
     CG;
    
    
     
    
    }
    
    
     
    
    float
     useroutput(floata, floatb, floatc, floatd)
    
    {
    
    
        printf (
    "Final exam was %f out of 100 \n", a);
    
        printf (
    "Medterm exam was %f out of 75 \n", b);
    
        printf (
    "Homework assignments were %f out of 120 \n ",c);
    
          puts (
    "---------------------------------------- \n");
    
        printf (
    "Grade is %f \n", d);
    
    
    }
    


  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In addition to other ways to round, printf() has a built in rounder you might find easy to use:

    Code:
    #include <stdio.h>
    
    int main(void) {
       float f=1.14;
       double d=1.155;
    
       printf("float f (1.14rounded to even 10ths): %.1f\n\n",f);
       printf("double d (1.155, rounded to even 100ths): %.2f\n",d);
    
    
       return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    It varies depending on rounding rules of course...
    Code:
    float fl = 1.5;
    int rounded = floor( fl + 0.5f );
    If you need to use ceiling, I'm sure you can figure it out from here.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    @phillyflyers - please make sure you "paste as text" when you copy code.
    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. Rounding Up
    By bayles_83 in forum C Programming
    Replies: 4
    Last Post: 09-04-2012, 03:52 AM
  2. Rounding in C
    By blackcell in forum C Programming
    Replies: 13
    Last Post: 02-16-2008, 03:44 AM
  3. Rounding
    By mdoland in forum C# Programming
    Replies: 2
    Last Post: 10-04-2007, 01:18 AM
  4. C++ rounding
    By dogbert234 in forum C++ Programming
    Replies: 9
    Last Post: 12-23-2005, 04:09 PM
  5. Always rounding up
    By titleist_03 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2004, 01:08 PM