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

  • Home
  • SEARCH
  • 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 6612033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:02:50+00:00 2026-05-25T20:02:50+00:00

Ok, this may seem trivial to some, but I’m stuck. Here’s the algorithm I’m

  • 0

Ok, this may seem trivial to some, but I’m stuck.

Here’s the algorithm I’m supposed to use:

Here’s a recursive algorithm. Suppose we have n integers in a non-increasing sequence, of which the first is the number k. Subtract one from each of the first k numbers after the first. (If there are fewer than k such number, the sequence is not graphical.) If necessary, sort the resulting sequence of n-1 numbers (ignoring the first one) into a non-increasing sequence. The original sequence is graphical if and only if the second one is. For the stopping conditions, note that a sequence of all zeroes is graphical, and a sequence containing a negative number is not. (The proof of this is not difficult, but we won’t deal with it here.)

Example:
Original sequence: 5, 4, 3, 3, 2, 1, 1

Subtract 1 five times: 3, 2, 2, 1, 0, 1

Sort: 3, 2, 2, 1, 1, 0

Subtract 1 three times: 1, 1, 0, 1, 0

Sort: 1, 1, 1, 0, 0

Subtract 1 once: 0, 1, 0, 0

Sort: 1, 0, 0, 0

Subtract 1 once: -1, 0, 0

We have a negative number, so the original sequence is not graphical.

This seems simple enough to me, but when I try to execute the algorithm I get stuck.

Here’s the function I’ve written so far:

//main
 int main ()
{
     //local variables
     const int MAX = 30;
     ifstream in;
     ofstream out;
     int graph[MAX], size;
     bool isGraph;

     //open and test file
     in.open("input3.txt");
     if (!in) {
         cout << "Error reading file. Exiting program." << endl;
         exit(1);
     }

     out.open("output3.txt");

     while (in >> size) {
         for (int i = 0; i < size; i++) {
             in >> graph[i];
         }

        isGraph = isGraphical(graph, 0, size);

        if (isGraph) {
             out << "Yes\n";
         }else
             out << "No\n";

     } 

     //close all files
     in.close();
     out.close();

     cin.get();
     return 0;
 }//end main

 bool isGraphical(int degrees[], int start, int end){

     bool isIt = false;
         int ender;      

     inSort(degrees, end);

     ender = degrees[start] + start + 1;

     for(int i = 0; i < end; i++)
            cout << degrees[i];
     cout << endl;

     if (degrees[start] == 0){
        if(degrees[end-1] < 0)
            return false;
        else
            return true;
     }
     else{

    for(int i = start + 1; i < ender; i++) {
        degrees[i]--;
    }
         isIt = isGraphical(degrees, start+1, end);
     }

     return isIt;
 }

 void inSort(int x[],int length)
 {
    for(int i = 0; i < length; ++i)
     {
         int current = x[i];
         int j;
         for(j = i-1; j >= 0 && current > x[j]; --j)
         {
                 x[j+1] = x[j];
         }
         x[j+1] = current;
     }
 }

I seem to get what that sort function is doing, but when I debug, the values keep jumping around. Which I assume is coming from my recursive function.

Any help?

EDIT:
Code is functional. Please see the history if needed.

With help from @RMartinhoFernandes I updated my code. Includes working insertion sort.

I updated the inSort funcion boundaries

I added an additional ending condition from the comments. But the algorithm still isn’t working. Which makes me thing my base statements are off. Would anyone be able to help further? What am I missing here?

  • 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-25T20:02:51+00:00Added an answer on May 25, 2026 at 8:02 pm

    Ok, I helped you out in chat, and I’ll post a summary of the issues you had here.

    1. The insertion sort inner loop should go backwards, not forwards. Make it for(i = (j - 1); (i >= 0) && (key > x[i]); i--);

    2. There’s an out-of-bounds access in the recursion base case: degrees[end] should be degrees[end-1];

    3. while (!in.eof()) will not read until the end-of-file. while(in >> size) is a superior alternative.

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

Sidebar

Related Questions

This one may seem trivial to some, but I'm trying to adopt good habits
This may seem a trivial question, but it's one that's bothered me a lot
This question may seem trivial, but I hope you won't ignore it. Before destroying
This may be something common and trivial, but I seem to be having trouble
I know this may seem trivial to some, other won't see the point, however
this may seem like a very simple question, but I want to dump some
This may seem trivial, but I can't figure it out how to 1) retrieve
This may seem straightforward but for some reason my emulator won't simulate any of
This may seem trivial, but i'm working on a project to be deployed exclusively
This may seem simple, but somehow it isn't. I have a table of historical

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.