For loop with an array - c++

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a[5];
for (int i = -1; i < 7; i++)
cout << i << " " << setw(2) << a[i] << endl;
}
So I'm trying to figure out why this won't work. If i take out a[i] it works, but if i leave it in, the for loop goes from -2 until the 1000's which is obviously not right. Is it because a[i] is not bound to the parameters of the for loop (i < 7)? I'm not really sure I understand.
EDIT As many of you have explained it was a matter of uninitializing the array and using bounds outside of the array (e.g -2). It was not something I thought of, nor found when searching for why this was happening.

First, as marcadian pointed out, the array a is not initialized, so values in the array are completely random.
Also, the size of the a array is 5, meaning that you can access between indexes 0 and 4 inclusive.
However, in your loop, you try to access at index -1, and index 6. Attempting to write and read at invalid indexes ( such as -1 and 6, in this case ) is undefinded behavior ( it can crash with a segmentation fault, or corrupt other variables, which can make debugging process very hard ... )
A way to avoid buffer overrun like you did is to use std::array STL container, like this :
std::array<int, 5> a;
//Access elements using the method 'at()', it has bound checking
a.at(0); // 0 is a valid index
//a.at(-1); // -1 is not a valid index, it will crash ( at least in debug mode )
//a.at(6); // 6 is also not a valid index
The std::array STL container does the same things normal array does, but provides useful methods

You're printing out random value in memory because variable a is not initialized. Uninitialized variable has random value in memory. What are you trying to do?

in computers an array is really just a given number of values, starting at array[0], and ending at array[n].
int a[5]; statically allocates space for 5 integers on the stack, starting at a[0], and ending at a[4].
Try this iteration instead:
for (int i = 0; i < 4; i++)

In addition to not initializing the array, a, the indices you would be using to access its elements are out of bounds. C++ uses zero-indexing - you could try, for instance:
int main() {
int a[] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
cout << i << " " << a[i] << endl;
}
}
Output would be:
0 1
1 2
2 3
3 4
4 5

Related

Where is the array getting that value from?

So here's a simple program that just search for two numbers in an array that sum up to a certain value k
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
unordered_set<int> hashtable;
int k =7;
int arr[5] = {1, 2, 3, 4, 5};
int s = sizeof(arr);
for (int i =0; i<s; i++){
if( hashtable.find(k - arr[i])!= hashtable.end() )
{
cout << arr[i] << endl;
cout<< "found one " << arr[i] << " and "<< k-arr[i]<< endl;
} else {
hashtable.insert(arr[i]);
}
}
return 0;
}
And here's the out put, I am getting
4
found one 4 and 3
5
found one 5 and 2
7
found one 7 and 0
7
found one 7 and 0
Am I missing something?
You access the array outside of its bounds. The behaviour of the program is undefined.
sizeof does not yield the number of elements in an array. It yields the size of an object in bytes. When the size of the element is more than one byte - and int is more than one byte on most systems - then the number of bytes in the array is more than the number of elements.
A correct way to get the number of elements in an array is to use std::size:
int s = std::size(arr);
Since you use only arr[i] and not i itself, you can write for (auto a : arr). This will respect the array bounds, you don't need to calculate the maximum index. Hence, it avoids the wrong calculation (which the other answers fix)
Maybe there are other ways to get the size of an array but for now this will do :
int s = sizeof(arr)/sizeof(arr[0]);

In this Fibonacci function which works, doesn't it pass the number of elements that can be stored in the array? [duplicate]

This question already has answers here:
Accessing an array out of bounds gives no error, why?
(18 answers)
Closed 3 years ago.
This is my code and it works. For example, when n=5, it returns 5. Since n=5, and defining arr[0] = 0, isn't there only four more spaces left to store the elements from arr[1] to arr[4]?
However, it seems like 6 elements(from arr[0] to arr[5]) could be stored and I do not know how.
int fibonacci(int n) {
int i;
int arr[n];
arr[0] = 0;
for(i=1; i<=n; i++)
{
if(i==1)
{
arr[i] = 1;
}
else
{
arr[i] = arr[i-2] + arr[i-1];
}
}
return arr[n];
}
This is undefined behaviour.
Behind the scenes
What's going on behind the scenes is that 5 ints are allocated on the stack. Lets say they're put at address 0x20 to 0x30. The array notation [] is really syntax sugar for using pointers. This means that arr[0] really is a pointer to 0x20. Your array in this case translates to the following addresses (because sizeof(int) = 4):
arr[0] = (arr+0) = 0x20
arr[1] = (arr+1) = 0x24
arr[2] = (arr+2) = 0x28
arr[3] = (arr+3) = 0x2C
arr[4] = (arr+4) = 0x30
This is all fine and good, you're operating on allocated memory. What happens when you try to write to index 5? Well, the same thing, basically:
arr[5]= (arr+5) = 0x34.
It's still just a pointer to an address that you want to write to. The problem is, however, that you haven't told anyone that you intend to write to that address - the memory has not been set aside for you.
If it has not been set aside for anything else, this will likely turn out alright - as you observe.
However if it has it can turn into all sorts of strange behavior depending on what it was used for. Maybe it has been set aside and is just not used yet, maybe it has been used but is simply overwritten and will not be checked again. The point is we don't know! Therefore it is dangerous and undefined.
The reason why it even works in the first place, is that - again - it is just dereferencing a pointer. And dereferencing a pointer should work, as you're trusted to know more that the compiler. It may warn you that you probably shouldn't do this, but it's not an error per se.
What you should do instead
When working with variable length arrays in C++, one should use the modern tools available since C++11. In this scenario, it's advisable to use std::vector instead. If you use the at function rather than [] it checks whether the index is inside the bounds.
Its unexpected behavior, even warn by C++ documents, maybe they removed out-boundary checking. Not only that, you even assign to it. So better to be under range.
#include <iostream>
using namespace std;
int main(){
int i, n = 5;
int arr[n];
arr[0] = 0;
arr[1] = 1;
for(i=2; i<n; i++){
arr[i] = arr[i-2] + arr[i-1];
}
arr[8]=99; //assigning out of range
//accessing out of range
for(i=0;i<20;i++){
cout<<arr[i]<<" ";
}
}
#OUT: 0 1 1 2 3 0 211951624 1 99 32766 -484579280 32766 -1740650656 32767 -484579408 32766 5 0 5 0
You are facing issues with the int array. int arr[n]; is not valid C++ code if n is not fix at compile time. You can rewrite your code with the help of std::vector as
int fibonacci(int n) {
std::vector<int> arr(n);
arr[0] = 0;
for(int i=1; i<=n; i++)
{
if(i==1)
{
arr[i] = 1;
}
else
{
arr[i] = arr[i-2] + arr[i-1];
}
}
return arr[n];
}
Comments:
Your code will use a lot of memory for large Fibonacci numbers, since you store the entire series. This is superfluous, since only have to store the last two entries of the Fibonacci series.
You don't have to loop over n in order to calculate the Fibonacci number. The Moivre-Binet formula gives a much quicker way of calculating these.
for(i=1; i<=n; i++)
the for loop wil run if i is less than or equal to n, which means that when n = 5, it will still run.

Printing out an entire array multiple times

I am wondering how you can print out an entire array multiple times in C++. Say you have the following array:
arr1 = [1,2,5,6,7,8]
and you want to print it out n times such that the output would be:
1 2 5 6 7 8
1 2 5 6 7 8
1 2 5 6 7 8
If n would be equal to 3. You could just code some number of for loops if n is a static integer, but what if it's dynamic?
I know you need to use a for loop for printing out all the contents of an array, but I'm not sure what you would do if you want to get the above output.
If n is dynamic, it doesn't matter. You can have a loop that prints the array n times quite easily:
void printInts(int* arr, size_t size) {
// some printing logic
}
int main() {
int arr[] = {0,1,2,3,4,5,6,7,8};
int n = 3; // could be anything really
for(int i = 0; i<n; i++) {
printInts(arr, 9);
}
}
The value of n doesn't really matter here: it should print n times.
If you're asking how do we know the size of arr if it's a dynamic array, that's actually pretty easy too:
Until C++11:
size_t size = sizeof(arr)/sizeof(arr[0]);
After C++11 you can use:
size_t size = *(&arr + 1) - arr;
You could do this in main() or even in printInts() if you want.
Note: keep in mind you can't get the size of a pointer allocated dynamically easily. If you allocate with say new, you'll have to keep track of the size of the array yourself.
You can't. The size of an array allocated with new[] is not stored in any way in which it can be accessed. Note that the return type of new [] is not an array - it is a pointer (pointing to the array's first element). So if you need to know a dynamic array's length, you have to store it separately.
If what you're asking is how to get user input:
int n;
cin >> n;
for (int i = 0; i < n; i++) {
// print array
}
I think you know how to use for loop.
int rows;
int arr1 [5] = {1, 2, 3, 4, 5};
cout << "Enter number of rows? ";
cin >> rows;
for(int row=1; row<=rows;row++) {
for(int index=0;index<=4;index++) {
// print the array index here and space after a digit
}
// print line-break here
}

Explanation of undefined behaviour, and whether it's truly undefined

I have dynamically allocated memory for an array of 5 elements, then tried to print its elements to std::cout, which should have left me with a fairly straightforward result. I got something else instead, and it left me with some questions.
My code:
#include <iostream>
int main()
{
int *array = new int[5];
int array_size = sizeof(array);
for (int index = 0; index < array_size; index++) {
std::cout << array[index] << "\n";
}
std::cout << "\nThe array is " << array_size << " elements long.";
return 0;
}
This is what this resulted in:
0
0
0
0
0
0
132049
0
Now, I understand this isn't how things are done, but such a result has left me with several questions.
Why is the size of the array 8, and not 5? Originally, I thought that it's becase the memory according to powers of 2, but I have a feeling I'm wrong.
What's with 132049?
Your code has two problems, each of them leading to undefined behavior.
First, sizeof(array) is the size of the pointer in bytes, not the array. On your machine, a pointer happens to take 8 bytes. This causes out-of-range access, which is undefined behavior.
Second, new int[5] gives you an uninitialized array. Accessing the value of an uninitialized object is undefined behavior.
To fix these problems, first note that the size of an array returned by new is lost. sizeof cannot help you. You can only provide this information yourself. Then you have to initialize the elements.
#include <iostream>
int main()
{
int *array = new int[5]{}; // note: initialization
int array_size = 5;
for (int index = 0; index < array_size; index++) {
std::cout << array[index] << "\n";
}
std::cout << "\nThe array is " << array_size << " elements long.";
return 0;
}
This is code is guaranteed to print
0
0
0
0
0
The array is 5 elements long.
(As long as there is enough memory and output succeeds)

Multiplying and displaying the values of an array in C++

I am trying to complete a C++ exercise in which an array is displayed and the user is prompted to input a multiplier, which will result in the initial numbers that were displayed being multiplied by the user's input. Here is the code that I have so far:
#include <iostream>
using namespace std;
int main()
{
int array[5] = { 1, 2, 3, 4, 5 };
for (const auto& a : array)
{
std::cout << a << std::endl;
}
double multiplier;
cout << "Input a multiplier: ";
cin >> multiplier;
for (int array = 1; array <= 5; ++array)
{
array == multiplier * array;
std::cout << array << std::endl;
}
}
When it runs, it prints the correct array, with a newline being created after each array value, and prompts the user for the multiplier. However, when the multiplier is inputted, the values do not change. Here is an example output:
1
2
3
4
5
Input a multiplier: 2
1
2
3
4
5
The goal is to get this output:
1
2
3
4
5
Input a multiplier: 2
2
4
6
8
10
Any help or code improvement would be appreciated, as figuring out how to multiply and display the multiplied values is the only thing needed to be done in order to complete the exercise. Thank you in advance!
There are three big issues with your code:
1. Naming conventions.
Do not name your array and your temporary for loop variable the same thing. This will cause an issue further down the line, which I'll illustrate.
2. Incorrect operator
As minterm has mentioned, you are using a comparison operator instead of the equal operator. But that alone will not fix your issue.
3. Not accessing array values
You are not actually multiplying the array values with the multiplier. You have to access the elements, which means you cannot start the index at 1.
for (int i = 0; i < 5; i++){
array[i] *= multiplier
cout << array[i] << endl;
}
Use = instead of ==. Currently, it just evaluates a logical statement instead of setting it to a new value.
Also, you need to change "int array" in the for loop to a different name so as to not get it confused with the array called array. Call the variable in the for loop something else, like "int i".
So then the line in question would not be "array == multiplier*array", but instead something like "int j = multiplier * array[i]", and then have it print out j instead of array.
A simpler approach would be to use another range-based for loop instead of the indexed version:
for (auto& a : array)
{
a *= multiplier;
std::cout << a << std::endl;
}
If you don't need to update the array itself (after all, your code doesn't actually read from the array again), then you a very similar, though more straightforward version might be applicable:
for (const auto& a : array)
{
std::cout << a * multiplier << std::endl;
}
You have quite a few things going on that are incorrect.
First is your over scoping (maybe not the right term) the array value.
You have an array value declared outside of your for loop, and you're using the value array again inside of the for loop for the counter. So you're not actually doing anything to your array. You're actually trying to do something to the initialization variable.
Second you're using == instead of =
You're using a comparison operator (==) instead of an assignment operator (=), but there's other big no no's going on.
array is an int[5] not just an int
To actually modify each element of your array, you need to reference the index by saying array[index] where index is a value of 0 to length of array - 1, so 4 in your case.
You're using a double multiplier and trying to apply it to an int *
If you try to multiply by a double value like 2.5, your array values are going to be int's and not a number with a decimal value. You should make your int array [] into a double array []
Not accessing your array, starting from index 0
When looping through an array, the first index is always 0. You're using array = 1 and array <= 5. This would skip your first index, and give you an out of bound index at the end. You should use int index = 0; index < 5; index++; instead.
You're trying to print array
Since array is an int [] printing array in the for loop like that will just give you the address of where the array is. You'll have to either print out each index or use the enhanced for loop method after you've applied your multiplier.
If you're wanting to use your above implementation, do this
for (int index = 0; index < 5; index++)
{
array[index] *= multiplier; // Or you can use array[index] = multiplier * array[index]
std::cout << array[index] << std::endl;
}