Question about functions

This is a discussion on Question about functions within the C Programming forums, part of the General Programming Boards category; Hi! My program finds the average & mode of a set of 6 integers. It's all under main. See? PHP ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    16

    Question about functions

    Hi! My program finds the average & mode of a set of 6 integers. It's all under main. See?


    PHP Code:
    #include<stdio.h>

    int main()
    {
        
    int temp[6], iavgmode;


        
    printf("Enter 6 integers: *REMEMBER TO PRESS ENTER BETWEEN EACH INTEGER!* ");
        for(
    i=0i<6i++){
            
    scanf("%d", &temp[i]);

        }

        
    /*average*/

        
    avg 0;
        for(
    i=0i<6i++){
            
    avg avg+temp[i];}
        
    printf("Average temperature: %d\n"avg/6);


                   
    /*mode*/
            
    for(i=0i<6i++){
            if(
    temp[i]== temp[i+1]){
                
    mode=temp[i];
                }
            else{
                
    mode=0;}
        }

        
    printf("Mode is: %d\n"mode);
        return 
    0;
        } 
    Could anyone show me I can make the average & mode 2 separate functions?

    Thanks!!!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I'd say something along this line.
    Code:
    #include <stdio.h>
    
    double mean(const int *array, size_t size)
    {
       double sum = 0;
       size_t i;
       for ( i = 0; i < size; ++i )
       {
          sum += array[i];
       }
       return sum / size;
    }
    
    int main(void)
    {
       int data[] = {10,21,32,43,54,65};
       double average = mean(data, sizeof data / sizeof *data);
       printf("average = %g\n", average);
       return 0;
    }
    
    /* my output
    average = 37.5
    */
    Last edited by Dave_Sinkula; 11-08-2004 at 02:03 PM. Reason: Changed some names.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You may want to hold off on the functions for a bit, and double check your mode code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's question about functions.
    By Crocodile23 in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 06:00 AM
  2. Functions Question
    By audinue in forum C Programming
    Replies: 2
    Last Post: 01-09-2009, 08:39 AM
  3. functions question.
    By Boozel in forum C Programming
    Replies: 1
    Last Post: 02-22-2008, 11:38 PM
  4. Question concerning functions
    By Warrax in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2007, 11:00 AM
  5. Question about creating flash functions
    By jbh in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2005, 08:39 AM

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