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 6070301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:55:03+00:00 2026-05-23T09:55:03+00:00

Having some issues with one small function I’m working on for a homework assignment.

  • 0

Having some issues with one small function I’m working on for a homework assignment.

I have a static array size of 20 (shelfSize), however, I only need to use a max of 10 elements. So I don’t have to worry about out of bounds etc (the entire array of 20 is initialized to 0).

What I am looking to do is insert an integer, booknum, into an element of an array it receives as input.

This my current logic:

void insert_at(int booknum, int element){
for(int i=element+1; i < shelfSize; i++)
    bookshelf[i+1]=bookshelf[i]
bookshelf[element]=booknum;
}

so let’s say I have the this array:

[5,4,3,1,7]

I want to insert an 8 at element 1 and have the array turn to:

[5,8,4,3,1,7]

Technically, everything after the final element 7 is a 0, however, I have a separate print function that only prints up to a certain element.

No matter how many times I take some pencil and paper and manually write out my logic, I can’t get this to work.

Any help would be appreciated, thanks.

  • 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-05-23T09:55:03+00:00Added an answer on May 23, 2026 at 9:55 am

    You should start from the end of the array, this should word for you:

    void insert_at(int booknum, int element)
    {
        for (int i = shelfsize-1;i>element;i--)
            bookshelf[i] = bookshelf[i-1];
        bookshelf[element] = booknum;
    }
    

    Also I recommend that you get used to handling illegal values, for example, what if a user entered 21?

    The optimized code would be:

    bool insert_at(int booknum, int element)
    {
        if (element>=shelfsize-1)
            return false;
        for (int i = shelfsize-2;i>element;i--)
            bookshelf[i] = bookshelf[i-1];
        bookshelf[element] = booknum;
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, I'm having some issues and I believe it's a CSS one. Here is
I have been having some issues with LINQ-To-SQL around memory usage. I'm using it
Having some issues getting this MPMoviePlayerViewController to work. I have two sample URLs pointing
We're having a small issue and could use some help - we have the
I'm having some issues with dijit.form.Select items. In a page i have two of
I'm having some issues with maintaining scroll bar locations. The Background I have a
I am having some issues with Microsoft Explorer 6/7 and the jQuery clone function.
Ok I am still having some issues with this code below. I have received
Having some issues getting my head around the differences between UTF-8, UTF-16, ASCII and
Having some issues with the ... in ObjectiveC. I'm basically wrapping a method and

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.