array question

Hey guys I am just figuring out arrays and need some guidance. I made an array of 100 elements but I want to square each element. so arr[0]=0, arr[1]=1, arr[2]=4 and so on till arr[99]. And i wanna print each value out However I am having alot of trouble with my code on doing that
Why am I getting that 100 at the end?

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

using namespace std;

int main()
{
  int arr[100];
  for(int i =0; i<100; i++)
  {
      arr[i]=i*i;
  }
 for(int i=0; i<=100;i++)
 {
     cout<<arr[i]<<endl;
 }
}

Last edited on
When you declare code like
 
int arr[100];


The array's elements are uninitialized, which means they contain garbage data. In your loop, you take each element of the array (which is a garbage value) and you multiply it with itself. The result is another garbage value, which you then store inside of the array. This is not correct. What you should do in the array is this:
1
2
3
4
5
6
   for (int i = 0 ; i < 100 ; i++)
   {


        arr[i] = i * i //making each value squared
   }
So, you've declared that the size is 100. Now, there are 100 empty spaces in the array.

arr[i] = arr[i] * arr[i];

So when you're multiplying arr[i] * arr[i], you're not really multiplying an element - because there's no element in it. That's why your code is freaking out. I think instead, what you're looking for is:

arr[i] = i * i;

Let me know if you have any questions!
Last edited on
Hello clayoda,

Looking at the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
	int arr[100]; //declaring my array. Defines an array of 100 garbage values

	for (int i = 0; i < 100; i++)
	{
		arr[i] = arr[i] * arr[i];  //making each value squared
	}

	for (int i = 0; i < 100; i++)
	{
		cout << arr[i] << endl;  //printing it out 
	}

}

As the comment on line 7 states you have defined space for an array , but failed to initialize it. So it contains garbage values, means whatever happens to be at the memory location when the space was set aside for the array.

On line 11 what you need to find out is what is the value of "arr[i]" or "arr[0]" when you do your multiplication.
But that is only half the problem. Even if you initialize the array, which you should, it would be (0) zero unless you use a for loop to give each element the numbers 0 - 99.

You have the right idea for line 11, but you are multiplying the wrong number. What you want is to multiply the loop iterator not the array which does not have the correct number.

Your code would work better something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main()
{
	constexpr size_t MAXSIZE{ 100 };

	int arr[MAXSIZE]{}; //declaring my array. Defines an array of 100 elements all set to zero.

	for (int i = 0; i < MAXSIZE; i++)
	{
		arr[i] = i * i;  //making each value squared
	}

	for (int i = 0; i < MAXSIZE; i++)
	{
		cout << arr[i] << endl;  //printing it out 
	}

	return 0;  // <--- Not required, but makes a good break point.
}


Andy
KittyIchigo1 wrote:

So when you're multiplying arr[i] * arr[i], you're not really multiplying an element - because there's no element in it. That's why your code is freaking out.


That's not exactly true. There IS an element there. Actually 100 elements in the array. They exist and have memory allocated to them on main's stack frame. However, they haven't been initialized, which means they contain garbage.
Ohhh okay
Hello KittyIchigo1,

KittyIchigo1 wrote:

So, you've declared that the size is 100. Now, there are 100 empty spaces in the array.


Yes an no. The first sentence is correct. The second is not.

There are 100 elements to the array and they are not empty. They do contain a garbage value. Even if you initialize the array it still contains a value.

Hope that helps,

Andy
There's another problem in this program:

The computer stores an int using a fixed number of digits, which means that there's a minimum value and a maximum value that it int can represent.

A typical int is (usually) made up of 32 digits, but these are always binary digits, or bits, which alone can only represent two numbers - zero or one. 32 bits together can represent a total of 2^32 distinct values.

As a consequence, the largest number one can store in a int is 2^31 - 1 = 2147483647, and the smallest number is - (2^31) = -2147483648.

2^31 is much smaller than the numbers you're attempting to store in your array. A program which puts a value larger than the maximum (or smaller than the minimum) into an int is said to overflow or underflow the int.

A program which does this is said to exhibit undefined behavior, or UB for short. UB always represents an error on the programmer's part. A program that contains UB may behave correctly, crash, output complete garbage, or silently fail in a subtle manner unrelated to the real issue. In the worst case, such a program may curse you with a case of nasal demons. You'll have to change your program to avoid storing overly-large numbers in your array.
http://catb.org/jargon/html/N/nasal-demons.html
Last edited on
A classic example of overflow/underflow leading to undefined behavior is the Civilization II game bug with a Nuclear Gandhi.

https://knowyourmeme.com/memes/nuclear-gandhi

The bug became an actual feature in later iterations of the game series.
Topic archived. No new replies allowed.