Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Array Addition
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Fashoomp




PostPosted: Tue Feb 26, 2008 6:08 pm   Post subject: Array Addition

I have an array thats 1..30, and all 30 contain numbers
I want another array to store the results of addition within the array.

I want addition_array (i) := num_array (i) + num_array (i+1)
Problem is for the final value, (i+1) is out of range.
Sponsor
Sponsor
Sponsor
sponsor
syntax_error




PostPosted: Tue Feb 26, 2008 6:11 pm   Post subject: RE:Array Addition

trace though your program :L

you will see that the reason why you get out of range is you are looking for the 31st index; doesnt exist.

I do that that is your problem.
Tony




PostPosted: Tue Feb 26, 2008 6:11 pm   Post subject: RE:Array Addition

well if your range is up to 30 and you want i + 1 to be the final value, what should i go up to? that is:

i + 1 = 30
i = ?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
OneOffDriveByPoster




PostPosted: Tue Feb 26, 2008 6:12 pm   Post subject: Re: Array Addition

So... your addition_array has one less entry, or you assume that the missing value is 0, or that the source array is circular, etc.
Fashoomp




PostPosted: Tue Feb 26, 2008 6:15 pm   Post subject: Re: Array Addition

That doesn't help, since my code is (i+1) - (i)

I need to subtract value 2 from 1, then 3 from 2, then 4 from 3, etc.

The array of 30 was just an example. My array size is actually the length of a word I have entered.
Tony




PostPosted: Tue Feb 26, 2008 6:17 pm   Post subject: RE:Array Addition

it doesn't make a difference. Just make sure that

max_i + 1 == length_of_word
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Fashoomp




PostPosted: Tue Feb 26, 2008 6:18 pm   Post subject: Re: Array Addition

Yes, the missing value in the addition array is a 0.
syntax_error




PostPosted: Tue Feb 26, 2008 6:19 pm   Post subject: Re: Array Addition

Fashoomp wrote:

Yes, the missing value in the addition array is a 0.


care to clarify?
Sponsor
Sponsor
Sponsor
sponsor
Fashoomp




PostPosted: Tue Feb 26, 2008 6:20 pm   Post subject: Re: RE:Array Addition

Tony @ Tue Feb 26, 2008 6:17 pm wrote:
it doesn't make a difference. Just make sure that

max_i + 1 == length_of_word


But thats impossible. The fact that its i+1 means that it will always exceed the value of the for loop (sorry if I never mentioned thats what I'm using)
Tony




PostPosted: Tue Feb 26, 2008 6:23 pm   Post subject: RE:Array Addition

hmm... really?

let word = "think"
then length_of_word := length(word)
length_of_word == 5

let max_i = length_of_word - 1
max_i == 4

max_i + 1 == length_of_word
4 + 1 == 5

QED
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Fashoomp




PostPosted: Tue Feb 26, 2008 6:23 pm   Post subject: Re: Array Addition

Sorry for double post

Would a conditional loop be better for adding my values to the array? Since I can just tell the loop to exit at word_length - 1, so my variable + 1 is my last array value?

That sounds right so I'm gonna go with that.
syntax_error




PostPosted: Tue Feb 26, 2008 6:24 pm   Post subject: RE:Array Addition

glad you can think it out. Try that again next time.
Nick




PostPosted: Tue Feb 26, 2008 6:43 pm   Post subject: RE:Array Addition

code:

for i:2..length of word
word(i)+word(i-1)
end for


or

code:

for i 1..length of word + 1
word(i) + word (i+1) %this will produce an error though
end for
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: