0

How does one create C++ code that prints the running product of every third number.

I don't know if I understand it or not.

tshepang
  • 12,111
  • 21
  • 91
  • 136

5 Answers5

0

Don't know what you are running this on (32-bit architecture, 64-bit?) but you might be overflowing an int. Your answer is going to get very large.

RobP
  • 9,144
  • 3
  • 20
  • 33
  • Even `unsigned long long` will only handle up to 136. – Carey Gregory Mar 30 '14 at 21:39
  • I don't know really if I am running on 32-bit or 64? Some told me that this program can't be done in c++ !! Is that right? I mean, my code is correct, it's just the result is too big to fit, right? The important thing is that my understanding to the question is correct, right? – user3479130 Mar 30 '14 at 21:40
  • I wouldn't say "can't be done" in the Turing sense -- there are techniques for representing and handling very large numbers but it's a ton more work than when native types are sufficient. – RobP Mar 30 '14 at 21:43
  • Well, this is my first semester in computer science :(. That's why I am not familiar with some of the terms you guys use. – user3479130 Mar 30 '14 at 21:55
0

The variable sum is overflowing. Even making it an unsigned long long will only handle up to 136. Using double for sum will get you close -- 481 -- but no data type available in C++ will handle all the way up to 500.

If you absolutely must go to 500, you're going to need to use a class that can support large integer values. You don't say what platform and compiler you're using, but take a look at something like the BigInteger class.

Carey Gregory
  • 6,836
  • 2
  • 26
  • 47
  • Thanks Carey. I haven't studied classes yet. I am a fresh computer science student – user3479130 Mar 30 '14 at 21:57
  • Well, it doesn't have to be a class. It could just be a set of functions. You might find some open source code out there that supports large integers, but if not you would have to write it yourself. That would be an extremely advanced project for a beginning CS student. – Carey Gregory Mar 30 '14 at 21:58
  • Thank you so much Carey.I guess I am going with using unsigned long long data type as it gives the most results :/. – user3479130 Mar 30 '14 at 22:19
0
unsigned long product(100);
for (unsigned long i(103); i<=500; i+=3)
{
  product *= i;
  std::cout << product << std::endl;
}

So this iterates in steps of 3 every number from 100 to 500 and computes the product then outputs that.

Though as RobP says this will likely overflow, hence my using unsigned long's in the code above. To learn about detecting that overflow see this:

How to detect integer overflow?

Community
  • 1
  • 1
lmcdowall
  • 423
  • 3
  • 11
0

I am not sure if this can be accepted, but this prints a product (after all the format was not specified, right?).

unsigned long long  product = 1;
std::vector<int> fct;
for (int i = 100; i <= 500; i = i +3)
{
    if ( (i%2) == 0) {
        int r = i / 2;
        fct.push_back( r);
        std::cout << "product:" << product << " * 2";
        std::vector<int>::iterator it = fct.begin();
        while ( it != fct.end()) {
            std::cout << " * " << *it++;
        }
        std::cout << std::endl;
    } else {
    product = product * i;
    std::cout << "product:" << product << " * 2";
        std::vector<int>::iterator it = fct.begin();
        while ( it != fct.end()) {
            std::cout << " * " << *it++;
        }
        std::cout << std::endl;
    }
}

Output:

product:1 * 2 * 50

product:103 * 2 * 50

product:103 * 2 * 50 * 53

product:11227 * 2 * 50 * 53

product:11227 * 2 * 50 * 53 * 56

product:1291105 * 2 * 50 * 53 * 56

product:1291105 * 2 * 50 * 53 * 56 * 59

(...)

product:2557801186831291169 * 2 * 50 * 53 * 56 * 59 * 62 * 65 * 68 * 71 * 74 * 77 * 80

product:2557801186831291169 * 2 * 50 * 53 * 56 * 59 * 62 * 65 * 68 * 71 * 74 * 77 * 80 * 83

(...)

product:9086141910361656411 * 2 * 50 * 53 * 56 * 59 * 62 * 65 * 68 * 71 * 74 * 77 * 80 * 83 * 86 * 89 * 92 * 95 * 98 * 101 * 104 * 107 * 110 * 113 * 116 * 119 * 122 * 125 * 128 * 131 * 134 * 137 * 140 * 143 * 146 * 149 * 152 * 155 * 158 * 161 * 164 * 167 * 170 * 173 * 176 * 179 * 182 * 185 * 188 * 191 * 194 * 197 * 200 * 203 * 206 * 209 * 212 * 215 * 218 * 221 * 224 * 227 * 230 * 233 * 236 * 239 * 242 * 245

product:9086141910361656411 * 2 * 50 * 53 * 56 * 59 * 62 * 65 * 68 * 71 * 74 * 77 * 80 * 83 * 86 * 89 * 92 * 95 * 98 * 101 * 104 * 107 * 110 * 113 * 116 * 119 * 122 * 125 * 128 * 131 * 134 * 137 * 140 * 143 * 146 * 149 * 152 * 155 * 158 * 161 * 164 * 167 * 170 * 173 * 176 * 179 * 182 * 185 * 188 * 191 * 194 * 197 * 200 * 203 * 206 * 209 * 212 * 215 * 218 * 221 * 224 * 227 * 230 * 233 * 236 * 239 * 242 * 245 * 248

product:14532515211626403169 * 2 * 50 * 53 * 56 * 59 * 62 * 65 * 68 * 71 * 74 * 77 * 80 * 83 * 86 * 89 * 92 * 95 * 98 * 101 * 104 * 107 * 110 * 113 * 116 * 119 * 122 * 125 * 128 * 131 * 134 * 137 * 140 * 143 * 146 * 149 * 152 * 155 * 158 * 161 * 164 * 167 * 170 * 173 * 176 * 179 * 182 * 185 * 188 * 191 * 194 * 197 * 200 * 203 * 206 * 209 * 212 * 215 * 218 * 221 * 224 * 227 * 230 * 233 * 236 * 239 * 242 * 245 * 248

HelloWorld123456789
  • 5,299
  • 3
  • 23
  • 33
4pie0
  • 29,204
  • 9
  • 82
  • 118
  • Thanks lizusek. But I can't present this to my instructor. There are term that I've never seen. I will A+ for cheating :D. – user3479130 Mar 30 '14 at 22:16
  • @user3479130 is there requirement for this to be real multiplication? I don't think so as this really cannot be represented by any fundamental type. Or maybe really you misunderstood the task. Maybe it was just to print every third number? – 4pie0 Mar 30 '14 at 22:19
  • what is "running product of a number"? This is a question, ask your colleagues or professor... ; ) – 4pie0 Mar 30 '14 at 22:36
  • I don't know. I searched the internet but I couldn't find an answer! – user3479130 Mar 30 '14 at 22:37
  • @user3479130 Because it is not standard thing. I am myself very, very curious what your professor meant and what is an answer. Can you please ping me here when you will know this? – 4pie0 Mar 30 '14 at 22:39
0

The largest number you con go using integer data type is, unsigned long long int b = 0XFFFFFFFFFFFFFFFF; (18446744073709551615)

In your case the result is even bigger than this number.

That is the twist in this problem. You might have to find a way to handling large numbers. I recommend finding already existing API or algorithm.

I have written a program that does this... I did not test this for edge cases and there can be a better way of doing this.

For now,

#include <iostream>
#include <deque>

using namespace std;

void print_num(deque<int> &num) {
  for(int i=0;i < num.size();i++) {
    cout<<num[i];
  }
  cout<<endl;
}

deque<int> sum(deque<int> &oppA, deque<int> &oppB) {
  if (oppA.size() == 0) return oppB;
  if (oppB.size() == 0) return oppA;

  deque<int> result;
  unsigned int carry = 0;

  deque<int>::reverse_iterator r_oppA = oppA.rbegin();
  deque<int>::reverse_iterator r_oppB = oppB.rbegin();
  while ((r_oppA != oppA.rend()) && (r_oppB != oppB.rend())) {

    int tmp = *r_oppA + *r_oppB + carry;
    result.push_front(tmp % 10);
    carry = tmp / 10;

    r_oppB++;
    r_oppA++;

  }
  while (r_oppA != oppA.rend()) {
    int tmp = *r_oppA + carry;
    result.push_front(tmp % 10);
    carry = tmp / 10;
    r_oppA++;
  }

  while (r_oppB != oppB.rend()) {
    int tmp = *r_oppB + carry;
    result.push_front(tmp % 10);
    carry = tmp / 10;
    r_oppB++;
  }

  return result;
}

deque<int> multiply(deque<int>& multiplicand, deque<int>& multiplier) {

  unsigned int carry = 0;
  deque<int> result;
  int deci_cnt = 0;

  deque<int>::reverse_iterator r_multiplier = multiplier.rbegin();
  deque<int> tmp_result;

  while (r_multiplier != multiplier.rend()) {

    for (int i=0; i<deci_cnt ;i++) {
      tmp_result.push_front(0);
    }

    deque<int>::reverse_iterator r_multiplicand = multiplicand.rbegin();
    while (r_multiplicand != multiplicand.rend()) {
      int tmp = (*r_multiplicand) * (*r_multiplier) + carry;
      tmp_result.push_front(tmp % 10);
      carry = tmp / 10;
      r_multiplicand++;
    }

    if (carry != 0) {
      tmp_result.push_front(carry);
      carry = 0;
    }

    result = sum(result, tmp_result);

    deci_cnt++;
    tmp_result.clear();
    r_multiplier++;
  }

  return result;
}

deque<int> int_to_deque(unsigned long num) {
  deque<int> result;

  if (num == 0) {
    result.push_front(0);
  }

  while (num > 0) {
    result.push_front(num % 10);
    num = num / 10;
  }

  return result;
}

int main() {

  deque<int> num1 = int_to_deque(18446744073709551615ULL);
  deque<int> num2 = int_to_deque(18446744073709551615ULL);

  deque<int> result = multiply(num1, num2);
  print_num(result);

  return 0;
}

Output: 340282366920928463426481119284349108225

sdomalap
  • 21
  • 2