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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:22:06+00:00 2026-06-14T03:22:06+00:00

I’ve been trying to get better at c++, so I’ve been solving problems designed

  • 0

I’ve been trying to get better at c++, so I’ve been solving problems designed for programming contests. I started this problem a few days ago, and cannot solve it for the life of me. I need some help with my algorithm and how to fix it. This is the problem: ACM Image Compression problem

MY CODE: I explain it underneath.

#include "Compress.h"
using namespace std;

Compress::Compress(){
    size = 0, threshold = 0, nRows=0, nCols=0;
    // Enter in a file name
    cout << "Welcome. Please type in the name of the file to read the numbers.\n";
    cin >> readFileName;

    inFile.open(readFileName.c_str());
    if(!inFile.is_open()) {
        cout << "Failed to open the file! Press Enter to exit..." << endl;
        exit(1);
    }

    //Finding the array size and threshold
    inFile >> size >> threshold;

    nRows = size;
    nCols = size;
    topright = size;
    bottomleft = size;

    //Let's make the array
    // creating the columns
    compressArray = new int* [nCols];

    // creating the rows
    for (int r = 0; r < nRows; r++){
        compressArray[r] = new int[nRows];
    }

    // FIll the array
    for (int i = 0; i < nRows; i++){
        for (int j = 0; j < nCols; j++){
            inFile >> compressArray[i][j];
        }
    }

    inFile.close();

    // Show before editing.
    print();
    work(0, nRows, 0, nCols);

}
Compress::~Compress(){
    for (int i = 0; i < nRows; i ++){
        delete compressArray[i];
    }
    delete [] compressArray;
}


void Compress::work(int start_x, int end_x, int start_y, int end_y){
    int nb_blacks = 0;
    int nb_whites = 0;
    int total_blocks = 0;
    int majority = 0;
    int percent = 0;

     cout << start_x << end_x << start_y << end_y << "\n------\n";

    for(int i = start_x; i < end_x; i++){
        for(int j = start_y; j < end_y; j++){
            if(compressArray[i][j] == 1){
                nb_blacks++;
            }
        }
    }

    total_blocks = ((end_x - start_x) * (end_y - start_y));
    nb_whites = total_blocks - nb_blacks;

    // give the max back
    majority = max(nb_blacks, nb_whites);
    // find the percent of the highest amount of colored blocks.
    percent = ((majority*100)/total_blocks);

    cout << "\n----\nPercent: " << percent << " Threshold: " << threshold  << endl;



    // majority/total_blocks is determining the percent of the greater
    // color in the box. We are comparing it to the threshold percent.
    if (percent >= threshold){
        for(int i = start_x; i < end_x; i++){
            for(int j = start_y; j < end_y; j++){
                if(nb_blacks > nb_whites) compressArray[i][j] = 1;
                else compressArray[i][j] = 0;
            }
        }
    }
    else {
            topright = topright/2;
            bottomleft = bottomleft/2;

            work(start_x, (end_x/2), (topright), end_y);
            work(start_x, (end_x/2), start_y, (end_y/2));
            work((bottomleft), end_x, start_y, (end_y/2));
            work((bottomleft), end_x, (topright), end_y);

    }

}

void Compress::print(){
    for (int r = 0; r < nRows; r++){
        for (int c = 0; c < nCols; c++){
            cout << compressArray[r][c];
        }
        cout << endl;
    }
}

So, what my program does is count the number of black squares in the image (1’s). It than compares it to the number of white squares (0’s). Whichever is larger, is turned into a percent based on how many squares are in the image. It compares it with the threshold. If the threshold is smaller than the percent… The whole image turned the majority color.

If the threshold is higher… It breaks into four recursive parts and zooms in. It will start with the top right, then top left, bottom left, and bottom right.

My program works with a 4 by 4 square, because it breaks into four parts correctly. However, with the 8 by 8 square… if it needs to be broken into smaller than four parts, everything messes up.

I know why it does this. My algorithm for zooming in the recursive function is wrong. If the square is an 8 by 8… the parameters would be something like

0, 8, 0, 8 = looking at the whole square

0, 4, 4, 8 = the top right

corner using a 4 by 4 0, 2, 6, 8 = looking at the smallest top right

square of a 2 by 2.

I just don’t know a mathematical function that would get what I need. I have no idea how to fix this for 8 by 8 squares. Is my code even possible to fix? Or do I need to figure out another way of going about it? If so, how?

Thank you

  • 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-14T03:22:08+00:00Added an answer on June 14, 2026 at 3:22 am

    Fixed it! The mathematical function was just being a pain.

    Header Function

    #include<cstdlib>
    #include<iostream>
    #include<string>
    #include<fstream>
    
    
    using namespace std;
    
    class Compress{
        public:
            Compress();
            ~Compress();
            void input();
            void work(int start_x, int end_x, int start_y, int end_y);
            void print();
    
        private:
            int size;
            int threshold;
            int** compressArray;
            int nRows, nCols;
    
            string readFileName;
            ifstream inFile;
    };
    

    CPP FILE

    #include "Compress.h"
    using namespace std;
    
    Compress::Compress(){
        size = 0, threshold = 0, nRows=0, nCols=0;
        // Enter in a file name
        cout << "Welcome. Please type in the name of the file to read the numbers.\n";
        cin >> readFileName;
    
        // Open the file.
        inFile.open(readFileName.c_str());
        if(!inFile.is_open()) {
            cout << "Failed to open the file! Press Enter to exit..." << endl;
            exit(1);
        }
    
        //Finding the array size and threshold
        inFile >> size;
    
        nRows = size;
        nCols = size;
    
        // Enter a threshold.
        cout << "Enter the desired threshold between 50-100: ";
        cin >> threshold;
    
        // Keep asking for the desired threshold until it is given.
        while (threshold < 50 || threshold > 100){
            cout << "\nIncorrect Threshold.\n";
            cout << "Enter the desired threshold between 50-100: ";
            cin >> threshold;
        }
    
    
        //Let's make the array
        // creating the columns
        compressArray = new int* [nCols];
    
        // creating the rows
        for (int r = 0; r < nRows; r++){
            compressArray[r] = new int[nCols];
        }
    
        // FIll the array
        for (int i = 0; i < nRows; i++){
            for (int j = 0; j < nCols; j++){
                inFile >> compressArray[i][j];
            }
        }
    
        inFile.close();
    
        // Show before editing.
        print();
        work(0, nRows, 0, nCols);
    
    }
    Compress::~Compress(){
        for (int i = 0; i < nRows; i ++){
            delete compressArray[i];
        }
        delete [] compressArray;
    }
    
    
    void Compress::work(int start_x, int end_x, int start_y, int end_y){
        int Size = end_y - start_y; // Finding the midpoints.
        int nb_blacks = 0;
        int nb_whites = 0;
        int total_blocks = 0;
        int majority = 0;
        int percent = 0;
    
    //    Testing everything.
    
    //    cout << "\nx1, y1: " << start_x << "," << start_y << " x2,y2: " << end_x << "," << end_y << endl;
    //    for (int r = start_x; r < end_x; r++){
    //        for (int c = start_y; c < end_y; c++){
    //            cout << compressArray[r][c];
    //        }
    //        cout << endl;
    //    }
    
        // Initial case. If 1, break and start returning results
        if (end_x <= start_x || end_y <= start_y){
            return;
        }
    
       //  Keep breaking it down until it reaches 1.
        else {
            // Count the Number of Black pieces
            for(int i = start_x; i < end_x; i++){
                for(int j = start_y; j < end_y; j++){
                    if(compressArray[i][j] == 1){
                        nb_blacks++;
                    }
                }
            }
    
            // Find the total and number of white pieces.
            total_blocks = ((end_x - start_x) * (end_y - start_y));
            nb_whites = total_blocks - nb_blacks;
    
            // give the max back
            majority = max(nb_blacks, nb_whites);
            // find the percent of the highest amount of colored blocks.
            percent = ((majority*100)/total_blocks);
    
    //        cout << "Percent: " << percent << " Threshold: " << threshold  << "\n-----\n";
    
    
            // majority/total_blocks is determining the percent of the greater
            // color in the box. We are comparing it to the threshold percent.
            if (percent >= threshold){
                for(int i = start_x; i < end_x; i++){
                    for(int j = start_y; j < end_y; j++){
                        if(nb_blacks > nb_whites) compressArray[i][j] = 1;
                        else compressArray[i][j] = 0;
                    }
                }
            }
    
            // Keep breaking down until we reach the initial case.
            else {
                work((end_x - (Size/2)), (end_x), (start_y), (start_y + (Size/2)));
                work(start_x, (start_x + (Size/2)), (start_y), (start_y + (Size/2)));
                work((start_x), (start_x + (Size/2)), (end_y - (Size/2)), end_y);
                work((end_x - (Size/2)), end_x, (end_y - (Size/2)), end_y);
    //
    //            work((start_x), (mid_x), (mid_y), end_y);
    //            work(start_x, (mid_x ), (start_y), (mid_y));
    //            work((mid_x), end_x, start_y, (mid_y));
    //            work((mid_x), end_x, (mid_y), end_y);
            }
        }
    }
    
    void Compress::print(){
    
        // Print the function
        cout << "\nImage: " << threshold << "%\n";
        for (int r = 0; r < nRows; r++){
            for (int c = 0; c < nCols; c++){
                cout << compressArray[r][c];
            }
            cout << endl;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
This could be a duplicate question, but I have no idea what search terms

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.