Need to understand how to add values in a loop - c++

#include <iostream>
using namespace std;
int main()
{
int numint, sum, a, b, ctr;
cout << "Enter the number of intervals: ";
cin >> numint;
ctr = 0;
while (ctr != numint)
{
cout << "Enter the lower and upper bounds of the interval: ";
cin >> a;
cin >> b;
ctr++;
}
cout << "The sum of the " << numint << " intervals is: [" << a << "," << b << "]";
return 0;
}
I need to add the upper bounds of the interval (a) and lower bounds of the interval (b). This depends on the amt of intervals there are.

here:
cout << "The sum of the " << numint << " intervals is: [" << a << "," << b << ]";
you are not adding the values but instead just printing them
instead use some accumulator variable and do the math before printing it, like:
int main()
{
int numint,sum,a,b,ctr;
int result = 0;
cout << "Enter the number of intervals: ";
cin >> numint;
ctr=0;
while (ctr!=numint)
{
cout << "Enter the lower and upper bounds of the interval: ";
cin >> a;
cin >> b;
ctr++;
}
result = a + b ;
cout << "The sum of the " << numint << " is: " << result;
return 0;
}

As for what I understood from the question, you first need a int resultA=0 and another for b, and keep adding in the loop:
#include <iostream>
using namespace std;
int main()
{
int numint,sum,a,b,ctr;
int result = 0;
int resultA=0;
int resultB=0;
cout << "Enter the number of intervals: ";
cin >> numint;
ctr=0;
while (ctr!=numint)
{
cout << "Enter the lower and upper bounds of the interval: ";
cin >> a;
cin >> b;
resultA+=a;
resultB+=b;
ctr++;
}
result = resultA + resultB ;
cout << "The sum of the " << numint << " is: " << result;
return 0;
}

I think this is what you are looking for:
#include <iostream>
using namespace std;
int main() {
int numberOfIntervals;
int lowerBound;
int upperBound;
int counter;
int lowerBoundSum = 0;
int upperBoundSum = 0;
cout << "Enter the number of intervals: ";
cin >> numberOfIntervals;
counter = 0;
while (counter != numberOfIntervals) {
cout << "Enter the lower and upper bounds of interval "
<< counter << ": ";
cin >> lowerBound;
cin >> upperBound;
lowerBoundSum += lowerBound;
upperBoundSum += upperBound;
counter++;
}
cout << "The sum of the lower bounds is " << lowerBoundSum
<< endl;
cout << "The sum of the upper bounds is " << upperBoundSum
<< endl;
return 0;
}

Related

Only negative numbers adding correctly

So I'm trying to write a program that will read in ten whole numbers and outputs the sum of all numbers greater than zero, the sum of all numbers less than zero,
and the sum of all numbers, whether positive, negative, or zero.
Currently only the total sum and negative sum is adding correctly. My positive sum is always 1 or 0. Any tips to get me in the right direction would help.
Current Code:
#include <iostream>
using namespace std;
int main()
{
int N = 0;
int sum = 0;
int positiveSum = 0;
int negativeSum = 0;
cout << "Number 1" << endl;
cin >> N;
cout << "Number 2" << endl;
cin >> N;
cout << "Number 3" << endl;
cin >> N;
cout << "Number 4" << endl;
cin >> N;
cout << "Number 5" << endl;
cin >> N;
cout << "Number 6" << endl;
cin >> N;
cout << "Number 7" << endl;
cin >> N;
cout << "Number 8" << endl;
cin >> N;
cout << "Number 9" << endl;
cin >> N;
cout << "Number 10" << endl;
cin >> N;
for(int i=0;i<10;i++)
{
if (N >= 0 )
{
positiveSum += N;
}
else
{
negativeSum += N;
}
}
sum = positiveSum + negativeSum;
cout << "The positive sum is= " << positiveSum << endl;
cout << "the negative sum is= " << negativeSum << endl;
cout << "The total sum is= " << sum << endl;
return 0;
}
After entering all Ns and when entring to the loop, you have only the last N. because after assigning the first number to N you don't process the value but you assigning the next value to N again after asking the user to enter it and so on. Use something like the following
#include <iostream>
int main()
{
int N = 0;
int sum = 0;
int positiveSum = 0;
int negativeSum = 0;
for(int i=0;i<10;i++)
{
std::cout << "Number "<<i + 1<< std::endl;
std::cin >> N;
if (N >= 0 )
{
positiveSum += N;
}
else
{
negativeSum += N;
}
}
sum = positiveSum + negativeSum;
std::cout << "The positive sum is= " << positiveSum << std::endl;
std::cout << "the negative sum is= " << negativeSum << std::endl;
std::cout << "The total sum is= " << sum << std::endl;
return 0;
}
And see Why is "using namespace std;" considered bad practice?

Use of do–while loop to perform the operation as many times as user want

I'm developing a program in C++ that calculate the factorial of a given number. Use for loop to calculate the factorial & do–while loop to perform the operation as many times as user want
I wrote a code for factorial with for loop But not able that how I use Do while loop for performing
The code is following
int n;
int factorial = 1;
cout << "Enter a positive integer: ";
cin >> n;
for(int i = 1; i <=n; ++i)
{
factorial *= i; // factorial = factorial * i;
}
cout << "Factorial of " << n << " = " << factorial;
Please help me.
Try this one:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string answer;
do {
int n;
int factorial = 1;
cout << "Enter a positive integer: ";
cin >> n;
for(int i = 1; i <=n; ++i)
{
factorial *= i; // factorial = factorial * i;
}
cout << "Factorial of " << n << " = " << factorial << endl;
cout << "Would you like to do again? (type 'n' for No, anything for yes)" << endl;
cin >> answer;
cin.clear(); // Clearing the cin buffer
} while (answer != "n");
cout << "Exitting" << endl;
return 0;
}
Update (according to the request in the comment)
#include <iostream>
#include <string>
using namespace std;
int main()
{
int counter=0, amount;
cout << "How many times : ";
cin>> amount;
do {
int n;
int factorial = 1;
cout << "Enter a positive integer: ";
cin >> n;
for(int i = 1; i <=n; ++i)
{
factorial *= i; // factorial = factorial * i;
}
cout << "Factorial of " << n << " = " << factorial << endl;
counter++;
} while (counter < amount);
cout << "Completed" << endl;
return 0;
}
Here is the logic using do-while loop.
int count =0, f=1, times, n;
cout << "Enter how many times : ";
cin>> times
cout << "\n Enter number for which you want to calculate factorial : ";
cin>> n
do{
for(var i=1;i<=n;i++){
f *= i;
}
cout<< "Factorial of n is : " << f;
cout << "\n Enter next number for which you want to calculate factorial : ";
cin>> n
f = 1;
count++;
}while(count <= times);

Using a while () loop to subtract numbers from the sum

I am trying to subtract the initial value that I gave my code from the sum and then have that sum be subtracted by the initial value - 1. For example, my number is 4 so 0+1+2+3+4= 10 and then have 4 subtract from 10 to get 6, then 6 subtracted by 3 to get 3, and so on. I'm extremely new to c++ and I just need a nudge in the right direction.
#include <iostream>
using namespace std;
int a;
int b = 0;
int c = 0;
int sum = 0;
void main() {
cout << "Please give me a number" << endl;
cin >> a;
do {
if (a <= 0) {
cout << "Incorrect number. The input must be positive" << endl;
cin >> a;
}
} while (a <= b);
for (int i = 0; i <= a; i++) {
cout << i;
cout << "+";
sum += i;
}
cout << endl;
cout << "All numbers from 0 to " << a << " is " << sum << endl;
cout << "Starting with the sum of " << sum << endl;
while (a > 0) {
c = sum - a;
cout << "After subtracting " << a << ", I got the number " << c << endl;
a--;
}
#include <iostream>
using namespace std;
int a;
int b = 0;
int c = 0;
int sum = 0;
int main() {
cout << "Please give me a number" << endl;
cin >> a;
do {
if (a <= 0) {
cout << "Incorrect number. The input must be positive" << endl;
cin >> a;
}
} while (a <= b);
for (int i = 0; i <= a; i++) {
cout << i;
cout << "+";
sum += i;
}
cout << endl;
cout << "All numbers from 0 to " << a << " is " << sum << endl;
cout << "Starting with the sum of " << sum << endl;
c = sum; /*Initialize c */
while (a > 0) {
c = c - a;
cout << "After subtracting " << a << ", I got the number " << c << endl;
a--;
}
return 0;
}
#include <iostream>
using namespace std;
int a;
int sum = 0;
int main() {
cin>>a;
while(a<=0){
cin>>a;
}
for(int i = 0; i <= a; i++){ cout<<i; cout<<"+"; sum+=i;}
while(a > 0){
sum-=(a--);
}
}

Program to calculate NFL quarterback passer rating. Why is it returning 0?

Hey so the code I made should be working to calculate the passer rating for quarterbacks in the NFL. The program, however, returns a value of 0 for almost anything, unless I put ridiculously large numbers, in which case it gives 100. What's wrong with it?
#include <iostream>
using namespace std;
int main()
{
int PassCompletions;
cout << "Enter pass completions" << endl;
cin >> PassCompletions;
int PassAttempts;
cout << "Enter pass attempts" << endl;
cin >> PassAttempts;
int TotalPassY;
cout << "Enter total yards" << endl;
cin >> TotalPassY;
int Touch;
cout << "Enter touchdowns" << endl;
cin >> Touch;
int Int;
cout << "Enter interceptions" << endl;
cin >> Int;
int C = (PassCompletions/PassAttempts-0.30)*5;
int Y = (TotalPassY/PassAttempts-3)*0.25;
int T = (Touch/PassAttempts)*20;
int I = 2.375 - (Int/PassAttempts*25);
if (C<0){
C=0;
}
if (Y<0){
Y=0;
}
if (T<0){
T=0;
}
if (I<0){
I=0;
}
if (C>2.375){
C=2.375;
}
if (Y>2.375){
Y=2.375;
}
if (T>2.375){
T=2.375;
}
if (I>2.375){
I=2.375;
}
int PasserRating = (C+Y+T+I)/6*100;
if (PasserRating <= 85){
cout << "Rating " << PasserRating << ", this is poor" << endl;
}
if (PasserRating > 85 && PasserRating < 90){
cout << "Rating " << PasserRating << ", this is mediocre" << endl;
}
if (PasserRating > 90 && PasserRating < 95){
cout << "Rating " << PasserRating << ", this is good" << endl;
}
if (PasserRating > 95){
cout << "Rating " << PasserRating << ", this is great" << endl;
}
You need use data type which is suitable to store fractional value. For this purpose use float instead of int for these statements:
float C = (PassCompletions/PassAttempts-0.30)*5;
float Y = (TotalPassY/PassAttempts-3)*0.25;
float T = (Touch/PassAttempts)*20;
float I = 2.375 - (Int/PassAttempts*25);
The variable type int is only used to store whole numbers, eg 1,2,3...
Any expression with a decimal will be truncated and rounded down. Since you are doing a lot of calculations with floating point numbers, eg. 2.375, I would suggest you changing your int's to float's

Parallel Circuit

I am having trouble with this programming assignment. I need to calculate the value of total resistance for a parallel and series circuit. I have the series circuit functioning, but my problem is that when I try to calculate the total resistance for parallel circuit, my return value is 1.#INF. Any suggestions to how I can fix this
// project.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
void menu()
{
cout <<"\t\t\tLab 2 Menu Driven Algorithms" << endl;
cout <<"\t\t Calculating Parallel and Series Resistance" << endl;
cout <<"1)\tSeries" << endl;
cout <<"2)\tParallel" << endl;
cout <<"3)\tQuit" << endl;
}
int series(int& num, int& sum)
{
int answer;
num = 0;
sum = 0;
do
{
cout << "Enter Resistor " << num+1 << " value, 0 to calculate: ";
cin >> answer;
cout << endl;
sum = sum + answer;
num++;
}
while(answer != 0);
return sum;
}
double parallel (int& num, double& sum)
{
double answer;
num = 0;
sum = 0.0;
int counter = 0;
do
{
cout << "Enter Resistor " << num+1 << " value, 0 to calculate: ";
cin >> answer;
cout << endl;
counter++;
sum = (1/answer) + sum;
cout << sum << endl;
num++;
}
while(answer != 0);
return sum;
}
int main()
{
char choice;
int num = 0;
int sum = 0;
double sum2 = 0.0;
menu();
cout <<"\n\nPlease enter a value from the menu: ";
cin >> choice;
cout << endl;
while (choice != '3' && choice != 'q' && choice != 'Q')
{
switch(choice)
{
case '1': cout << "Calculating Series Resistance" << endl;
cout << "The series resistance for the " << num-1 << " resistors is: " << series(num, sum) << " Ohms\n";
system("pause");
break;
case '2': cout << "Calculating Parallel Resistance" << endl;
cout << "The parallel resistance for the " << num-1 << " resistors is: " << parallel(num, sum2) << " Ohms\n";
system("pause");
break;
default: break;
}
system("cls");
menu();
cout <<"\n\nPlease enter a value from the menu: ";
cin >> choice;
cout << endl;
}
system("pause");
return 0;
}
You probably want to rewrite the cycle inside the parallel() function this way, so that you will never process a value of 0 (which causes a division by zero in this case):
cout << "Enter Resistor " << num+1 << " value, 0 to calculate: ";
cin >> answer;
cout << endl;
while (answer != 0);
{
counter++; // NOTICE: This is never used for computation...
sum = (1/answer) + sum;
cout << sum << endl;
num++;
cout << "Enter Resistor " << num+1 << " value, 0 to calculate: ";
cin >> answer;
cout << endl;
}
The counter variable is never used for computation, so I guess you could get rid of it.
Also notice, that even the loop inside series() has a similar problem, although the fact that you never cause a division by zero doesn't make it visible.