Preparation for test

Hey, on Friday I have a c++ test and as a preparation we have been given an old test to practice on, one of the questions are: What does this program output. I though it would be:
0
1
2
3
4

However the output when I try it is:

4273030
4272928
9385600
92

Why is that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;2686760


int main(){
int tal[5];
int i;


    for(i=0; i<5; i++)
{
	cout << tal[i] << "   ";  //____________0  1 2 3 4
cout <<endl;}
    return 0;
}
Why would it output 0->5? You're printing tal[i], not i itself.

Just ask yourself: what value is assigned to tal[0], tal[1], ...?
hmm...I do not know...
I mean there is no where in the deceleration that shows what the tal(number in swedish) is...or am I just derpaherp?
Basically int tal[5]; just grabs some memory for that array. It does not change what is in that memory, so you get "random" numbers. Restart your computer and those numbers will be different. (there are other ways to get them to change like loading another program, and then loading this program.)

So the answer is:

The output is unpredictable.
Last edited on
Soooo...my teacher is fucking with us? How odd...oh wait..not odd at all.
Essentially the correct answer is "The output is undefined.". Tell him this and he will be impressed.
closed account (1vRz3TCk)
using namespace std;2686760 <--- Would it even compile?
Probably a typo by the OPer?

Though for fun you could do this:
1
2
3
4
5
using namespace std;int


main()
{

Hahaha...
Last edited on
Topic archived. No new replies allowed.