-4

I am trying to create a program that raises 10 to the power of the index number (1 for index 0, 10 for index 1, 100 for index 2, etc.). When I run this program, it does not output anything. What is the issue?

#include<iostream>
#include<cmath>
using namespace std;

int* powersOfTwo(int n);

int* powersOfTwo(int n)
{
    int i, x=10;
    n=3;
    
    int *arr = new int(n);
    
    for (i=0; i<=n; i++) {
        
        arr[i]=pow(x, i);
        
        cout<<arr[i]<<endl;
        
    }



    return 0;

}



int main() {

    powersOfTwo;

    
    return 0;
}
  • `int int(n)` isnt' the same as `new int[n]`. Also, you'll go out of bounds in your array access even after fixing this bug. – Stephen Newell Mar 09 '21 at 02:02
  • `new int(n)` allocates a single `int` with the value `n`. `new int[n]` allocates an array of size `n`. – Stephen Newell Mar 09 '21 at 02:04
  • Changing the parentheses to brackets did not seem to help. Are there any other issues? – W. W. Atkinson Mar 09 '21 at 02:06
  • `powersOfTwo;` ? Does that even compile ? if it does then `n` inside the function is undefined - you may be lucky and it's getting a value of 0. Returns in `int*` but you only ever return 0. – John3136 Mar 09 '21 at 02:06
  • `for (i=0; i<=n; i++) {` if you allow `n` better make the array have n+1 elements instead of n – drescherjm Mar 09 '21 at 02:08
  • 1
    @John3136 `powerOfTwo;` evaluates to the function, and the result is discarded. It doesn't do anything, but it compiles. And `0` will return an `int*` null pointer value. – François Andrieux Mar 09 '21 at 02:09
  • You may get a warning about code that has no effect. – drescherjm Mar 09 '21 at 02:09
  • 3
    You're not even calling your function. What book are you learning C++ from? – Stephen Newell Mar 09 '21 at 02:10
  • What edits do I need to make to keep it from returning a null value? – W. W. Atkinson Mar 09 '21 at 02:10
  • @FrançoisAndrieux sure - but I'd expect a modern compiler to complain about it. – John3136 Mar 09 '21 at 02:11
  • Replace `pow(x,2)` with `(x * x)`. The `pow` function is for floating point and multiplication is much faster and simpler. – Thomas Matthews Mar 09 '21 at 02:12
  • To do 2 to a power, use bit shifting: `(1 << n)` will raise 2 to the power of `n`. – Thomas Matthews Mar 09 '21 at 02:13
  • I don't want to square x, though. I need to raise it to the power of i. – W. W. Atkinson Mar 09 '21 at 02:13
  • @John3136 Compilers generally only identify the most frequently encountered dead code. It is a hard problem in general with low payout, so there hasn't been a lot of effort put into that. Static analysis tools already do a good job of that, so there isn't much motivation to have compilers be better at it. – François Andrieux Mar 09 '21 at 02:14
  • To call a function, you need to supply a parameter list, even if it is empty. In your `main` function, try `powersOfTwo(5)`. – Thomas Matthews Mar 09 '21 at 02:15
  • 2
    @W.W.Atkinson C++ cannot really be self taught. It has the concept of Undefined Behavior. That means it is impossible to learn by trial and observation. The behavior you see could be standard behavior, or it could be the result of Undefined Behavior. You have no way of knowing without looking up everything with an authoritative source. You need a good book or some sort of equivalent resource to learn C++. – François Andrieux Mar 09 '21 at 02:15
  • 2
    Since you are self-taught, disregard anything you learned about dynamic memory allocation. Use `std::vector`, it can be passed by reference. – Thomas Matthews Mar 09 '21 at 02:16
  • @François Andrieux yep - just tested on an ancient solaris box - Solaris CC doesn't flag it even with +w2 (highest warning level I could find from a quick scan on the manual).. g++ -Wall does flag it (even the ancient Solaris version I tried) – John3136 Mar 09 '21 at 02:32
  • @FrançoisAndrieux, I just like to say that I disagree with this. It's true that you can't learn the standards perfectly from experimenting with code on a certain compiler - but in practice if you're learning it's fine. You might make some wrong assumptions, but you'll do that anyway even if you're learning "text book style." Personally I think that the best learning is through tackling interesting problems (for the learner), then later being exposed to some new syntax/ideas/standards, etc, then repeat. – Elliott Mar 09 '21 at 02:35

1 Answers1

2

You may want to do the operation in a loop without calling pow:

void powerOfTwo(std::vector<int>& powers, int quantity)
{
  int power = 1;
  powers.push_back(power);
  for (int i = 0; i < quantity; ++i)
  {
    power = power * 10;
    powers.push_back(power);
  }
}

int main()
{
  std::vector<int> database;
  powersOfTwo(database, 10);
  for (int i = 0; i < 10; ++i)
  {
     std::cout << " " << database[i];
  }
  std::cout << "\n";
  return 0;
}

Notes:

  1. The vector is passed by reference, so it can be modified.
  2. There is no forced dynamic memory allocation; no leaks.
  3. The vector will expand as necessary.
  4. The power of 10 will be calculated, by mathematical definition of power or exponentiation.
  5. No calls to power; all integer arithmetic.
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154