Thread: Program on Functions

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    14

    Program on Functions

    I hope someone can help me on an assignment I was given. I have asked around on other forums, but no one has an answer. I have to create a program with the following information:

    Create a program with the following variables, all created in main:
    a const short, shMAX containing the value 10
    a short, shCnt
    an array of long containing 10 elements, lArr
    a double variable to store the answer returned by functions you write, dAnswer
    a string containing your name, sName
    a 2 dimensional array of double, 2 rows and 3 columns, d2Arr

    Array data does not need to be initialized when the array is defined. Exception is the string with your name in it, put the data into the string when you define sName.

    In main we will always store the values returned by non void functions (except the Highest, #3 below) in the variable provided for answers, dAnswer, so the form of all function calls, except void functions, will be:

    dAnswer = functionname( .....

    The variable will then be printed to the screen using a function called PrintData, which you create below. An example is below:

    PrintData( "Average1 returned" , dAnswer);

    PrintData and dAnswer are not used for void functions.

    Before creating any of the functions below create the function PrintData with the following prototype:

    void PrintData( char *sLabel, double dAnswer );
    or
    void PrintData( char sLabel[], double dAnswer );


    Create the following functions and call each from main:

    1. FillArray - It will receive the lArr array, an array of long integers, from main and a count of the number of elements in the array. A random number will be assigned to each element of the array in a loop. Use rand(). This function has no return value, a void function.

    2a. Average1 - It will receive the array lArr and a count of the items. It will add up the total and then return the average as a double. Use subscript notation throughout. In main call the function PrintData to print the average returned. This function receives 2 arguments and returns a double.

    2b. Average2 - It will receive the array and a count of the items. It will add up the total and then return the average as a double. Use pointer notation throughout. In main print the average using PrintData. This function receives 2 arguments and returns a double.


    I think my problem is int the Print Data function. I do not understand why there is a char in the function. Here is the code I have so far:

    Code:
    #include <iostream>
    #include <string>
    #include <ctime>
    
    //prototype functions
    void PrintData(char sLabel[], double dAnswer);
    void FillArray(long array[], long size);
    double Average1(long array[], long size);
    
    using namespace std;
    
    int main()
    {
        //variables used
        const short shMAX  = 10;
        short shCnt;
        long lArr[10];
        double dAnswer;
        string sName = "bob";
        double d2Arr[2][3];
    
        srand((unsigned)time(NULL));
    
    
    //FillArray called
    FillArray(lArr, 10);
    
    dAnswer = Average1(lArr, 10);
    
    PrintData("lArr", dAnswer);
    
    
        system("pause");
        return 0;
    }
    
    void PrintData(char sLabel[], double dAnswer)
    {
        for(int count = 0; count < dAnswer; count++)
        {
            cout << sLabel[count] << " ";
        }
    }
    
    
    //#1. FillArray - It will receive the lArr array, an array of long integers, 
    //from main and a count of the number of elements in the array. 
    //A random number will be assigned to each element of the array in a loop. 
    //Use rand(). This function has no return value, a void function.
    void FillArray(long array[], long size)
    {
        for(int count = 0; count < size; count++)
        {
            array[count] = rand();
        }
    }
    
    
    //#2a Average1 - It will receive the array lArr and a count of the items. 
    //It will add up the total and then return the average as a double. 
    //Use subscript notation throughout. In main call the function PrintData 
    //to print the average returned. This function receives 2 arguments and returns a double.
    double Average1(long array[], long size)
    {
        double total = 0.0;
        double average;
    
        for(int count = 0; count < size; count++)
        {
            total += array[count];
        }
    
        average = total / size;
    
        return average;
    
    }
    If anyone can tell me what I am missing I would appreciate it.

    Thanks,
    R.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You should NOT have the literal 10 instead use a define or a constant of 10.

    Edit1: Get rid of the for loop inside "PrintData" function.
    Display/print both of the items passed to the function "PrintData".

    Tim S.
    Last edited by stahta01; 04-10-2014 at 04:24 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    14
    Thanks Tim for a quick reply. If I change the 10 to a constant the code stills does not run right. I think It hast to do with the char in the print function.

    R.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Did you fix the "PrintData" function?

    Quote Originally Posted by stahta01 View Post

    Edit1: Get rid of the for loop inside "PrintData" function.
    Display/print both of the items passed to the function "PrintData".

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Apr 2014
    Posts
    14
    I think I got the program to work. This is what I have for the PrintData function:
    Code:
    void PrintData(char sLabel[], double dAnswer)
    {
    
            cout << *sLabel << dAnswer << endl;
    }
    I am not sure I understand how it works but it works. If someone can explain it to me I would appreciate it.

    Thanks!
    R.

  6. #6
    Registered User
    Join Date
    Apr 2014
    Posts
    14
    When the PrintData function prints out the Average from the average function the number is printed out with a lower case L next to the average number. For example: l9.9 instead of 9.9.

    I have added a second PirntData function to see the ten random numbers before they are averaged out.

    Here is my new code:
    Code:
    #include <iostream>
    #include <string>
    #include <ctime>
    
    //prototype functions
    void PrintData(char sLabel[], double dAnswer);
    void PrintData(long sLabel[], double size);
    void FillArray(long array[], long size);
    double Average1(long array[], long size);
    
    
    
    
    using namespace std;
    
    int main()
    {
        //variables used
        const short shMAX  = 10;
        short shCnt;
        long lArr[10];
        double dAnswer;
        string sName = "bob";
        double d2Arr[2][3];
    
        srand((unsigned)time(NULL));
    
    
    //FillArray called
    FillArray(lArr, 10);
    
    dAnswer = Average1(lArr, 10);
    
    PrintData(lArr, 10);
    
    PrintData("lArr", dAnswer);
    
    
        system("pause");
        return 0;
    }
    
    void PrintData(char sLabel[], double dAnswer)
    {
    
            cout << *sLabel << dAnswer << endl;
    }
    
    void PrintData(long sLabel[], double size)
    {
        for(int cnt = 0; cnt < size; cnt ++)
        {
            cout << sLabel[cnt] << " ";
        }
        cout << endl;
    }
    
    
    //#1. FillArray - It will receive the lArr array, an array of long integers, 
    //from main and a count of the number of elements in the array. 
    //A random number will be assigned to each element of the array in a loop. 
    //Use rand(). This function has no return value, a void function.
    void FillArray(long array[], long size)
    {
        for(int count = 0; count < size; count++)
        {
            array[count] = rand()%20 +1;
        }
    }
    
    
    //#2a Average1 - It will receive the array lArr and a count of the items. 
    //It will add up the total and then return the average as a double. 
    //Use subscript notation throughout. In main call the function PrintData 
    //to print the average returned. This function receives 2 arguments and returns a double.
    double Average1(long array[], long size)
    {
        double total = 0.0;
        double average;
    
        for(int count = 0; count < size; count++)
        {
            total += array[count];
        }
    
        average = (double)total / size;
    
        return average;
    
    }
    Can someone explain why I get a lower case L with the output.

    Thanks,
    R.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    PrintData(" ", dAnswer);

    Try that

  8. #8
    Registered User
    Join Date
    Apr 2014
    Posts
    14
    I have to have some type of string in the PrintData function. Something like: PrintData( "Average1 returned" , dAnswer);
    I put a loop in the function statement and it seem to work. Here is the code:
    Code:
    void PrintData(char sLabel[], double dAnswer)
    {
        for(int cnt = 0; cnt < 8; cnt++)
        {
            cout << sLabel[cnt];
        }
    
        cout << " " << dAnswer << endl;
    }
    I will keep playing with the code.

    Thanks for your help.
    R.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    void PrintData(char sLabel[], double dAnswer)
    {
        for(int cnt = 0; cnt < 8; cnt++)
        {
            cout << sLabel[cnt];
        }
     
        cout << " " << dAnswer << endl;
    }
    Equivalent to:
    Code:
    void PrintData(char sLabel[], double dAnswer)
    {
        cout << sLabel << " " << dAnswer << endl;
    }
    Even better would be if you used std::string instead of char arrays.
    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

Similar Threads

  1. Can functions be used too much in a program?
    By Cron in forum C Programming
    Replies: 9
    Last Post: 06-29-2013, 11:20 PM
  2. Need help starting a program with functions
    By Neotriz in forum C Programming
    Replies: 7
    Last Post: 11-01-2009, 05:45 PM
  3. c program help - functions
    By Sleepwalker817 in forum C Programming
    Replies: 4
    Last Post: 04-27-2008, 04:32 AM
  4. Help please: program using functions
    By ZeroBurn7 in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2005, 05:38 PM
  5. Help with program-functions
    By akalvarado in forum C Programming
    Replies: 5
    Last Post: 10-13-2002, 10:36 AM