i'm having trouble getting the correct output of the function. The function output should show the expression. For example, if the input is "1234", then the output should be 1 + 2 + 3 + 4 = 10.
i can get the function to output the first part of the expression, but i'm not sure how to get it to output the sum as well.
heres what i have so far:
void sumDigits(int num, int &sum){
sum += num % 10;
if(num < 10)
cout << num;
else {
sumDigits(num/10, sum);
cout << " + " << num % 10;
}
}
why don't you do it in caller?
you could pass the remaining digits number to process to a function and output sum when it's 0, but I doing it in the caller is better..
<< can be chained. Try
cout << " + " << num % 10 << " = " << sum << endl;
Related
I have the following simple code. I allocate dynamically memory for 3 doubles, I assign to each double a number and after I deallocate the memory but as one can see if runs the code the only difference before and after the deletion (delete[] x) and the only difference is for the first double of the vector. I can't understand why the content of the first element of the vector changed and the content of x remained the same with the same address of memory.
#include <iostream>
#include <cmath>
int main(int argc, char * argv[])
{
double * x;
x = new double [3];
x[0] = 1; x[1]=3; x[2]=5;
std::cout << x[0] << " " << x[1] << " " << x[2] << "\n";
std::cout << x << "\n";
delete[] x;
std::cout << x[0] << " " << x[1] << " " << x[2] << "\n";
std::cout << x << "\n";
return 0;
}
To my understanding, this is undefined behaviour; x is read after it is deleted.
I'm trying to read in multiple element types from a text file.
Here's an example input:
push 0
push 1
push 3
push 5
add
push 3
The code below work until the last line, then I get a BAD_ACCESS error after trying anything after it reads in a line without 'b'. Any suggestions?
ifstream infile("example.txt");
while (!infile.eof())
{
infile >> a >> b;
if (a == "push")
push(b);
if (a == "add")
{
add();
}
if (a == "subtract")
{
int y = subtract();
cout << y << endl;
}
if (a == "multiply")
{
int y = multiply();
cout << y << endl;
}
if (a == "divide")
{
int y = divide();
cout << y << endl;
}
if (a == "pop")
{
cout << b;
b = NULL;
}
cout << a << b << endl;
}
I want to delete items out of my QMultiHash. Looking at the docs, I believe I am doing it correctly but it always crashes after the first delete. What am I doing wrong?
Here is my code:
for (QMultiHash<int, Frame*>::iterator i = m_FrameBuffer.begin(); i != m_FrameBuffer.end(); ++i) {
if ( (frameNumber - i.key()) >= ( 5 ) ) { // Delete frames 5 frames old or more
qDebug() << "DELETE ==> Key:" << i.key() << "Value:" << i.value() << " Difference: " << (frameNumber - i.key());
int removed = m_FrameBuffer.remove(i.key());
qDebug() << "Removed this many: " << removed;
}
}
Here is the output:
FRAME COUNT: 1
FRAME COUNT: 2
FRAME COUNT: 3
FRAME COUNT: 4
FRAME COUNT: 5
DELETE ==> Key: 2 Value: Frame(0x138a400) Difference: 5
Removed this many: 1
The program has unexpectedly finished.
Your iterator becomes invalid after you remove items from a container while iterating it. Try this:
QList<int> keys = m_FrameBuffer.keys();
foreach (int key, keys)
{
int diff = frameNumber - key;
if (diff >= 5)
{
qDebug() << "DELETE ==> Key:" << key
<< "Value:" << m_FrameBuffer.value(key)
<< "Difference: " << diff;
int removed = m_FrameBuffer.remove(key);
qDebug() << "Removed this many: " << removed;
}
}
Also you can use QMutableHashIterator for it:
QMutableHashIterator<int, Frame*> it(m_FrameBuffer);
while (it.hasNext())
{
it.next();
int key = it.key();
int diff = frameNumber - it.key();
if (diff >= 5)
{
qDebug() << "Items to be removed:"
<< m_FrameBuffer.values(it.key()).size();
it.remove();
}
}
Lets say we have a variable called X and we do some operations on it. now for printing it on the QtextEdit I want to print it like this cout on console:
cout << "The value of X is " << X << endl;
But the setText function only prints out a QString not both "the value of ... " and X.
You can use a QTextStream to write data into a QString similar to cout:
int X = 42;
QString str;
QTextStream out(&str);
out << "The value of X is " << X << endl;
qDebug() << str;
Output:
"The value of X is 42
"
I would solve this in the following way:
QString text = QString("This is my value: %1").arg(x); // x can be either number or string
textEdit->setText(text);
If your "x" is an integer, for example, you can convert that number into a string and concatenate that with the introducing string like that:
QString myText = "This is my value: " + QString::number(x);
If x=5 this will give you this string:
This is my value: 5
You can now assign myText to your QTextEdit with settext.
I'm very interested in cryptography, and since I like programming too, I decided to make a little program to encrypt files using XTEA encryption algorithm.
I got inspired from Wikipedia, and so I wrote this function to do the encryption (To save space, I won't post the deciphering function, as it is almost the same):
void encipher(long *v, long *k)
{
long v0 = v[0], v1 = v[1];
long sum = 0;
long delta = 0x9e3779b9;
short rounds = 32;
for(uint32 i = 0; i<rounds; i++)
{
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
sum += delta;
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
}
v[0] = v1;
v[1] = v1;
}
Now when I want to use it, I wrote this code:
long data[2]; // v0 and v1, 64bits
data[0] = 1;
data[1] = 1;
long key[4]; // 4 * 4 bytes = 16bytes = 128bits
*key = 123; // sets the key
cout << "READ: \t\t" << data[0] << endl << "\t\t" << data[1] << endl;
encipher(data, key);
cout << "ENCIPHERED: \t" << data[0] << endl << "\t\t" << data[1] << endl;
decipher(data, key);
cout << "DECIPHERED: \t" << data[0] << endl << "\t\t" << data[1] << endl;
I always get either run-time crash or wrong decipher text:
I do understand the basics of the program, but I don't really know what is wrong with my code. Why is the enciphered data[0] and data1 the same? And why is deciphered data completely different from the starting data? Am I using the types wrong?
I hope you can help me solving my problem :) .
Jan
The problem is here:
v[0] = v1; // should be v[0] = v0
v[1] = v1;
Also, you only set the first 4 bytes of the key. The remaining 12 bytes are uninitialized.
Try something like this:
key[0] = 0x12345678;
key[1] = 0x90ABCDEF;
key[2] = 0xFEDCBA09;
key[3] = 0x87654321;
The fixed code gives me this output:
READ: 1
1
ENCIPHERED: -303182565
-1255815002
DECIPHERED: 1
1