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

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--);
}
}

Related

How do i add up my array values and display it along side with my other values

I am trying to add up all the values that have been stored into array b and have it display under the "total column" and don't know how to only have the scores add together.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int array[5][4];
int sum = 0;
cout<<"Enter grades for 4 exams for the 5 students \n";
for(int i=0;i<5;i++)
{
for(int b=1;b<=4;b++)
{
cout <<setw(8)<< "enter student "<< i << "'s grade for exam " << b << '\n';
cin >> array[i][b];
}
}
cout <<"ID"<<setw(11)<<"score 1"<<setw(11)<<"score 2"<<setw(11)<<"score 3"<<setw(11)<<"score 4"<<setw(11)<<"total"<<setw(11)<<"letter"<<endl;
cout <<"--------------------------------------------------------------------------------------------------------------\n";
for(int i=0;i<5;i++)
{
cout << i<< " ";
for(int b=1;b<=4;b++)
{
sum = sum + array[b];
cout <<setw(10)<<array[i][b]<<sum;
}
cout <<'\n';
}
cout <<"--------------------------------------------------------------------------------------------------------------\n";
return 0;
}
To be more specific around line 28
for(int i=0;i<5;i++)
{
cout << i<< " ";
for(int b=1;b<=4;b++)
{
sum = sum + array[b];
cout <<setw(10)<<array[i][b]<<sum;
}
cout <<'\n';
Arrays indexes start at 0, not 1. You are correctly looping through your array's 1st dimension, but not its 2nd dimension. You need to change the inner for loops from for(int b=1;b<=4;b++) to for(int b=0;b<4;b++)
Also, to handle the total column, you simply need to reset sum to 0 on each iteration of the 1st dimension, and then print the result after the iteration of the 2nd dimension.
Try this:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int array[5][4];
cout << "Enter grades for 4 exams for the 5 students \n";
for(int i = 0; i < 5; i++)
{
for(int b = 0; b < 4; b++)
{
cout << "Enter student " << i+1 << "'s grade for exam " << b+1 << '\n';
cin >> array[i][b];
}
}
cout << "ID" << setw(11) << "score 1" << setw(11) << "score 2" << setw(11) << "score 3" << setw(11) << "score 4" << setw(11) << "total" << setw(11) << "letter" << endl;
cout << "--------------------------------------------------------------------------------------------------------------\n";
for(int i = 0; i < 5; i++)
{
cout << setw(2) << left << i+1 << right;
int sum = 0;
for(int b = 0; b < 4; b++)
{
sum = sum + array[i][b];
cout << setw(11) << array[i][b];
}
cout << setw(11) << sum << '\n';
}
cout << "--------------------------------------------------------------------------------------------------------------\n";
return 0;
}
Online Demo

In C++ how to put a variable inside an array?

#include <iostream>
#include <cmath>
using namespace std;
bool narcissistic(int value)
{
cout << "value is:" << value << endl;
int digitNumber = (log10(value) + 1);
cout << "Digit Number:" << digitNumber << endl;
int sum = 0;
int arr[5];
for (int i = 0; i <= digitNumber - 1; i++)
{
cout << "i digit:" << i << endl;
int exponential = pow(10, i);
arr[i] = (value % (exponential *10)) / pow(10, i);
cout << arr[i] << endl;
sum += pow(arr[i], digitNumber);
cout << pow(arr[i], digitNumber) << endl;
cout << i << "'th sum value:" << sum << endl;
}
return true;
}
int main() {
int value = 153;
narcissistic(value);
return 0;
}
In this code here;
I had to write:
int arr[5];
But I wanted the size of this array to be a variable so that its size could be defined and its values could be put by for loop so its size could be equal to the amount of loop.
I wanted to write like this:
for (int i = 0; i <= digitNumber - 1; i++)
{
int arr[i];
cout << "i digit:" << i << endl;
int exponential = pow(10, i);
arr[i] = (value % (exponential *10)) / pow(10, i);
cout << arr[i] << endl;
sum += pow(arr[i], digitNumber);
cout << pow(arr[i], digitNumber) << endl;
cout << i << "'th sum value:" << sum << endl;
}
Visual Studio says that the value that the array takes as length must be constant that's why you can't determine it by loops etc.
Is there a way to put a variable as size in an array and assign an array's size by a loop in C++?
You probably want something like this:
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
bool narcissistic(int value)
{
cout << "value is:" << value << endl;
int digitNumber = (log10(value) + 1);
cout << "Digit Number:" << digitNumber << endl;
int sum = 0;
std::vector<int> arr(digitNumber); // initialize a vector containing
// digitNumber elements
for (int i = 0; i <= digitNumber - 1; i++)
{
cout << "i digit:" << i << endl;
int exponential = pow(10, i);
arr[i] = value % (exponential * 10) / pow(10, i);
cout << arr[i] << endl;
sum += pow(arr[i], digitNumber);
cout << pow(arr[i], digitNumber) << endl;
cout << i << "'th sum value:" << sum << endl;
}
// printf values in arr
cout << "arr: ";
for (auto& value : arr)
cout << value << ", ";
return true;
}
int main() {
int value = 153;
narcissistic(value);
return 0;
}
But this is a rather convoluted way to decompose a number into it's digits. You don't need floating point arithmetic at all to do this:
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
bool narcissistic(int value)
{
cout << "value is:" << value << endl;
std::vector<int> arr; // start off with an empty vector
for (int i = 0; value != 0; i++)
{
arr.push_back(value % 10); // add to the vector
value /= 10;
}
// printf values in arr
cout << "arr: ";
for (auto& value : arr)
cout << value << ", ";
return true;
}
int main() {
int value = 153;
narcissistic(value);
return 0;
}
If you want to avoid using vector, you can use this:
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
bool narcissistic(int value)
{
cout << "value is:" << value << endl;
int digitNumber = (log10(value) + 1);
cout << "Digit Number:" << digitNumber << endl;
int sum = 0;
vector<int> v(digitNumber);
int *arr = &v[0];
for (int i = 0; i <= digitNumber - 1; i++)
{
cout << "i digit:" << i << endl;
int exponential = pow(10, i);
arr[i] = value % (exponential * 10) / pow(10, i);
cout << arr[i] << endl;
sum += pow(arr[i], digitNumber);
cout << pow(arr[i], digitNumber) << endl;
cout << i << "'th sum value:" << sum << endl;
}
// print values in arr
cout << "arr: ";
for (int i = 0; i <= digitNumber - 1; i++)
cout << arr[i] << ", ";
return true;
}
int main() {
int value = 153;
narcissistic(value);
return 0;
}

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?

Unhandled exception at 0x012B1CA9

I am new to C++ and am trying to build a simple program that with the users input to proceed will generate a random left or right. I had the program working correctly until I added in the array to try and store each item as I have to output them as soon and the user would like to exit the loop. The program seems to compile fine but at run time I receive "Unhandled exception at 0x012B1CA9" Any help would be greatly appreciated.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int userSelection = 1;
const int MAX = '100';
int randNum(0);
int one (0);
int two (0);
int total(0);
int sel[MAX];
do
{
cout << "Press 1 to pick a side or 0 to quit: ";
cin >> userSelection;
for (int i = 1; i < MAX; i++)
{
srand(time(NULL));
sel[i] = 1 + (rand() % 2);
if (sel[i] == 1)
{
cout << "<<<--- Left" << endl;
one++;
total++;
}
else
{
cout << "Right --->>>" << endl;
two++;
total++;
}
}
} while (userSelection == 1);
cout << "Replaying Selections" << endl;
for (int j = 0; j < MAX; j++)
{
cout << sel[j] << endl;
}
cout << "Printing Statistics" << endl;
double total1 = ((one / total)*100);
double total2 = ((two / total)*100);
cout << "Left: " << one << "-" << "(" << total1 << "%)" << endl;
cout << "Right: " << two << "-" << "(" << total2 << "%)" << endl;
system("pause");
return 0;
};
You have a multi-character constant here... and the behavior doesn't go as expected...
Change this line
const int MAX = '100';
to
const int MAX = 100;
Note the removed single quotes.
And secondly, I will advice you to remove the Seed of the C random generator from the for loop because, you'll likely get the same values from the rand() if you always call it immediately after seeding...
But preferable use the algorithm from C++'s random header
Here is a corrected version of your original code....
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int userSelection = 1;
const int MAX = 100; // <---changed
int randNum(0);
int one (0);
int two (0);
int total(0);
int sel[MAX];
do
{
cout << "Press 1 to pick a side or 0 to quit: ";
cin >> userSelection;
srand(time(NULL)); //< moved to here
for (int i = 0; i < MAX; i++) // <-- modified starting index
{
sel[i] = 1 + (rand() % 2);
if (sel[i] == 1)
{
cout << "<<<--- Left" << endl;
one++;
total++;
}
else
{
cout << "Right --->>>" << endl;
two++;
total++;
}
}
} while (userSelection == 1);
cout << "Replaying Selections" << endl;
for (int j = 0; j < MAX; j++)
{
cout << sel[j] << endl;
}
cout << "Printing Statistics" << endl;
double total1 = ((one / total)*100);
double total2 = ((two / total)*100);
cout << "Left: " << one << "-" << "(" << total1 << "%)" << endl;
cout << "Right: " << two << "-" << "(" << total2 << "%)" << endl;
system("pause");
return 0;
};
I think that it is basically good idea to read more about C data types and declaration. Your error:
const int MAX = '100' should be const int MAX = 100 without any quotes. C++ does implicit conversion from character literals to int.

Resizing a dynamic bitset in C++ to a length entered by the user

This C++ program asks the user to enter the length of the bit sequence he/she will enter next. This length variable is named xx, it is of type int. I am using three dynamic bitset with the initial size of 5, these are inpSeq, operSeq and bit. I am resizing the bitsets by
inpSeq.resize(xx); for example. When compiling the program, a very big list of errors appears, I feel I can't paste it here. but I am sure that these errors are all related to this variable xx, the code was compiled fine before using it as a variable to resize the bitset. What is wrong with the way I am resizing the bitset? And can I make the bitsets of the size of inpSeq without asking the user to enter the length of inpSeq bitset?
#include <iostream> //Standard library.
#include <boost/dynamic_bitset.hpp> //Library for 10 handling.
#include <vector> //Variable size array.
#include <algorithm> //We use sorting from it.
using namespace std;
int main()
{
int y = 0;
int turnCount = 0;
int count1 = 0, count0 = 0;
int xx = 0;
boost::dynamic_bitset<> inpSeq(5);
int polyLoc;
boost::dynamic_bitset<> operSeq(5);
boost::dynamic_bitset<> bit(5);
vector <int> xorArray;
vector <int> keyReg;
cout << "What is the legnth of the sequence?";
cin << xx;
inpSeq.resize(xx);
operSeq.resize(xx);
bit.resize(xx);
cout << "Enter a bit sequence: \n";
cin >> inpSeq;
int seq_end = inpSeq.size() - 1;
cout << "Enter polynomial:";
cin >> polyLoc;
while(polyLoc>0)
{
xorArray.push_back(polyLoc%10);
polyLoc/=10;
}
cout << "xorArray is: ";
for ( unsigned int i = 0; i < xorArray.size(); i++)
{
cout << xorArray[i] << " ";
}
sort(xorArray.rbegin(), xorArray.rend());
cout << "\n";
operSeq = inpSeq;
keyReg.push_back(inpSeq[0]);
int x = xorArray[0];
cout << "x is: " << x << "\n";
for ( unsigned int i = 0; i < xorArray.size(); i++)
{
cout << xorArray[i] << "\n";
}
cout << "bit 3 of initial " << bit[seq_end] << "\n";
do {
for (unsigned int r = 1; r < xorArray.size(); r++)
{
bit[seq_end] = operSeq[x];
y = xorArray[r];
bit[seq_end] = bit[seq_end] ^ operSeq[y];
}
operSeq >>= 1;
operSeq[seq_end] = bit[seq_end];
keyReg.push_back(operSeq[0]);
turnCount ++;
cout << "--\n";
}
while ((operSeq != inpSeq) && (turnCount < 20));
cout << "Generated key is: ";
for (unsigned int k = 0; k < keyReg.size(); k++)
{
cout << keyReg[k];
}
cout << "\n";
cout << "Bit 1 positions: ";
for ( unsigned int g = 0; g < xorArray.size(); g++)
{
cout << xorArray[g];
}
cout << "\n";
cout << "Key length is: " << keyReg.size();
cout << "\n";
for ( unsigned int i = 0; i < keyReg.size(); i++)
{
if (keyReg[i]==1)
{
count1++;
}
else {
count0++;
}
}
cout << "Number of 0's: " << count0 << "\n";
cout << "Number of 1's: " << count1 << "\n";
if ( keyReg.size()%2 ==0)
{
cout << "key length is even. \n";
if (count1==count0)
{
cout << "Key is perfect! \n";
}
else {
cout << "Key is not perfect! \n";
}
}
else
{
cout << "key length is odd. \n";
if ((count1==count0+1) || (count0==count1+1))
{
cout << "Key is perfect! \n";
}
else {
cout << "Key is not perfect! \n";
}
}
cin.get();
}