quick question

This is a discussion on quick question within the C Programming forums, part of the General Programming Boards category; lets say you have a bunch of ints, and you want to find the average in a float, so you ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    33

    quick question

    lets say you have a bunch of ints, and you want to find the average in a float, so you add them all together and divide by how many (25 in my case), but like do i need to cast the int to a float before dividing because i have

    float ave;
    int sum;

    ave = sum / 25;

    and the answer is never correct :-( i know this is a simple question but i think i was asleep when we went over it in class

    thanks guys

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Code:
    #include <stdio.h>
    
    int main()
    {
       int total, tests;
       float ave;
       
       total = 5, tests = 439;
       ave = (float)tests / (float)total;
       
       printf("%.2f", ave);
       
       return 0;
    }
    Is that what you're after? You should be able to cast either int to get a floating point.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    oh thanks i kept doing num (float) / 25

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-27-2005, 12:05 AM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 04:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 05:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 12:04 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 06:46 PM

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