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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:30:29+00:00 2026-05-15T21:30:29+00:00

i have trying to imlement IntSetArray in c++ it compiles fine but result is

  • 0

i have trying to imlement IntSetArray in c++ it compiles fine but result is wrong first 300 is ok and other numbers are below zero something very strange numbers .e.g -8231313 something like this)
what is wrong? it is code

#include <iostream>
using namespace std;
int quantity=10;
class  Set
{
private :
    int n,*x;
public:
    Set(int maxval){
        x=new int[quantity+1];
        n=0;
        x[0]=maxval;

    }
    int size(){ return n;}
    void insert(int t){

         for (int i=0;x[i]<t;i++)
         {

              if (x[i]==t)
                   return ;
               for (int j=n;j>=i;j--)
                   x[j+1]=x[j];
               x[i]=t;
         }

               n++;




    }

     void display()
     {
           for (int i=0;i<n;i++){
               cout<<x[i]<<"  "<<"\n";
           }
     }



};

int main(){

    Set s(300);
    s.insert(123);
    s.insert(45);
    s.insert(89);
    s.insert(50);
    s.insert(13);
    s.insert(19);
    s.display();

     return 0;
}
  • 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-15T21:30:30+00:00Added an answer on May 15, 2026 at 9:30 pm

    Think through what happens the first time you try to insert something. x[0] contains 300 and t, what you are trying to insert, is 123.

    The first statement in the insert method is this:

         for (int i=0;x[i]<t;i++)
    

    This for loop increments i while the ith element of x is less than t. But the 0th element of x is 300, which is not less than 123, so the loop never executes at all. Since in the constructor you only initialized the first element of x, the remainder have garbage values which are never changed.

    I think that you most likely don’t want the second loop to be inside the first loop anyway. What it seems that you are trying to do with the outer loop is find the first position in x where the value is greater or equal to t, and then the inner loop shifts everything down and inserts t. Then what you should be doing is this:

    void insert(int t){
    
         int i; // Declare this outside the first loop so that it
                // remains accessible afterwords
    
         for (i=0;x[i]<t;i++)
         {
           // Do nothing; the whole point is to increment i
         }
    
         // Now i contains the first index of x where x[i] >= t
         // So now do the shift and insert:
    
         if (x[i]==t)
           return ;
    
         for (int j=n;j>=i;j--)
           x[j+1]=x[j];
    
         x[i]=t;
         n++;
    }
    

    A different, and potentially easier to understand, way to write this:

         int i;
         for (i=0;x[i]<t;i++)
         {
           // Do nothing; the whole point is to increment i
         }
    

    Is this:

         int i = 0;
         while (x[i] < t) ++i;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.