What’s wrong with my code for summing odd-indexed elements?

0
13
Asked By CodingAdventurer92 On

Hey everyone! I'm trying to debug my code for calculating the sum of odd-indexed elements in an array, but it's not working according to AtCoder's requirements. Here's the link to my submission: https://atcoder.jp/contests/abc403/submissions/65401113. I thought I was doing it right, but I keep getting an error. Any insights would be appreciated!

3 Answers

Answered By TechSavvyBunny On

It looks like you might be adding values from even indices instead of odd ones. Since arrays start at 0, index 0 is even, so you'll want to start from index 1 and increase by 2 each time to get the odd-indexed elements.

Answered By ArrayNinja12 On

The key issue here is in how you're interpreting the term "odd-indexed". Since arrays are 0-based, index 1 is the first odd index. So your loop should begin at i = 1 and then add 2 with each iteration to sum up the odd-indexed positions.

Answered By DebuggingHero On

You're starting at i = 0 in your loop and incrementing by 2. This means you're summing values at 0, 2, 4, etc. Instead, begin at i = 1 and then add 2 to sum the odd-indexed values. Also, rather than creating a large array, just define one that fits the number of elements you need.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.