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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:25:33+00:00 2026-05-27T22:25:33+00:00

I have written a small piece of code that would perform Run length encoding

  • 0

I have written a small piece of code that would perform Run length encoding kind of stuff on 1-D array but still far from desired result.

main()
{
    int a[8]={2,0,0,0,3,0,0,9};
    int i,temp,ct=0,flag,m;
    int found[90]={0};
    for(i=0;i<=7;i++)
    {
        if(!a[i])
        {
            ct++;
            if(!found[a[i]])
            {
                flag=i;
                found[a[i]]=1;
            } 
        } 
    }

    a[flag]=ct;
    m=ct;    
    for(i=0;i<m;i++)
    {
        printf("%d",a[i]);
    }       
}/* end of main*/

Now for above array i would like to have output something below

 2 5 0 3 9

But with my piece of code am getting

 2 5 0 0 3

Can I have any suggestion on that?

  • 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-27T22:25:34+00:00Added an answer on May 27, 2026 at 10:25 pm

    Shouldn’t run length encoding turn 2,0,0,0,3,0,0,9 into 2 1 0 3 3 1 2 0 9 1?

    1) The first thing I see is wrong is that you aren’t looking at the entire array. You’re using < to stop before 8, but also stopping at 7, so you only evaluate array items 0 – 6.

    2) If ct stands for count it’s never reset (ct=0 only on declaration). Also it’s assignment is this: a[flag]= ct; which overwrites your original data. It basically tracks the value of i.

    This is my version I’ve just put together:

    #define SZ 8
    
    main()
    {
        int a[SZ]={2,0,0,0,3,0,0,9};
        int i; //absolute position
    
        int runningCount = 1; //because we start at array index 1 and not zero
    
        for (i = 1; i <= SZ; i++) {
            if (a[i - 1] == a[i]) //value same as one before it...
               runningCount++;
            else { // new value found. print last one, and the count of the last one.
                printf("%d %d ", a[i - 1], runningCount);
                runningCount = 1; //reset for next loop
            }
        }
    
        return 0;
    }
    

    The output is 2 1 0 3 3 1 0 2 9 1

    Ok based on the comment left below, your algorithm would actually look like this:

    #define SZ 8
    
    main()
    {
        int a[SZ]={2,0,0,0,3,0,0,9};
        int i; //absolute position
    
        int zero_count = 0; //target zeros specifically...
    
        for (i = 0; i < SZ; i++) {
            if (a[i] == 0)
               zero_count++;
        }
    
        //now write it out in a bizarre, unparsable format again...
    
        for (i = 0; i < SZ; i++) {
    
            if (a[i] != 0)           //write out all non zero values
                printf("%d ", a[i]);
    
            if (i == 0) { //this says put the zero count after the first number was printed
               printf("%d 0 ", zero_count); //inserting it into a strange place in the array
            }
    
        }
    
        return 0;
    }
    

    which outputs: 2 5 0 3 9

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

Sidebar

Related Questions

I have written a small piece of code to calculate quadratic equations, but if
i have written a small piece of code. This code first blocks the {SIGSEGV},
I have written a small piece of code regarding the dynamic loading of assemblies
I have written a small piece of code which handles the input of a
I have written a small Perl script and now I would like to create
I have written a small chat app that uses mysql + php to facilitate
I have written a small app that puts my bluetooth in discoverable mode for
I have written a small XML validator, that takes in an XML file and
I have written some code in which a small part of the code takes
I have written a small util in an app that syncs the time from

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.