Array Initialization giving garbage values [duplicate] - c++

This question already has answers here:
Initialization of all elements of an array to one default value in C++?
(12 answers)
Closed 8 years ago.
I want to initialize all the array elements with just one value so I wanted to use option 1, the shorter version. But that does not seem to working. However option 2 works. Can anybody please explain what's going wrong when I try to initialize via option 1..
int main()
{
int arr[5] = { 2 }; // option 1
int arr1[5] = { 2, 2, 2, 2, 2 }; //option 2
for (int i = 0; i < 5; i++)
cout << arr[i] << " ";
for (int i = 0; i < 5; i++)
cout << arr1[i] << " ";
}

int arr[5] = { 2 };
You are providing initial value to first element only. In that case all elements are initialized by default to that type i.e 0 in your case.

Related

I have a dynamic array using the new operator, but only some of the elements contain a value and others dont. How do i check this? [duplicate]

This question already has answers here:
Uninitialized variable behaviour in C++
(4 answers)
Closed 6 days ago.
int *arr = new int[3];
arr[0] = 1;
arr[2] = 3;
for (int i = 0; i < arr.size(); i++)
{
if(arr[i] exists/initialized) // what would be the condition?
std::cout << arr[i] << " ";
}
I'm trying to print elements from the array that are initialized, and ignore elements that aren't.
So, the above code would print out:
1 -1231409875 3
I want to fix this so that it outputs:
1 3
You cannot read from uninitilized elements, theirs values is indeterminate and reading them is undefined.
You can use a bool array of same size, initialize all its elements to false and every time you initialize something in the other array you set the corresponding element to true.
int* arr = new int[3];
bool* has_value = new bool[3]{};
arr[0] = 1;
has_value[0] = true;
arr[1] = 42;
has_value[1] = true;
for (size_t i= 0;i<3; ++i) {
if (has_value[i]) std::cout << arr[i] << " ";
else std::cout << "* ";
}
As mentioned in comments, if you use a contiguous portion of the array starting at the first element, it is sufficient to store the number of already initialized elements.

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]);

Looping using pointers and arrays [duplicate]

This question already has answers here:
What happens if I increment an array variable?
(5 answers)
Increment operator on pointer of array errors?
(2 answers)
Closed 2 years ago.
I just got into learning pointers in C++ and I have been trying some different instances of using them to get a better understanding to them but there is some I go into that seems a little weird to me.
The code below is for looping through an array and what I assumed after it ran well that arr is just a pointer to the beginning of the array (because I could do ptr = arr;.
int size = 3;
int arr[size] = {50, 30, 20};
int *ptr;
ptr = arr;
for (int i = 0; i < size; i++) {
std::cout << *ptr << std::endl;
ptr++;
}
But When I try looping through the same array but with using arr instead of ptr without assigning it to arr it gave me an error (lvalue required as increment operand) referring arr++.
This is the Code.
int size = 3;
int arr[size] = {50, 30, 20};
for (int i = 0; i < size; i++) {
std::cout << *arr << std::endl;
arr++;
}
I don't understand why the first one work and the second doesn't although they are both pointers(as far as I know).

Why size of c++ dynamic array is not changed? [duplicate]

This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 2 years ago.
Hi guys I tested c++ dynamic array and I changed 2 inputs every time.
but array size is not changed. Why?
#include <iostream>
using namespace std;
int main()
{
int r = 0;
int c = 0;
int cnt = 1;
cin >> r;
cin >> c;
int** arr1 = new int* [r];
for (int i = 0; i < r; i++)
{
arr1[i] = new int[c];
}
cout << sizeof(arr1) << endl;
cout << sizeof(arr1[0]);
}
I knew that If I entered two value 3 and 4 then results are 3 and 4
but the results are 4 and 4
You are testing the size of a pointer which is always 4 on 32 bit.
That's because sizeof is a compile time thing; it cannot determine an array size.
Generally arrays do not carry size information, that is why functions like strlen need a null terminator.
Suggestion: use std::vector.

C++ unable to index an element of an array using a nested loop [duplicate]

This question already has answers here:
Split an Integer into its digits c++
(12 answers)
Closed 3 years ago.
I am having difficulty understanding how i can fix this this error "array1[i][j] = expression must be a pointer-to-object type. I have searched the error but i am unable to apply the solutions to my code snippet.
int main(){
int array1[]= {1234,4321}; //{1234,4321};
int array2[]= {2345,3214}; //{2345,3214};
int counter = 0;
int arr_element = sizeof(array1);
int arr_index = sizeof(array1)/sizeof(*array1);
for(int i = 0, count1 = arr_index; i < count1; i++ ){
for(int j = 0, count2 = 4; j < count2; j++){
cout << array1[i][j] << endl;
}
}
return 0;
}
What i would like to do is be able to print out the elements in array1; for example, i would like this output: 1,2,3,4,4,3,2,1. From my understanding, the int a needs to be a pointer. I i added * in front of the array (*array1) and in front of the int (int**), but with no luck. Thank you for your time.
Ypu are using one dimensional array, What you are wishing for is a 2dimensional array and that's how you declare it
int array1[][]= {{1,2,3,4},{4,3,2,1}};
int array2[][]= {{2,3,4,5},{3,2,1,4}};