Thread: need help

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    62

    need help

    // hi iam new programmer and i have a question what does nsensors mean in the function and in the if statement

    Code:
    #include<stdio.h>
    #include<conio.h>
    float avgtemp(int temperature[], int nsensors);
    
    int main()
    {
        int tempreadings[10] = { 21, 17, 23, 26, 18, 14, 11, 19, 20, 15};
        float average;
        
        average = avgtemp(tempreadings, 10);
        printf("\nthe average temperature is %.1f degrees. ",average);
        
     getch();
     
    } 
    float avgtemp(int dummy[], int nsensors) // what does nsensors due
    
    {
     float average = 0;
     int j;
     for(j=0; j<nsensors; j++)
     average+= dummy[j]; // dont understant
     if(nsensors >0)           // dont understant
     average /=nsensors;
     
     return average;     
          
    
          
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    'nsensors' probably stands for 'number of sensors', i.e. the size of the array 'dummy'

  3. #3
    One Level Below Beginner
    Join Date
    Jul 2009
    Location
    Corner of Nowhere and Yonder
    Posts
    19
    In other words, see in the beginning of the code that says:

    float avgtemp(int temperature[], int nsensors);

    This is stating that they are a declared variable (that the programmer has made up)

    Edit:
    I just realized the previous post was not putting down the OP, for there really is a dummy array in the script....
    Last edited by Banned; 08-03-2009 at 06:57 PM. Reason: ooops

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    62
    what does nsensor ddoes

  5. #5
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    It is used to pass the size of array to a function. In this case - number of readings (10). Because when passing an array to function you actually pass a pointer to first element, separate variable for number of items required.

    Code:
    average+= dummy[j];
    is just short form of
    Code:
    average = average + dummy[j];
    It computes sum of element values.

    Code:
    if(nsensors >0)
    This is just check to see if there are at least one element.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by joker_tony View Post
    what does nsensor ddoes
    It's a friggin' argument. Read the code and figure out what the programmer intended it to be.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed