c++ statement has no effect - c++

I'm having problems with some homework and I can't find the answer.
I have to do a simple program that solves a math problem, but it does not comp
This is the code:
#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
int a, b, FirstA;
int result = 0;
FirstA = a;
// The sum of the cubes between a and b: (a^3 + (a + 1)^3 + .. + (b + 1)^3 + b^3)
while (cin >> a >> b) {
for (a; a <= b; a++) {
result = result + pow(a,3);
}
cout << "suma dels cubs entre " << FirstA << " i " << b << ": " << result << endl;
}
}
The error it gives is this:
program.cc: In function ‘int main()’:
program.cc:23:15: error: statement has no effect [-Werror=unused-value]
for (a; a <= b; a++) {
All warnings being treated as errors.
What should I do?

You have an unused value in for (a; a <= b; a++) because the a; makes no sense.
Use a for-loop without initialization: for (; a <= b; a++).

Your function is not resembling the formula in the comment above it and the for loop is not defined properly: for (a; a <= b; a++).
A possible solution is to replace:
for (a; a <= b; a++) {
//---^
result = result + pow(a,3);
}
with:
cin >> a >> b;
int n = 10; // number of iterations
int i = 0;
int j = n;
do{
++i;
--j;
result += pow(a + i, 3) + pow(b + j, 3);
}while(i <= n);

Related

Can anyone explain what is the error I am getting in the output and how to remove it?

My code:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int BinaryToDecimal(int n)
{
int ans = 0;
int x = 1;
while (n > 0)
{
int y = n % 10;
ans = ans + x * y;
x = x * 2;
n = n / 10;
}
return ans;
}
int DecimalToBinary(int num)
{
vector<int> vect;
while (num > 0)
{
vect.push_back(num % 2);
num = num / 2;
}
int s = vect.size();
int i = s - 1;
for (i = s - 1; i >= 0; i--)
{
cout << vect.at(i);
}
return vect.at(i);
}
int main()
{
int a, b;
cout << "Enter first number: " << endl;
cin >> a;
cout << "Enter second number: " << endl;
cin >> b;
int a_deci = BinaryToDecimal(a);
int b_deci = BinaryToDecimal(b);
int sum = a_deci + b_deci;
DecimalToBinary(sum);
cout << endl;
return 0;
}
Output:
Enter first number:
10101
Enter second number:
11010
101111terminate called after throwing an instance of 'std::out_of_range'what(): vector::_M_range_check: __n (which is 18446744073709551615) >= this->size() (which is 6)
What does this error message mean and how do I fix it?
After this for loop
for (i = s - 1; i >= 0; i--)
{
cout << vect.at(i);
}
the variable i is equal to -1.
So the next call of the member function at with the value equal to -1 (that yields a very big number of the unsigned type std::vector<int>::size_type)
return vect.at(i);
throws the exception.
It seems you need to return from the function the whole vector elements of which will represent a number in the binary form.
Instead of the container std::vector<int> it will be better to use std::bitset.

Sum of digits in C++

program asked is sum of digits :
Input data are in the following format:
first line contains N - the number of values to process;
and then N lines will follow describing the values for which sum of digits should be calculated by 3 integers A B C;
for each case you need to multiply A by B and add C (i.e. A * B + C) - then calculate sum of digits of the result.
Answer should have N results, also separated by spaces
MY CODE IN C++ :
#include <iostream>
using namespace std;
int main ()
{
int n, a, b, c, t, sum = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a >> b >> c;
t = a * b + c;
while (t % 10 != 0)
{
sum = sum + t % 10;
t = t / 10;
}
while (t % 10 == 0)
{
sum = sum;
t = t / 10;
}
}
cout << " ";
cout << sum;
cout << " ";
return 0;
}
I'm having hard time correcting my code.
Any help is appreciated.
My assumption is there should be a better way to code this other than using 2 while loops.
PS : I checked other topics just want somebody that could help with my code thank you.
You don't need second while loop, and first one should be corrected to while (t != 0). After that your program for computing sum works correctly.
Try it online!
#include <iostream>
using namespace std;
int main ()
{
int n, a, b, c, t, sum = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a >> b >> c;
t = a * b + c;
while (t != 0)
{
sum = sum + t % 10;
t = t / 10;
}
}
cout << " ";
cout << sum;
cout << " ";
return 0;
}
Input:
1
123 456 789
Output:
33
Just noticed that you need N separate outputs instead of single sum (like you did), so then your program becomes like this:
Try it online!
#include <iostream>
using namespace std;
int main ()
{
int n, a, b, c, t, sum = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a >> b >> c;
t = a * b + c;
sum = 0;
while (t != 0)
{
sum = sum + t % 10;
t = t / 10;
}
cout << sum << " ";
}
return 0;
}
Input:
2
123 456 789
321 654 987
Output:
33 15

C++: implementing Modular Exponentiation

I am using this New and improved code I corrected in order to solve this question I have.
I am using modular Exponentiation to use the formula [a^k mod n] to get my answer for an assignment I had to do where I was required to code it in two steps.
First int k must be converted to a binary
representation K consisting of a list of 0s and 1s. Second, Modular Exponentiation must be performed
using a, n and K[] as arguments..
Earlier My code was incorrect and was able to correct it.
The Problem I now face is that when I google the online calculator for modular Exponentiation of 5^3 % 13, it should == 8
The result that I get from my code is 5.
I am trying to understand if there something minor I'm missing from the code or my math is wrong? Thanks
#include <iostream>
#include <vector>
using namespace std;
vector <int> BinaryK(int k);
int ModularExpo(int a, vector <int> & k, int n);
int main()
{
int a = 0;
int k = 0;
int n = 0;
cout << "a^k % n" << endl;
cout << "a = ";
cin >> a;
cout << "k = ";
cin >> k;
cout << "n = ";
cin >> n;
vector<int> B = BinaryK(k);
int result = ModularExpo(a, B, n);
cout << "a ^ k mod n == " << result << endl;
return 0;
}
// c == b^e % m
vector<int> BinaryK(int k)
{
vector<int> K; //hint: make K a vector
int tmp = k;
while (tmp > 0)
{
K.push_back(tmp % 2); //hint: use pushback
tmp = tmp / 2;
}
return K;
}
int ModularExpo(int a, vector<int> & K, int n)
{
if (n == 1)
return 0;
int b = 1;
if (K.size() == 0)
return b;
int A = a;
if (K[0] == 1)
b = a;
for (int i = 1; i < K.size() - 1; i++)
{
A = A * A % n;
if (K[i] == 1)
b = A*b % n;
}
return (b);
}
Change this one line:
for (int i = 1; i < K.size(); i++) // K.size() not K.size()-1

How to print common multiples of two number?

I'm trying to print all common multiples of two integers smaller than a certain limit(100 in my case). However, when I call my function, it does nothing. This is my code:
void com_mul(int a, int b)
{
int original = b;
for(int i = 1; a <= 100; i++)
{
a *= i;
b = original;
for(int j = 1; b <= a; j++)
{
b *= j;
if(a == b)
cout << b << ", ";
}
}
}
You can solve this problem much simpler, using a single loop.
In a for loop iterate over potential divisors d from 1 to 100. If d divides both a and b, print d.
You can tell if a number divides another number by applying the % operator, and checking the result for zero:
if (a%d == 0 && b%d == 0) {
cout << d << endl;
}
Tested with a = 4, b = 2, max = 100 on my machine. And it outputs 4.
This is because of the line for (int j = 1; b <= a; j++). j can only go upto 'a'
I think this would do.
#include <iostream>
#include <string>
int main()
{
int a, b, max;
std::cin >> a >> b >> max;
for (int i = a; i <= max; i++)
{
if (i%a == 0 && i%b == 0)
std::cout << i << std::endl;
}
return 0;
}

'cout' statement not being executed

The following program is to find the sum of even-valued fibonacci terms not more than four million.
The last 'cout' statement in this program doesn't get executed at all. Why is it? Help please.
#include <iostream>
using namespace std;
int main()
{
int a, b, c, sum, sum1, sum2;
a = 1;
b = 2;
sum2 = 0;
cout << b << endl;
c = a + b;
sum1 = c;
while (c <= 4000000)
{
a = b;
b = c;
if ((a + b) <= 4000000)
{
c = a + b;
if (c%2 == 0)
{
sum2 = sum2 + c;
cout << c << endl;
}
}
}
cout << "The sum of even fibonacci numbers not greater than 4 million is: " << (sum1 + sum2); //Not being executed
return 0;
}
I can't execute the program, but I think that your program never ends, which is why you never get to that statement. Your outer while loop will keep on going unitl c <= 4000000. However, you only increment c if and only if (a + b) <= 4000000, so c never goes beyond 4 million.
To fix this, you could try the below:
#include <iostream>
using namespace std;
int main()
{
int a, b, c, sum, sum1, sum2;
a = 1;
b = 2;
sum2 = 0;
cout << b << endl;
c = a + b;
sum1 = c;
while (c <= 4000000)
{
a = b;
b = c;
c = a + b; //Update c regardless.
if (c <= 4000000)
{
if (c%2 == 0)
{
sum2 = sum2 + c;
cout << c << endl;
}
}
}
cout << "The sum of even fibonacci numbers not greater than 4 million is: " << (sum1 + sum2); //Not being executed
return 0;
}
while ( c <= 4000000 )
{
// ...
if ( ( a + b ) <= 4000000 )
{
c = a + b; // i.e. <= 4000000
// ...
}
}
How do you expect that loop to terminate?