-8
#include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{
        int x[3];
        int i;
        int pause;
        for (i = 0; x[i] < 10; i++); {
                x[i] *= x[i];
                cout << x[i];
        }
        cout << x;
        cin >> pause;

    return 0;
}

What did I do wrong?

Shouldn't it print:

16
25
36
{16, 25, 36}

I'm quite new to C++ so I may have misused a loop.

Thanks

Daniel
  • 69
  • 1
  • 2
  • 6
    Why should it print that? You never initialized your array. – Jodocus Oct 29 '17 at 11:26
  • And you are going outside the size of the array. What do you expect when invoking undefined behaviors? – Ed Heal Oct 29 '17 at 11:28
  • Uninitialized local variables, including array, are *uninitialized!* Their values will be *indeterminate* and in C++ even *reading* those values leads to [*undefined behavior*](http://en.cppreference.com/w/cpp/language/ub). – Some programmer dude Oct 29 '17 at 11:28
  • `using namespace std;` - This is also a bad idea - google that – Ed Heal Oct 29 '17 at 11:28
  • Furthermore, you can't print arrays like you seem to think. You have to loop over each element and print them separately. – Some programmer dude Oct 29 '17 at 11:29
  • 4
    You seem to have quite a few misunderstandings about C++. You should take a step back and systematically learn the language from a good book. – Baum mit Augen Oct 29 '17 at 11:29
  • @BaummitAugen - There ought to be a bot which pastes [this link](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) at the mention of good books in C++ questions. – StoryTeller - Unslander Monica Oct 29 '17 at 11:41
  • 1
    @Someprogrammerdude - Except for character arrays – Ed Heal Oct 29 '17 at 11:42
  • The C++ community on SO is ridiculously intolerant. Instead of rating the quality of the posed question they rate the level of knowledge of the person asking the question. In my opinion this is an excellent question - it's minimal, complete and verifyable, making it possible to give an answer in a matter of seconds. – rustyx Oct 29 '17 at 12:47

3 Answers3

1

You need to initialise the array contents. The compiler does not do that for you.

Even then, you’ll need to initialise them to something other than zero, else the loop will never terminate.

Your algorithm is also vulnerable to your writing outside the bounds of the array. Are you sure that i will never be greater than 2? I’m not.

Finally your cout call will output the address of the first element of x due to pointer decay. To output actual elements use x[i] or similar.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

Reading/Writing to an Un-initialized variable will produce an Undefined Behavior.

In your case you should initialize the array before using it.

int x[3] = {4, 5, 6};

Also that is not how to iterate over the array elements:

 for (i = 0; x[i] < 10; i++);{}

Above you should check whether i is less than the size of the array (3) not the value 10.

Also remove the semicolon ; Because in your code no iteration will occur and the body below for loop will be executed once.

So your code will look like:

    int x[3] = {4, 5, 6};

    for (int i = 0; i < 3; i++) {
        x[i] *= x[i];
        std::cout << x[i];
    }

   std::cin.get();
  • As I guess above from what output you wanted and what the loop for does I filled the array with the values: 4, 5, 6;

  • Also you shouldn't print the elements of the array that way:

    std::cout << x;

Above it will only print the address of first element x[0] because the name of an array will decay to pointer to its first element (element 0).

std::cout << *x; // will print 16 (4 * 4). Which is the first element.

To print or assign all the elements use iteration:

for(int i(0); i < 3; i++)
    std::cout << x[i] << ", ";
Raindrop7
  • 3,889
  • 3
  • 16
  • 27
1

You have to initialize your array. Default initialization is not performed for local variables. That is why it is printing garbage value.

Rahul Das
  • 26
  • 3