dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: compression algorithm outputting wrong data

  1. #1
    Join Date
    Aug 2018
    Posts
    5

    compression algorithm outputting wrong data

    Hello coders,
    The code is supposed to go through the string and every time there are similar characters it's supposed to increment the counter. An input of "303030404040" is supposed to print out "303403" but its printing bogus data!! please help

    Code:
    #include <iostream>   // std::cout
    #include <iomanip>
    #include <string>     // std::string, std::to_string
    #include <stdlib.h>     /* atof */
    #include <math.h>
    #include<sstream>
    #include <cstring>
    #include <vector>
    #include <array>
    #include <string>
    #include <algorithm>
    string integerrunlengthencoding(string hextext) {
         
        int counter=1;
        int length = hextext.size(); 
        string output = "";
         
        for(int i=0;i<length -1;i=i+2){
            for(int k=1;k<length;k=k+2){
                if(hextexts[i] == hextexts[i+2]){
                    if(hextexts[k] == hextexts[k+2] ){
                        counter++;
                    } else{
                        output = output + hextexts[i] + hextexts[k] + std::to_string(counter);
                         
                        counter =1;                 
                        }           
                }
                 
            }   
        } 
        output = output + hextexts[length -1] + hextexts[length] + 
             std::to_string(counter);
        return output.size() < hextext.size() ? output : hextext; 
    }
     
     
    int main(){
    string t = "303030404040";
    cout << integerrunlengthencoding(t);
    }
    Last edited by ikhram; August 11th, 2018 at 04:09 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,482

    Re: compression algorithm outputting wrong data

    As I mentioned in reply to a previous post, when code doesn't perform as expected the first thing is to use the debugger.

    What debugging of the code has been done? Has the debugger been used to trace through the code line by line and the contents of the variables watched? If not, now's the time to learn how to use the debugger. if yes, where did the code deviate from that expected from the program design?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.9)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,482

    Re: compression algorithm outputting wrong data

    Is this homework for a course?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.9)

  4. #4
    Join Date
    Feb 2017
    Posts
    360

    Re: compression algorithm outputting wrong data

    Quote Originally Posted by ikhram View Post
    An input of "303030404040" is supposed to print out "303403" but its printing bogus data!!
    Your code wouldn't compile at all because you're using hextext and hextexts for the same variable.

    When I changed that I got an out-of-bounds runtime error.

    The "bogus data" probably comes from this line,

    Code:
    output = output + hextexts[i] + hextexts[k] + std::to_string(counter);
    Here "hextexts[i] + hextexts[k]" will evaluate to the integer sum of the ASCII representations of the two chars which will look strange in the output string.

    This is my take on it (not much tested),

    Code:
    std::string integerrunlengthencoding(const std::string& in) {
    	const int N = int(in.size());
    	assert((N&1)==0); // length must be even
    	std::string out = "";
    	for (int i=0,k=2; i<N; i=k,k+=2) { // loop thru hex numbers of the input
    		while (k<N && in[i]==in[k] && in[i+1]==in[k+1]) k+=2; // skip a sequence of equal hex numbers
    		out += std::string(&in[i],2) + std::to_string((k-i)/2); // output hex number and sequence length
    	}
    	return (int(out.size()) < N) ? out : in; // return shortest string
    }
    Last edited by wolle; August 11th, 2018 at 05:30 PM.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,482

    Re: compression algorithm outputting wrong data

    This is my version. It seems to be working but it's not much tested.
    If it's a homework exercise, we're not supposed to provide the solution - just hints! That's why I asked in post #3 before I posted any code.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.9)

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,482

    Re: compression algorithm outputting wrong data

    The "bogus data" probably comes from this line,

    Code:
    output = output + hextexts[i] + hextexts[k] + std::to_string(counter);
    Here "hextexts[i] + hextexts[k]" will evaluate to the sum of the ASCII representations of the two chars which will look strange in the output string.
    You need to bracket this as
    Code:
    output = (output + hextexts[i]) + (hextexts[k] + std::to_string(counter));
    now the +'s are taken as string concatenation.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.9)

  7. #7
    Join Date
    Aug 2018
    Posts
    5

    Re: compression algorithm outputting wrong data

    to answer 2kaud's inquiry the answer is no. I am doing this for myself

  8. #8
    Join Date
    Feb 2017
    Posts
    360

    Re: compression algorithm outputting wrong data

    Quote Originally Posted by 2kaud View Post
    If it's a homework exercise, we're not supposed to provide the solution - just hints!
    It didn't feel like homework from the OP's asking behavior.

    Also the coding scheme contains a flaw which is unusual for homework. If the number of repeats can exceed 9 then it's not possible to determine whether two chars is a hex number or a number of repeats. For example 0210 can be the two hex numbers 02 and 10 or it can be 02 repeated 10 times.

    Luckily the OP has confirmed it isn't homework but I'll be more careful in the future. It's just that an opportunity to write C-style C++ code is almost irresistible to me.
    Last edited by wolle; August 11th, 2018 at 02:56 PM.

  9. #9
    Join Date
    Aug 2018
    Location
    Burbank, California
    Posts
    5

    Re: compression algorithm outputting wrong data

    First of all, it will print of "303" if it was correct to start, index i is 0 to start, therefore '3', k is 1 therefore 0, and then you append the length of the counter at that point which will be 3, as you match 30 3 times...

    Then length - 1 index would be the last character of 0, so that is 3030 right there and the index of length in a string variable would be out of bounds as he is saying, because in a null terminated string that would be the null character and therefore out of bounds beyond the normal text in a string object.

  10. #10
    Join Date
    Aug 2018
    Location
    Burbank, California
    Posts
    5

    Re: compression algorithm outputting wrong data

    Quote Originally Posted by JamesBernardSchumach View Post
    First of all, it will print of "303" if it was correct to start, index i is 0 to start, therefore '3', k is 1 therefore 0, and then you append the length of the counter at that point which will be 3, as you match 30 3 times...

    Then length - 1 index would be the last character of 0, so that is 3030 right there and the index of length in a string variable would be out of bounds as he is saying, because in a null terminated string that would be the null character and therefore out of bounds beyond the normal text in a string object.
    In other words I am giving you a hint to working out your compression code without giving you an exact answer.

  11. #11
    Join Date
    Aug 2018
    Location
    Burbank, California
    Posts
    5

    Re: compression algorithm outputting wrong data

    Quote Originally Posted by JamesBernardSchumach View Post
    In other words I am giving you a hint to working out your compression code without giving you an exact answer.
    Hint, realize if you are reading about range code and mapping values to integers as some certain versions will talk of, understand that mapping straight character mappings to range code will itself only gain if there is entropy if you are to support the entire range of characters, and if you cannot map that integer to the characters that it represents, that is restore then you cannot decompress it.

    Words of advice if you are working on this type of idea.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)