Pointer Arithmetic using arrays - c++

My question is that if you were print out the resulting *(ary + i), which I know is another way to say ary[i] value, would the following output be a hexadecimal/garbage data or would that specific index be assigned a new value based on the result computed from *ary + i? This whole pointer stuff still throwing me off.
int ary[] = [7, 5, 3, 1, 2, 4, 6, 8];
for (int i = 0; i < 8; i++)
{
*(ary + i) = *ary + i;
}

ary decays to a pointer in the expressions you posted. And since it always produces the same address (the first element), *ary will evaluate to 7 at every iteration.
Since you understand that *(ary + i) is equivalent to ary[i], you should now gather that your loop body is akin to this:
ary[i] = 7 + i;

int ary[] = [7, 5, 3, 1, 2, 4, 6, 8]; is wrong assignment method,
one must use { } to assign values to array.
So, correct way to define is:
int ary[] = {7, 5, 3, 1, 2, 4, 6, 8};
Here *(ary + i) is equivalent to ary [i] , and *ary + i is equivalent to ary [0] + i.
Addressing your question, no it won't throw hex or garbage, you have dereferenced a pointer, so it will give the data stored at that location.
Code:
#include <iostream>
int main ()
{
int ary[] = {7, 5, 3, 1, 2, 4, 6, 8};
for (int i = 0; i < 8; i++)
{
*(ary + i) = *ary + i;
}
for (int j=0;j <8;j++)
{
std::cout <<ary [j];
}
}
Output:
7891011121314
Hope you draw the correlation.
if this helps great, if it doesnot, indicate so in comments, willing to edit

Related

Problem with iteration on array in For loop

I'm having a trouble for a long time in a problem related to array and loops.
Assume an array like:
int arr[] = {2, 3, 5, 3, 6, 8, 10, 1, 9};
The program should print the expected column as shown (note that the table is an idea for clarification of what I actually want to achieve):
max: expected arr[index] for max
------------------------------------
2 : 3 5 value of: arr[0]
3 : 6, 8, 10 value of: arr[3]
1 : 9 value of: arr[7]
This is what I've tried so far:
#include <iostream>
int main(void) {
int arr[] = {2, 3, 5, 3, 6, 8, 10, 1, 9};
int max = arr[0];
int i = 1, it = i;
for (; i <= max; i++) {
if (i == max) {
std::cout << arr[i] << std::endl;
max = arr[it + 1]; // when loop end has come, increment max to next element value
it = i + 2; // incrementing iterator by 2 next element position (after max)
} else {
std::cout << arr[i] << ' '; // when loop is executing
}
}
return 0;
}
What it actually prints which is unexpected V/S what it should print:
3 5 | 3, 5
3 6 8 | 6, 8, 10
10 1 9 | 1, 9
10 | <nothing>
The program is about to get max value and print the next elements until max value reaches the number of element position.
Where the problem's occurring and how to fix it? Please let me know.
So I rewrote your code, frankly I couldn't follow it, it seems a bit confused with variable names that seem a bit off.
Here's some working code. The two key concepts in the code are the place where each block starts (the variable start) and the size of each block (the variable count). Plus I use the size of the whole array sizeof(arr)/sizeof(arr[0]) to terminate the outer loop. Something that wasn't present in your code. If you are using C++17, you can use std::size(arr) instead of sizeof.
int main(void) {
int arr[] = {2, 3, 5, 3, 6, 8, 10, 1, 9};
size_t start = 0;
while (start < sizeof(arr)/sizeof(arr[0]))
{
int count = arr[start];
for (int i = 0; i < count; ++i)
{
std::cout << arr[start + i + 1] << ' ';
}
std::cout << std::endl;
start += count + 1;
}
}
The output is
3 5
6 8 10
9

c++ array copy gives weird result

I have known java for a while and I was trying to translate a java program i wrote to c++ but the copy function gives an odd result:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
long gcd2(long a, long b) {
if ( a == 0 )
return b;
return gcd2(b%a,a);
}
long gcd(long nums[]) {
long ans = nums[0];
int len = sizeof(nums);
for (int i = 1; i < len; i++)
ans = gcd2( nums[i] , ans );
return ans;
}
string com(string s) {
s = s+",";
return (","+s);
}
void printa(long array[]) {
for (int i = 0 ; i < sizeof(array); i++)
cout << array[i] << ", ";
cout << "\n";
}
int main()
{
int length;
cin >> length;
long input[length];
for (int i = 0; i < length; i++)
cin >> input[i];
string possible = "";
int ans = 0;
for (int a = 0; a < length; a++) {
for (int b = length; b > a; b--) {
long arr[b-a];
std::copy(input+a,input+b,arr);
printa(arr);
long gcdans = gcd(arr);
if (possible.find( com(gcdans+"") ) == -1 ) {
possible += com(gcdans+"");
ans++;
}
}
}
cout << (ans);
return 0;
}
I give it the input of:
4
9 6 2 4
and it returns:
9, 6, 2, 4, 140725969483488, 4197851, 9, 6,
9, 6, 2, 4197851, 9, 6, 2, 4,
9, 6, 2, 4197851, 9, 6, 2, 4,
9, 4197851, 9, 6, 2, 4, 140725969483488, 4197766,
6, 2, 4, 4197851, 9, 6, 2, 4,
6, 2, 4, 4197851, 9, 6, 2, 4,
6, 4197851, 9, 6, 2, 4, 140725969483488, 4197766,
2, 4, 6, 4197851, 9, 6, 2, 4,
2, 4197851, 9, 6, 2, 4, 140725969483488, 4197766,
4, 4197851, 9, 6, 2, 4, 140725969483488, 4197766,
1
the number at the very end is what i want the program to output at the end, all the numbers above are me test printing the array to see its contents. Basically I am trying to copy a range of the array(for example (2,3,4) from (1,2,3,4,5,6)) But it gives weird numbers like 140725969483488 and 4197766 when the only numbers I input are 9 6 2 4
Variable length arrays is a C++ extension, not standard C++. If your compiler will allow them, then OK. However standard C++ would use an std::vector container which is dynamically sized at runtime, meaning you can initialise them with any size or numbers at runtime, and add anything you want at runtime.
Also note when passing an array in C++ to functions which take an array argument always (with the exception of explicitly declared sized reference to an array) gets passed as a pointer, so you can't know the size of the array once passed as an argument. So this:
void printa(long array[])
{
for (int i = 0 ; i < sizeof(array); i++) {}
// At this point of the code the sizeof(array) will return the size of
// a pointer, usually 4 or 8 bytes.
// It's a quirk that this happens, and is a holdover from C.
}
By taking an argument of std::vector you can know the size of the array. You can take the argument by value or by reference or pointer.
void printa(const std::vector<long>& array)
{
for (int i = 0 ; i < array.size(); i++)
{
cout << array[i] << ", ";
cout << "\n";
}
}
This is the better way to do it. If you want to use a C array or raw array the way you did, you will have to pass both the array and the size of the array as separate arguments.
Also, about the variable length array extension feature, I'm not sure whether it is reliable or not because I've never used the extension. Again, standard C++ requires that size of arrays are constant values, (known at compile time). Edit: actually (known at compile-time) is a bad description because:
int main()
{
int num = 6;
int myarray[num]; // In standard C++ this won't compile
//but
const int num = 6;
int myarray[num]; // Will
}
And one last thing, as SolutionMill pointed out, even if the sizeof(array) does give the right size and not the size of a pointer, it is the size given in bytes, not the number of elements, which was not you were wanting in:
for (int i = 0 ; i < sizeof(array); i++)
If the array is of 2 elements of 32 bit int, then the sizeof() operator will return size 8. A common but by no means pretty way to get the number of elements in an array is something like sizeof(array) / sizeof(array[0])

Shifting Elements to the Right After a Certain Index

I have an array _data and I'm trying to add a number to the array at a certain position. Every number after the index of position should be shifted one index to the right. I'm having trouble conceptually in doing this.
This is what I have as of now:
void list::add(int num, size_t position)
{
for (int i = 0; i < _size; i++)
{
_data[position + i + 1] = _data[position + i];
}
_data[position] = num;
}
For example, say num = 9 and position = 3 and the given values in the array are {1, 3, 4, 5, 6, 7}. I would want it to be {1, 3, 4, 9, 5, 6, 7}. What ends up happening, though, is that it grabs the number that has already been replaced and puts that back into the array. (ex 1, 3, 4, 9, 5, 5, 5}.
It's depending on type of your variable _data:
If it's an array:
for (std::size_t i = _size - 1; i != position; --i)
{
_data[i] = _data[i - 1];
}
_data[position] = num;
If it's a vector:
_data.insert(_data.begin() + position, num);
Anyway, it's recommended to use a std::vector instead of raw array, and it's NOT recommended to use underscore at the beginning of variable name

Swapping values in array C++

This is the code:
for (i = 0; i < size; i++)
{
swap(a[i], a[size - i - 1]);
}
It looks correct.. and it is because I have used it in other languages, but on C++ it doesn't work. Any suggestions? Thanks
Have you tried stepping through this program? Because at a glance, it doesn't look like it would work.
Like, for the following input, you'd get the following results:
a = {0, 1, 2, 3, 4};
i == 0: a = {4, 1, 2, 3, 0};
i == 1: a = {4, 3, 2, 1, 0};
i == 2: a = {4, 3, 2, 1, 0};
i == 3: a = {4, 1, 2, 3, 0};
i == 4: a = {0, 1, 2, 3, 4};
See what's happening? The numbers are getting swapped, and then swapped again.
Cutting it off half-way should fix the problem.
for (i = 0; i < size / 2; i++)
{
swap(a[i], a[size - i - 1]);
}
for (i = 0; i < size / 2; i++)
{
swap(a[i], a[size - i - 1]);
}
Since you will swap each item twice if you go i < size
Since you are using c++ you can use some awesome c++ features, like std::reverse:
std::reverse(&a[0], &a[0] + size);
Here is a live example.
You probably are looking for std::iter_swap. It swaps 2 values in an array using the appropriate iterators:
std::iter_swap(std::begin(a) + i, std::end(a) - i - 1);

Adding to Individual Array Elements - C++

I need to add either +0.25 or -0.25 to all elements within an array. Here is what I have so far. Any help would be appreciated.
int main() {
double i;
// Arrays
double x[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double x2[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for(i=1; i<=10; i++) {
const double pM[2] = {-1, 1};
int randoid = rand() % 2;
for(i=1; i<=10; i++){
x2[i] = x[i] + pM[randoid]*0.25; //Error Line
}
}
cout << x;
cout << x2;
}
I get this error at the marked line: "invalid types 'double[10][double] for array subscript"
The problem is that i is a double. Then you write x2[i].
It's not a very good error message; however with the [] operator, one of the operands must be a pointer and the other must be an integer. There is no implicit conversion of floating-point to integer when using this operator.
To fix this change double i; to int i;
Another issue is that your code accesses out of bounds of the arrays. double x2[10] means that there are 10 elements whose indices are 0 through 9. But your loop tries to write to x2[10]. This causes undefined behaviour, which could explain your strange output.
There is also a potential logic error. Maybe you meant to use a different variable for the inner loop than the outer loop. As it stands, the inner loop will take i to 11 (or 10 if you fix the code) and then the outer loop will be complete and not execute any more iterations.
Based on your description though, perhaps you only meant to have one loop in the first place. If so, remove the outer loop and just leave the contents there.
Also you do not need two separate arrays, you could just perform the addition in-place.
Regarding the output, cout << x and cout << x2 will output the number of the memory address at which the array is located. To output the contents of the array instead you will need to write another loop, or use a standard library algorithm that iterates over containers.
I see 3 issues -
Change the type of i to int.
x and x2 are arrays of size 10. You need to loop from i =
0 to i = 9. But you are looping from i = 1 to i = 10. x[10]
is out of bounds since arrays are 0 indexed.
cout << x - This is a wrong way to print an array. You need
to loop through the array and print - e.g. -
for(i = 0; i < 10; i++)
cout << x[i] << " ";
Try this, it works, I converted to C
int main( )
{
int i = 0;
// Arrays
double x[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
double x2[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for ( i = 1; i < 10; i++ )
{
const double pM[2] = { -1, 1 };
int randoid = rand( ) % 2;
for ( i = 1; i <= 10; i++ )
{
x2[i] = x[i] + pM[randoid] * 0.25; //Error Line
printf( "\nx[%d]==%2.2f", i, x[i] );
printf( "\nx2[%d]==%2.2f", i, x2[i] );
}
}
}