Learn C++ for Loop: Finding the Total of a Series with Example Code

  • C/C++
  • Thread starter loli12
  • Start date
  • Tags
    C++ Loop
In summary, you are working on a program that asks the user to input the number of terms they want to use for an approximation. The program will then use a for loop within a for loop to calculate and print out a table of term numbers and their corresponding approximation values. The formula used in the calculation is p = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ... and so on. The table will show the term number and the approximation value for each term, based on the user's input.
  • #1
loli12
I am learning how to use the for loop to find out the total of:
1/30 + 2/29 + 3/28 + ... + 29/2 + 30/1
and i wrote the following. but it doesn't work and give the correct total.
can you guys tell me what's wrong?
Thanks

#include <iostream>
using namespace std;
int main ()
{
int num, den; //numerator, denominator
double total=0;

for (num = 1, den = 30; num < 31; num++, den--)
total += (num/den);
cout << "Total is " << total<< endl;
return 0;
}
 
Technology news on Phys.org
  • #2
The for loop looks fine. The problem is that you're trying to do integer arithmetic with non-integer values (fractions). Try using floats or doubles instead.

- Warren
 
  • #3
The problem is that the answer of num/den would be taken as an interger, then put into a double. So it would convert 0 into...0. The easiest solution would be to declare den as a float (or double).
 
  • #4
OH! I see.. THanks both of you guys!
 
  • #5
You can still keep the integers, but use static casting to convert the result of division to double without truncation:

Code:
total += static_cast< double >( num ) / den;

den, being an integer will be coerced to a double by the compiler.
 
Last edited:
  • #6
This will correct the problem for you!

#include<stdio.h>

#define POUNDS_PER_KILO 2.2406
#define OZ_ALCOHOL_PER_DRINK 0.54
#define PERCENT_WATER_MALE 0.58
#define PERCENT_WATER_FEMALE 0.49
#define GRAMS_ALC_PER_OZ 23.36
#define PERCENT_WATER_BLOOD 0.806
#define METABOLISM_RATE 0.012

int main(void)
{

float numDrinks, weight, bac, mlWater, gramsAlc, alcPerMlWater, alcPerMlBlood, timeDrinking;
char gender = 'c';

printf("Welcome to the Percent Blood Alcohol Calculator!\n\n");
printf("Enter the number of drinks you've had so far: ");
scanf("%f", &numDrinks);

printf("How many hours ago did you start drinking: ");
scanf("%f", &timeDrinking);

printf("Enter your gender(m or f): ");
while((gender!='m')&&(gender!='M')&&(gender!='f')&&(gender!='F')) {

scanf("%c", &gender);
}

printf("How much do you weigh (yes, your real weight): ");
scanf("%f", &weight);

if (gender == 'M' || gender == 'm') {
mlWater = weight/POUNDS_PER_KILO * PERCENT_WATER_MALE*1000;
}else if(gender=='F' || gender == 'f') {
mlWater = weight/POUNDS_PER_KILO * PERCENT_WATER_FEMALE*1000;
}

gramsAlc = numDrinks*OZ_ALCOHOL_PER_DRINK*GRAMS_ALC_PER_OZ;
alcPerMlWater = gramsAlc / mlWater;
alcPerMlBlood = alcPerMlWater * PERCENT_WATER_BLOOD;
bac = alcPerMlBlood * 100;
bac = bac - METABOLISM_RATE * timeDrinking;

printf("Your BAC is: %f\n", bac);

printf("\n**Discalimer: The information from this calculator is only an estimate**\n");
printf(" In fact, you're probably not the least bit inebriated, go have five more.\n");

return 0;
}
 
  • #7
can anyone help

im having trouble with the for loop within the for loop.

i have to to write a program that asks the user to enter the amount of terms they would like to use for an approximatoin. then print out a table that shows term number and approximation.
the formula is p = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ....and so on.
the table should look like this. if the user enters 5,


Term Approximation Value
1 x.xxxxxx
2 x.xxxxxx
3 x.xxxxxx
4 x.xxxxxx
5 x.xxxxxx
 

FAQ: Learn C++ for Loop: Finding the Total of a Series with Example Code

What is a for loop in C++?

A for loop in C++ is a control flow statement that allows for the repeated execution of a block of code. It consists of three parts: initialization, condition, and increment/decrement. The loop will continue to execute until the condition is no longer true.

How do you use a for loop to find the total of a series in C++?

To find the total of a series using a for loop in C++, you can initialize a variable to hold the total, set the condition to iterate through the series, and use the increment/decrement to move through the series. Inside the loop, you can add each element of the series to the total variable. Once the loop is complete, the total variable will contain the sum of the series.

Can you provide an example code for using a for loop to find the total of a series in C++?

Yes, the following code snippet shows an example of using a for loop to find the total of a series in C++:

int total = 0; // variable to hold the total

for (int i = 1; i <= 10; i++) // loop through the series from 1 to 10

{

total += i; // add each element of the series to the total

}

// total will now contain the sum of the series (55)

What is the advantage of using a for loop to find the total of a series in C++?

The advantage of using a for loop to find the total of a series in C++ is that it allows for a concise and efficient way to iterate through a series of elements. It also eliminates the need for writing repetitive code, as the loop will continue to execute until the desired condition is met.

Are there any common errors to watch out for when using a for loop to find the total of a series in C++?

One common error to watch out for when using a for loop to find the total of a series in C++ is an infinite loop. This can occur if the condition is not properly set or if the increment/decrement is not written correctly. It is important to carefully check the logic of the loop to ensure it will terminate at some point.

Similar threads

Replies
22
Views
3K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
39
Views
4K
Replies
1
Views
1K
Replies
10
Views
2K
Back
Top