473,320 Members | 1,876 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.

Looping Sums

Hi I am very very new to C++ i am trying to create a loop that reads 2 integers and addsall the intergers between. I am very very lost!
#include <iostream>
using namespace std;

int main()
{
int num1=10;
int num2=0;
int total;

while (num2<10)
{ total= num2+ 1;
total++;
}
system ("pause");
return 0;
}
Apr 1 '07 #1
9 2436
Savage
1,764 Expert 1GB
Hi I am very very new to C++ i am trying to create a loop that reads 2 integers and addsall the intergers between. I am very very lost!
#include <iostream>
using namespace std;

int main()
{
int num1=10;
int num2=0;
int total;

while (num2<10)
{ total= num2+ 1;
total++;
}
system ("pause");
return 0;
}
Hi,this will start looping "when hell freezes over",because you are not increasing num2.
Apr 1 '07 #2
JosAH
11,448 Expert 8TB
So you want to sum m + (m+1) + (m+2) + ... + (n-2) + (n-1) + n?
Say for m=2 and n=6 you want to find 2+3+4+5+6?

There's a centuries old nice closed form formula available:

0+1+2 + ... + n= n*(n+1)/2

You don't want to count starting from 0, you want to count starting at m,
so the values 0, 1, 2, .. m-1 should be substracted again: that last sum,
using that same old formula again equals:

0+1+2+ ... + m-1 = (m-1)*m/2

So your sum for any m <= n (both nonnegative) equals:

n*(n+1)/2-(m-1)*m/2

We don't need no steenkin' loops ;-)

kind regards,

Jos
Apr 1 '07 #3
OK so I suck! I have only been doing this for 3 weeks.
Apr 1 '07 #4
ps thanks jos
Apr 1 '07 #5
JosAH
11,448 Expert 8TB
ps thanks jos
You're welcome of course. As you saw: there's no need for loops here. If your
assignment text tells you to use loops, simply add the numbers
m, (m+1), (m+2) ... (n-2), (n-1), n together; use a loop counter from m to n and
initialize a sum variable to zero before you start the loop.

kind regards,

Jos
Apr 1 '07 #6
ok this is exactly what my text says,...Write a C++ program with a loop structure that reads in 2 intergers, calculates the sum of all the intergers between thee 2 intergers (not including these 2 intergers) if any, and displays the final result. For example, if the 2 intergers ou read are -2 and 5, then your program should output 9 (=-1+0+1+2+3+4).


Sakinah :-)
Apr 1 '07 #7
Ganon11
3,652 Expert 2GB
OK, you can use a for...loop here if you'd like. The initial condition will set your index equal to num1 + 1, the end condition will be while the index is less than num2, and you will increment the index in each update. Inside the loop, add the index to the total.
Apr 1 '07 #8
Savage
1,764 Expert 1GB
ok this is exactly what my text says,...Write a C++ program with a loop structure that reads in 2 intergers, calculates the sum of all the intergers between thee 2 intergers (not including these 2 intergers) if any, and displays the final result. For example, if the 2 intergers ou read are -2 and 5, then your program should output 9 (=-1+0+1+2+3+4).


Sakinah :-)
First and first did you have to input numbers or are the numbers declared?

Find the diferance between those numbers:

5-(-2)=7,and lower it by two becasue you are not including limits of interval.

This will be 5,
so this is it:

<Code has been removed. Please review our FAQ Posting Guidelines>

Regards,Savage!!!
Apr 1 '07 #9
thanks a bunch :-)
Apr 1 '07 #10

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

Similar topics

2
by: guyzdancin | last post by:
I have developed a parsing program to handle large csv files and compute sums. The program was developed and successfully tested using only String objects. I want replace String objects with ...
9
by: rinmanb70 | last post by:
I have a table of transactions, some with past dates, some dated the current date, and some dated in the near future. On a report, I'm looking for a way to get four different sums using the...
8
by: DrewKull | last post by:
Hello, I have a query which sums check boxes (so 0 or -1) then Abs the sum so i can get the number of check box 'checked' per column... based on start and end date. SELECT Abs(Sum(CommCon)) AS ,...
3
by: tpdinh | last post by:
I have to evaluate the sum in the for-loop. How do i do that? Also, I need to print the partial sums using %e. Please help!
5
chunk1978
by: chunk1978 | last post by:
hey... can anyone show me how to calculate sums to the 2nd decimal only? like: 5.50 + 4.00 = 9.50 i seriously have zero idea and could defo use some help...thanks! <script...
8
by: RJMac | last post by:
I’m using ADO 2.6 in VB6 to read in data from a Union query (qryJoinBalanceData) in Access 2000. The Union query contains 3 sub-queries, each of which joins several tables, but they all generate...
1
by: wisemen | last post by:
I have a table with 2 columns, and .I want to run a query that will give me a cumulative sum of the no. of entries that have an <= and on the second column give me a cumulative sum of the no. of...
20
by: Ifoel | last post by:
Hi all, Sorry im beginer in vb. I want making programm looping character or number. Just say i have numbers from 100 to 10000. just sample: Private Sub Timer1_Timer() if check1.value= 1...
1
by: someusernamehere | last post by:
Hey people, I need to do some sistem in PHP and MySQL, I have do it a part but the other is the problem, basically is mathemetical operations, te user register something and after insert some...
3
by: theinsider2012 | last post by:
JAVA for Windows XP the question is: Write an application that shows the sum of 1 to n for every n from 1 to 50. That is, the program prints 1 (the sum of 1 alone), 3 (the sum of 1 and 2), 6...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
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: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.