Solved

help (urgent)

Posted on 1997-04-30
8
222 Views
Last Modified: 2010-04-10
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.

My email address is :

a.Al-allak@uea.ac.uk
0
Comment
Question by:9628657
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Expert Comment

by:SAS
ID: 1163124
Could use some source code. Is x and y an int? how are you defining x and y?
0
 

Author Comment

by:9628657
ID: 1163125
Yes, x and y are defined as integers.
0
 
LVL 2

Expert Comment

by:Philippe
ID: 1163126

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

 Philippe

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:AVaulin
ID: 1163127
What compiler do you use?
0
 

Expert Comment

by:michaelf
ID: 1163128
It is not possible to find error in your code until you post this code!
0
 
LVL 1

Accepted Solution

by:
jmmougeolle earned 10 total points
ID: 1163129
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!

Don't hesitate to ask for more info!


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

Expert Comment

by:Marauder010197
ID: 1163130
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.
0
 

Expert Comment

by:tooltown
ID: 1163131
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Arduino EDI - Programming language 3 114
Header of docx file 17 129
Embarcadero WebBroker REST server endpoint parameters 2 96
Why isn't object file created? 6 86
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question