Functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • queenma7
    New Member
    • Oct 2006
    • 16

    #1

    Functions

    I have to write this program this program:

    Write a function called CountIt that has as an argument an integer value. this function will ask the user to enter 10 integers, and will count the number of values entered by the user that are less than the argument passed. if the numbers are LESS, the function returns a 0.

    i know i have to write a count controlled loop, but i dont know how to do that with a function. I just need an idea of how to get started with this program. Thanks.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Write a function called CountIt that has as an argument an integer value. this function will ask the user to enter 10 integers, and will count the number of values entered by the user that are less than the argument passed. if the numbers are LESS, the function returns a 0.
    According to the function specification, there will be 2 steps of user input, both in the main() calling CountIt(int) and in the actual function. Once the user inputs a value into an integer value (say, num), you call CountIt(num). Since CountIt(int) returns an integer value, you should store this in a new int variable (say, total). Then just output the total with an appropriate message.

    As for the actual function, you must first declare an integer variable (sum) and initialize it to zero (int sum = 0;). You may have luck with a for() loop, looping from 1 to 10, inclusive. Such a loop header would look like this:

    Code:
    for (int i = 1; i <= 10; i++) {
    Inside your loop, define a new integer variable. Ask the user to input a value into this variable. After this, compare the new number to the argument passed to CountIt(int). If the new number is less, increment sum.

    Once the loop has finished, return sum. Pseudocode will look like this:

    Code:
    int CountIt(int myInt) {
       int sum = 0;
       for (loop header here; from 1 to 10) {
          int newNum;
          input into newNum;
          if (newNum is less than myInt) increment sum;
       }
       return sum;
    }
    Good luck!

    Comment

    • queenma7
      New Member
      • Oct 2006
      • 16

      #3
      Originally posted by Ganon11
      According to the function specification, there will be 2 steps of user input, both in the main() calling CountIt(int) and in the actual function. Once the user inputs a value into an integer value (say, num), you call CountIt(num). Since CountIt(int) returns an integer value, you should store this in a new int variable (say, total). Then just output the total with an appropriate message.

      As for the actual function, you must first declare an integer variable (sum) and initialize it to zero (int sum = 0;). You may have luck with a for() loop, looping from 1 to 10, inclusive. Such a loop header would look like this:

      Code:
      for (int i = 1; i <= 10; i++) {
      Inside your loop, define a new integer variable. Ask the user to input a value into this variable. After this, compare the new number to the argument passed to CountIt(int). If the new number is less, increment sum.

      Once the loop has finished, return sum. Pseudocode will look like this:

      Code:
      int CountIt(int myInt) {
         int sum = 0;
         for (loop header here; from 1 to 10) {
            int newNum;
            input into newNum;
            if (newNum is less than myInt) increment sum;
         }
         return sum;
      }
      Good luck!
      that's what i couldnt figure out. how do i pass in an argument?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Code:
        int main()
        {
           int result;
           int limit = 5;
        
           result = CountIt(limit);
        }

        Comment

        • queenma7
          New Member
          • Oct 2006
          • 16

          #5
          Originally posted by Banfa
          Code:
          int main()
          {
             int result;
             int limit = 5;
          
             result = CountIt(limit);
          }
          why did you set limit equal to 5?

          Comment

          • queenma7
            New Member
            • Oct 2006
            • 16

            #6
            Originally posted by queenma7
            why did you set limit equal to 5?
            i know the user as to input a value. i will compare other values to this value. what i dont understand, is in int main() once i declare a variable and ask the user to input a number. how do i then compare this number to subsequent numbers the user will input. how do i call the function from main?

            Desperatly trying to understand. Thanks for the replies.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by queenma7
              why did you set limit equal to 5?
              No reason, it just had to be initialised to something.

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by queenma7
                how do i call the function from main?
                The code I posted gives an example of the answer to this exact question.

                If you still need to ask it then do so with reference to the code.

                Comment

                Working...