473,403 Members | 2,359 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,403 software developers and data experts.

Could someone explain this for me?

Hi, ive been reading these forums for a while and just now after reading something that makes no sense to me I thought I might as well ask on here for some help.

Basically I was reading through a textbook example showing how to determine if numbers are odd or even and it basically dosent explain how it works, just tells you that it does.

What is really bugging me, and I cant get my head round it is why does it state int (i) and then when it does modulus uses int a. Also the line before the while statement has even (i) - why is that and what does it do?
And is this an example of what is known as passing the integers to function even? - I was trying to find an example of a program doing that and I think this is.

I typed it up and its like this...


#include <iostream>
using namespace std;


void even (int a);

int main ()
{
int i;
do {
cout << "Type a number (0 to exit): ";
cin >> i;
even (i);
} while (i!=0);
return 0;
}

void even (int a)
{
if ((a%2)==0) cout << "Number is even.\n";
else cout << "odd" << endl;
}
Nov 29 '07 #1
8 1397
What is really bugging me, and I cant get my head round it is why does it state int (i) and then when it does modulus uses int a. Also the line before the while statement has even (i) - why is that and what does it do?
The reason is: variable name in the function prototype must not necessarily match the variable name of the argument;please read your book carefully and don't jump immediately to topics that you are not enough trained for.
Nov 29 '07 #2
Well I know that as that's why the text book uses it.

Could someone help me and explain how this program works in a few words?

Also am I right in thinking this a case of "passing integers to function even" - or is that something else?
Nov 29 '07 #3
Airslash
221 100+
Beeing a student myself, i think i can help you without breaking forum rules. I will report some of the code and explain with steps what each part does. And why it does that.

Expand|Select|Wrap|Line Numbers
  1. int main ()
  2. {
  3.      int i;
  4.      do {
  5.           cout << "Type a number (0 to exit): ";
  6.           cin >> i;
  7.           even (i);
  8.      } while (i!=0);
  9.      return 0;
  10. }
  11.  
Ok this is the main method of your 'program'.
The first line int i; declares a new varialbe of the type integer. It has no value assigned yet, but thats not important right now.

The second line begins the while-loop. This is a reversed loop because you start with the do part, and perform the check at the end. Why this is done should be obvious from my previous point, if not you really should grab you c++ book again.

Inside the loop you ask the user for a number. You store that number inside the variable you declared. After storing the variable, you call the function even and pass it the variable you stored. So far still basic things.

After processing the even function, you check the conditition of your while loop, and repeat the process if the condition is met.

Expand|Select|Wrap|Line Numbers
  1. void even (int a)
  2. {
  3. if ((a%2)==0) cout << "Number is even.\n";
  4. else cout << "odd" << endl;
  5. }
  6.  
This is a basic function declaration in C++. A function declaration has nothing to do with the implementation. What we do in this snippet, is we declare a new function called even which accepts a parameter of the type integer and returns nothing because the returntype is set to void. The syntax in C++ for declaring functions is the following :
Expand|Select|Wrap|Line Numbers
  1. returntype funcname (parameters) { code }
  2.  
The code that is done inside the function body should be easy to understand.

My typing this piece of text, makes me wonder if you are already up to function definitions and passing arguments to functions.


anyways I hope it helps you.
Nov 29 '07 #4
Thats mint, thanks for the explanation, I really understand now. They should have had that in the textbook! Im up on my function definitions and have gone through quite a few programs its just he slightest new thing that i havent come across throws me, when it shouldn't.

Thanks again
Nov 29 '07 #5
Ganon11
3,652 Expert 2GB
Well, the modulus division operator (%) divides the two numbers, but returns the remainder. So, for example, 32 % 5 results in 2, because 5 divides into 30 6 times with a remainder of 2.

It is important to note that when you perform the general calculation a % b, there are b possible answers. Returning to our above example, these 5 answers are 0, 1, 2, 3, and 4. In general, the possible answers for a % b are all integers between 0 and b - 1, inclusive. (Test this out by manually seeing what 30 % 5 is, as well as 31 % 5, 32 % 5, 33 % 5, 34 % 5, 35 % 5, and 36 % 5).

Now we return to the original problem - how does a % 2 help us figure out if it is even or odd? Well, in this case, b == 2, so there are 2 possible answers (what are they?). There are also two possibilities for the number a - it is even, or it is odd. So that makes sense.

Now, what do all even numbers share in common? 24, 1056, 900000000002, and 6 (all even numbers) are all evenly divisible by 2. This means that, when they are divided by 2, an integer results (i.e. there is no remainder). In general, any even number e, when divided by 2, leaves no remainder. We can check this using modulus division! If a % 2 is 0 (there is no remainder), then 2 divides evenly into a; thus, a is even.

It is easy to see that, if a % 2 is not 0, then the number must be odd.
Nov 29 '07 #6
in main you create a variable whose name i,and then you pass this variable to even ,which creates its own copy of "i" and calls it "a"(don't get stuck to this too much,just think that a procedure copy and rename the value that are passed into it),then it makes the computatione needed to find out wether the number is prime or not.
Nov 29 '07 #7
Many thanks again guys, thats really helpful.

Its falling into place with the things ive learn't, nice one
Nov 29 '07 #8
weaknessforcats
9,208 Expert Mod 8TB
Code: ( text )
void even (int a)
{
if ((a%2)==0) cout << "Number is even.\n";
else cout << "odd" << endl;
}


This is a basic function declaration in C++. A function declaration has nothing to do with the implementation. What we do in this snippet, is we declare a new function called even which accepts a parameter of the type integer and returns nothing because the returntype is set to void. The syntax in C++ for declaring functions is the following :

Code: ( text )
returntype funcname (parameters) { code }
Not a bad explanation but I want to add that there is a difference between a declaration and a definition. A declaration says a thing exists. A definition allocates memory for it.

On that basis what you call a declaration is, in fact, the function definition. That is, the function definition and the function implementation are the same thing. The function declaration is the function prototype.
Nov 29 '07 #9

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

Similar topics

13
by: C++fan | last post by:
The following code is for list operation. But I can not understand. Could anyone explain the code for me? /* * List definitions. */ #define LIST_HEAD(name, type) struct name { type...
21
by: Gactimus | last post by:
Can anyone explain what the lines with the '*' by them do? ----------- #ifndef _COUNTER_H #define _COUNTER_H #include <iostream> using namespace std; class Counter
3
by: MarcJessome | last post by:
Hi, I was wondering if someone could help me through learning C++. I've tried learning before, but I find I would work better if I had someone that could help explain a few things to me. Im using...
2
by: Anastassios Giannaras | last post by:
Hello I have made an small access Database that is used by 4-5 people every day. Very often through the users get the message "Could Not Update; Currently Locked" There is an entry about that...
4
by: konsu | last post by:
hello, my client code communicates with a web service through a https connection. the code has a stub derived from SoapHttpClientProtocol. It also sets a certificate policy handler that allows...
2
by: blueyonder | last post by:
The statament below does exactly what I want it to do but I don't understand why? In my mind the subquery produces a result set which is a subset of the handset table which the initial part of...
8
by: Bart | last post by:
Could someone explain me what is wrong with this code ? I gives me a compile error: Error 1 Use of unassigned local variable 'fileStreamObject' C:\Documents and Settings\Bart\Local...
2
by: patrice.pare | last post by:
Hello, Here is a summary of my Dev Environment: I use Visual Studio 2005 Team Suite SP1 with Crystal Report XI SP1 on a Windows XP SP2 development workstation. I also use SQL Server 2000 SP4. ...
6
by: Dave Young | last post by:
I'm looking at some code that i've inherited and I'm not really familar with what's going on here and was hoping somone could explain it to me. For reference: f1 is a long f2 is a long ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.