suppose the input is 1,2,3,5-14,16-20,-25,31-
:the program must print
output: 1
        2
        3
        5 6 7 8 9 10 11 12 13 14
        16 17 18 19 20
        0 1 2 3 ...........25
        31 32 33..... til end of file
        I WAS REJECTED FROM MY LAST ROUND OF INTERVIEW BECAUSE I MADE THE CODE BIG.. PLZ CAN ANY1 HELP ME WIT OPTIMISED CODE. :) 

You have a stop variable. Every number that is single, is given a "stop" equal to, the first number read, meaning no other numbers will be printed beyond the first one. Otherwise, stop is given the value of the number after the hyphen (the second number).

So I picture this as a read, followed by a for or while loop: e.g.

int number, stop;
read the number, assign value to stop.
for(i=number;i<=stop;i++)
      print i

How verbose was your code? (How many lines?) I don't believe this is highly optimized, but it seems straight forward, clear and acceptably concise.

As Adak suggested, single digits followed by a comma (',') must be printed on their own line, pair of digits, separated by a dash ('-') represents a sequence, where the 1st number is the lower bound, and the 2nd number is the upper bound, and you should print the whole sequence, and finally, digits followed by a dash represents a sequence, starting from that number, with no upper bound (perhaps some will be impose), or with a dash in from of them, havin as their lower bound '0', and as upper bound the given number.

I would ask a few questions:

  1. "31 32 33..... til end of file". What does end of file mean here? Are you reading input from a file? Are you just printing numbers until the user tells you to stop? The behavior isn't obvious here, so writing code for it is impossible.

  2. How are negative values handled? Is it just assumed that all numbers are positive?

  3. What about things like ",," or ",-,"? Are they errors or need to be included in the logic?

Excluding the part that's completely ambiguous, and assuming only positive numbers, the rest of the logic is straightforward. However, dong it right doesn't result in teeny tiny code unless you're trying to obfuscate:

#include <stdio.h>
#include <stdlib.h>

void print_range(int start, int stop)
{
    if (start > stop) {
        /* Print a decreasing range */
        while (start >= stop) {
            printf("%d ", start--);
        }
    }
    else {
        /* Print an increasing range */
        while (start <= stop) {
            printf("%d ", start++);
        }
    }
}

int main(void)
{
    int next; /* Represents the next character from the stream */

    do {
        char buf[BUFSIZ] = {0};

        /* Look for non-negative numbers */
        int rc = scanf("%[0123456789]", buf);
        int start = atoi(buf);

        next = getchar();

        if (next == '-') {
            int stop;

            if (scanf("%d", &stop) != 1) {
                /* Placeholder for ranges with an implicit end. Just error for now */
                fprintf(stderr, "Invalid  format: Missing range end\n");
                break;
            }

            print_range(start, stop);
            putchar('\n');

            next = getchar();
        }
        else if (next == ',') {
            if (rc != 1) {
                fprintf(stderr, "Invalid format: Empty field\n");
                break;
            }

            printf("%d\n", start);
        }
    } while (next != EOF && next != '\n');

    return 0;
}

If the interviewer calls something like that "too big", I'd probably thank them for their time and end the interview right there because the company's idea of code quality doesn't mesh well with mine. Could the code be shortened and improved with a little more time? Absolutely, but I'd argue that it couldn't be shortened so much that the current code is "too big".

thank you Adak and Lucaci Andrew but the input must also accept "," (its like exact way how we use our normal printer to print required sheets by giving range). so for that i dont think using interger array to accept the input can workout. so when i use charater array all the problems start, how can a character array read 2 digits?? so i wrote a code to handle single digit,to handle ",",to handle"-",to handle double digit, but i m looking for an optimised solution.

Hi  

deceptikon,

 1.end of file means :The interviewer actually want a code to print the pages of a    document,so he means till the end of document.
 2."handling negitaive numbers": I dont think we can come across such situation because to use "," and "-" in the logic input we have to use character array.
 3."," and "-" have to be part of the input.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.