We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

# help (urgent)

on
Medium Priority
257 Views
I have a little problem concerning arrays.

Say I have an array called Numbers and it has 10 elements :

array pointer       contents
0                  3
1                  8
2                  12
3                  66
4                  7
5                  128
6                  1001
7                  45
8                  63
9                  1137

To access for example the third elemnt in the array, I have to write, cout << Numbers[2] << endl ;
and that would print 12.
But if I had two variables, x = 1 and y = 2 then typing, cout << Numbers[x+y] does not print 12, instead it prints 3.

Does anyone know why that is, and how can I correct this so that it prints the contents instead.

Cheers.

a.Al-allak@uea.ac.uk
Comment
Watch Question

## View Solution Only

Commented:
Could use some source code. Is x and y an int? how are you defining x and y?

Commented:
Yes, x and y are defined as integers.

Commented:

The way you discribe it there should be no problem... Can you post more of your code?

Philippe

Commented:
What compiler do you use?

Commented:
It is not possible to find error in your code until you post this code!
Commented:
That's normal you got that result...

Your X+Y value give a result of 3 So Numbers[3] is exactely 3!
What you want to have as a X+Y result is 2!!! Not 3! SO Numbers[2] will give you 12, and that's what you are expecting!

In fact, the error come from the 0 based organisation of your coordinate!

May the MFC be with you!
jmmougeolle@calva.net strikes back!

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
The problem you posted is NOT normal (as jmmougeolle said). In fact, x+y = 3, and therefore Numbers[x+y] should give 66, not 12 as you're expecting. But I'd recommend posting most of your program, so that I may be able to find the actual error.

Commented:
jmmougeolle said:

>That's normal you got that result...

>Your X+Y value give a result of 3 So Numbers[3] is exactely 3!
>What you want to have as a X+Y result is 2!!! Not 3! SO >Numbers[2] will give you 12, and that's what you are expecting!

>In fact, the error come from the 0 based organisation of your >coordinate!

I do not believe this answer to be correct for the following reason: If you want x+y to give you the answer 2 so the number  12 will be displayed, why was 66 not displayed when x+y summed to 3? 66 is the value in element 3 of the array and that is what should have been displayed if jmmougeolle's theory is correct. Just a thought....no offense is intended.
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile