473,795 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Functions

16 New Member
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 1959
Ganon11
3,652 Recognized Expert Specialist
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
queenma7
16 New Member
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 Recognized Expert Moderator Expert
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
queenma7
16 New Member
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
queenma7
16 New Member
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 Recognized Expert Moderator Expert
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 Recognized Expert Moderator Expert
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
3349
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 charcters in it, that the code needs to run until all bad characters are gone? For example, if a file has the name "<bad*mac\file" the program has to run 3 times to get all three bad chars out of the file name. The passes look like this:
99
5926
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 important changes, so in this thread I would like to focus on that issue alone. I have edited the Proposed Syntax example below to take out the changes unecessary to this discussion. I left in the change of "instance variable" syntax (...
21
746
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 read many authors (and it's my belief, too) who discourage the use of the 'inline' keyword, because: - The 'inline' word only advice the compiler about wich functions should be expanded, but not force it to expand them. Also, the compiler can...
17
3629
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 deep. In that case you need much more ram than a PC has to store functions calculated in loops so that you do not have to recalculate every time you cycle through the nest of loops. Using a HD for storage to extend ram is much too slow for many...
2
3792
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 produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently, Wang, Yu, and Lin showed a short- cut solution for finding collisions in SHA-1 . Their result
7
5907
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 route. The problem being that I can't call these functions from the query designer in Access. I decided that I would try the route of declaring the functions from the dll file the same way you would for the Windows API. Access then complains that...
23
4023
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 who will find them very helpfull. gcc has them as a non-standard option, but only when compiling C language code, so I'm afraid there might be some obscure reason why local functions are not so easy to be dealt with in C++, which I do not yet know.
14
4212
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
7
3974
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 functions. Is it normal how C++ Compiler can compile large class without any problem? Didn't C++ Compiler have rules to limit the number of member functions? One big object has complex operations how member variables and member functions can be...
6
9615
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 serialization The code itself generates a list of strings comprised of random numbers. No number will be repeated within a string, and no string will be repeated within the list of strings. Following the code is a brief discussion of how the above...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.