473,320 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.
Oct 25 '06 #1
7 1921
Ganon11
3,652 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. int CountIt(int myInt) {
  2.    int sum = 0;
  3.    for (loop header here; from 1 to 10) {
  4.       int newNum;
  5.       input into newNum;
  6.       if (newNum is less than myInt) increment sum;
  7.    }
  8.    return sum;
  9. }
Good luck!
Oct 25 '06 #2
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:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. int CountIt(int myInt) {
  2.    int sum = 0;
  3.    for (loop header here; from 1 to 10) {
  4.       int newNum;
  5.       input into newNum;
  6.       if (newNum is less than myInt) increment sum;
  7.    }
  8.    return sum;
  9. }
Good luck!
that's what i couldnt figure out. how do i pass in an argument?
Oct 26 '06 #3
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    int result;
  4.    int limit = 5;
  5.  
  6.    result = CountIt(limit);
  7. }
  8.  
Oct 26 '06 #4
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    int result;
  4.    int limit = 5;
  5.  
  6.    result = CountIt(limit);
  7. }
  8.  
why did you set limit equal to 5?
Oct 26 '06 #5
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.
Oct 26 '06 #6
Banfa
9,065 Expert Mod 8TB
why did you set limit equal to 5?
No reason, it just had to be initialised to something.
Oct 26 '06 #7
Banfa
9,065 Expert Mod 8TB
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.
Oct 26 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
21
by: Rubén Campos | last post by:
I haven't found any previous message related to what I'm going to ask here, but accept my anticipated excuses if I'm wrong. I want to ask about the real usefulness of the 'inline' keyword. I've...
17
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
7
by: Tim ffitch | last post by:
Hi I have created a VB dll file that contains common functions I use across various projects in VB, Access and Excel. Rather than have to code the functions in each I decided to use the dll...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
6
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.