473,385 Members | 1,838 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,385 software developers and data experts.

Thank you for the help on is there a better way to write this

Never mind I went over the whole code and found my mistake, I did not use
int sumSquareOdd=0, I either did not finish it or I erased it while I was trying something else. I knew it was not keeping count correctly

now I have a couple of questions
on JonLT’s code
1. int sumSquareOdd = 0;
2.
3. for (int n = firstNum; n <= secondNum; n++)
4. {
5. sumSquareOdd += n * n * (n%2); // if n is even, 0 will be added (unchanged)
6. }
how did it know that I wanted odd numbers without (n%2==1)?

On the code sumSquareOdd += n * n * (n%2); why is += used instead of just =?
Jul 25 '07 #1
5 1072
JonLT
41
Never mind I went over the whole code and found my mistake, I did not use
int sumSquareOdd=0, I either did not finish it or I erased it while I was trying something else. I knew it was not keeping count correctly

now I have a couple of questions
on JonLT’s code
1. int sumSquareOdd = 0;
2.
3. for (int n = firstNum; n <= secondNum; n++)
4. {
5. sumSquareOdd += n * n * (n%2); // if n is even, 0 will be added (unchanged)
6. }
how did it know that I wanted odd numbers without (n%2==1)?

On the code sumSquareOdd += n * n * (n%2); why is += used instead of just =?
if n is even the statement (n%2) will be 0 (false)

foo += bar is the same as foo = foo + bar
Jul 25 '07 #2
tburger
58
As John illustrated above, the "+=" syntax will add the value that is calculated on the right side of the assignment to the existing value of the variable on the left.

This is often helpful in keeping a running total (i.e. keeping a total sales figure as you run through a large list of numbers).

For future reference, you may also use "*=" and "-=" in your equations.

Tom
Jul 25 '07 #3
I don't know how the program knew that I wanted only odd #s without (n%2==1).

see in all my other code I had to use (n%2==0) or (n%2==1)

for(n=firstNum;n<=secondNum;n++)
{
if(n%2==1)
{
cout << "The odd numbers are: " << n << endl;
}

//since I want the sum of the even number I don't need the else statement
}
for(n=firstNum;n<=secondNum;n++)
{
if(n%2==0)
sum += n;
}
cout<< "The sum of all even numbers between your two numbers: " << sum << endl;

in the above two examples I needed those statements two tell the statements to use the odd or even...........I am trying to figure out if it is taking it from one of these two statements.
Jul 25 '07 #4
tburger
58
As you know, the statement (n%2) can only produce two values: 1 if odd or 0 if even.

Therefore, the statement can essentially function as a true/false trigger in your program.

In both of the examples above, you could have just used (n%2) and it would have worked.

Let's break it down:

If the number n is 5, then (n%2) becomes (5%2). This will return a value of 1.

The statement

if(n%2)

is now seen as

if(1) .

Therefore, the program continues with the following code.

If the number were even, however, the program would see

if(0) and ignore the next "if" statments.

Tom
Jul 25 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Everyone please use code tags.
Jul 25 '07 #6

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

Similar topics

4
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
12
by: windandwaves | last post by:
Hi Folks I have just completed a project for an accommodation finder in New Zealand - much with your help - thank you again. I would appreciate any constructive or deconstructive comments. ...
2
by: ASallade | last post by:
Hello, I've scoured my books and the web, but am still daunted, hopefully some of the users in this newsgroup will have advice for my problem. I am not an experienced javascript programmer,...
5
by: Tank | last post by:
I have had this post up here when i was trying to figure out how to make leading zeros and have been able to fudge that to work. I am now have trouble getting the loop that makes the folders to...
1
by: qushui_chen | last post by:
my write code is(i know that is error),how can I do?: ------------------------------------------------------------------- ThreadArray=new Thread(new ThreadStart(this.send_write_log( ec_send_email,...
0
by: Shaggyh | last post by:
hi im needing help with a program im writing to do subnetting i was on before about it and i got some help. the code below wont work for me and i cant think of why not. i was wondering if anyone...
2
by: wei | last post by:
thank Carl. do you mean there is no good way to reach the same goal as the Response.Write does in code behind except adding a label on it?
15
by: scorpion53061 | last post by:
An issue that has plagued me for a long time was the writing of a dataset to an array and using that array to write to an excel spreadsheet. For too long I relied on writing the dataset cell by...
23
by: SaraLeePerson | last post by:
Dear group, I am seeking a easy to maintain and more importantly *working* way to pre-fetch images, so the pages I develop load smoothly without seeing the images kick in flicker as they usually...
22
by: Chuck Connors | last post by:
Hey guys. I'm working on a little program to help my wife catalog her/ our coupons. I found a good resource but need help formatting the text data so that I can import it into a mysql database....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.