Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8590949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:24:48+00:00 2026-06-11T23:24:48+00:00

This is elementary, but my googling just doesn’t cut it. I know I have

  • 0

This is elementary, but my googling just doesn’t cut it. I know I have to do something else to shift the values of an array one by one, but the below coding gives me the same values for items[k] to items[infinity] all equaling items[k]. What I don’t understand is how to preserve the original k+1 value while I copy the k value into the k+1 slot.

if ( i < numItems) //if i is inside the used boundaries of the array
{
    for (int k = i; k < numItems; k++) //shift the array values from point i
    {
                double temp = 0.0;
        temp = items[k];
        items[k+1] = temp;
    }

    items[i] = value; //and insert value into i
}

Does it has to be a recursive method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:24:49+00:00Added an answer on June 11, 2026 at 11:24 pm

    An easy option would be to iterate through the array in reverse

    for (int k = numItems; k > i; k--){        
        items[k]=items[k-1];
    }
    

    Option 2:

    If you want to keep your method intact then you can also use the temp variable differently

    before your for loop initialize temp to

    double temp = items[i];
    

    and then in the loop you can use temp to store the [k+1] value in temp rather than storing the [k] value.

    items [k+1] = temp;
    temp = items [k+1];
    items[k+1] = items[k];
    

    also you should watch your boundaries so that k+1 is not going past the last element in the array. You could use something like numItems – 1 with a check before, to ensure that the array is not empty.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be a elementary question but why is better to use something like
Ok I know this practically sounds elementary, but I don't understand what I'm not
I know that this is elementary, but I'm completely unable to pass this as
Sorry if this is an elementary question but I've just started to consider whether
This seems like an elementary issue but it has me stumped. I have a
I know this should be a pretty elementary issue to fix, but 1) I'm
I know this is probably elementary to unix people, but I haven't found a
This question might be kind of elementary, but here goes: I have a SQL
I know this is probably very elementary, but I can't seem to figure it
This is probably one of the most elementary things in F#, but I've just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.