• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

A little C++ help please?

Techie333

Platinum Member
I can't seem to figure this out: What happens in the first loop, I understand the second loop, but it takes in the array and then assigns it to another array or what? And is it multiplying the array by 2 to make it an array of 10?



#include<iostream>
using namespace std;
const int max=5;
int main()
{
int i;
int array[max*2];

for(i=0;i<max;i++)
{
cin >> array[2*i] >> array[2*i+1];
}
for(int j=max;j>0;j--)
{
cout << *(array+j);
}


return 0;
}
 
Techie333->setForum("SOFTWARE - APPS, PROGRAMMING AND GAMES");
Techie333->setStatus(Status::N00b);
 
Don't see what's difficult about it... it reads in two values and inputs them into two consective spots in the array, then repeats until the array is full.
 
Originally posted by: Techie333
And is it multiplying the array by 2 to make it an array of 10?

Its a sh!tty and confusing implementation, but I think what its trying to do is load in two related values at once (a psuedo-node). The "max" number represents the number of "nodes" it wants to read in.

And btw, wrong forum.
 
Originally posted by: Krakerjak
Originally posted by: chuckywang
*(array+j)

This is the part confusing me.


Yeah, I've never seen anything like that before

When you define an array, it is really just a block of allocated memory. The array variable is actually a pointer; for example, when you type in "ArrayName[3]" what you're saying is "Go to the memory at pointer ArrayName, then move up 3 blocks to get to the value I want" The program "knows" how big each block is because you you gave it a type when you defined it (int, char, bool, whatever).

In this code, they're using "pointer arithmetic"... you can add or subtract to a pointer to move between blocks of memory. Its a useful trick for when you want to cycle through all the values of an array... just keep adding one to the pointer.
 
srry bout the wrong forum guys.....ok i think i understand it....its puttin two values per loop. The cout function.....I have no clue....a little more help? BTW, IT COMPILES!

OUTPUT WHEN I PUT IN NUMBERS 1-10:
1
2
3
4
5
6
7
8
9
10
65432Press any key to continue
 
Originally posted by: TheLonelyPhoenix
Originally posted by: Krakerjak
Originally posted by: chuckywang
*(array+j)

This is the part confusing me.


Yeah, I've never seen anything like that before

When you define an array, it is really just a block of allocated memory. The array variable is actually a pointer; for example, when you type in "ArrayName[3]" what you're saying is "Go to the memory at pointer ArrayName, then move up 3 blocks to get to the value I want" The program "knows" how big each block is because you you gave it a type when you defined it (int, char, bool, whatever).

In this code, they're using "pointer arithmetic"... you can add or subtract to a pointer to move between blocks of memory. Its a useful trick for when you want to cycle through all the values of an array... just keep adding one to the pointer.

OKAY cool, but wat about the output?

 
Originally posted by: Techie333
Originally posted by: TheLonelyPhoenix
Originally posted by: Krakerjak
Originally posted by: chuckywang
*(array+j)

This is the part confusing me.


Yeah, I've never seen anything like that before

When you define an array, it is really just a block of allocated memory. The array variable is actually a pointer; for example, when you type in "ArrayName[3]" what you're saying is "Go to the memory at pointer ArrayName, then move up 3 blocks to get to the value I want" The program "knows" how big each block is because you you gave it a type when you defined it (int, char, bool, whatever).

In this code, they're using "pointer arithmetic"... you can add or subtract to a pointer to move between blocks of memory. Its a useful trick for when you want to cycle through all the values of an array... just keep adding one to the pointer.

OKAY cool, but wat about the output?

It uses pointer arithmetic again; what I think was intended was for the program to go to the last element of the array and cycle back to the first, displaying them all, but it oversteps the bounds of the array and should never actually display the first one. It should read like this:

for(int j=max-1;j>=0;j--)
{
cout << *(array+j);
}
 
OKAY I FIGURED IT OUT. The output is simply outputtting array positions 5 to 1, which is why the output was 65432. Thx, TheLonelyPhoenix

EDIT: Nope, the program was designed by professor and he asked us to figure it out....no mistakes...
 
Back
Top