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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:55:45+00:00 2026-06-18T06:55:45+00:00

I was doing a coding challenge for a website, the premise was: In this

  • 0

I was doing a coding challenge for a website, the premise was:

In this challenge, write a program that takes in three arguments, a start temperature (in
Celsius), an end temperature (in Celsius) and a step size. Print out a table that goes from > the start temperature to the end temperature, in steps of the step size; you do not
actually need to print the final end temperature if the step size does not exactly match.
You should perform input validation: do not accept start temperatures less than a lower
limit (which your code should specify as a constant) or higher than an upper limit (which
your code should also specify). You should not allow a step size greater than the
difference in temperatures. (This exercise was based on a problem from C Programming
Language).

I got the same results as the solution did, but I’m curios as to why their solution is more efficient (I’d presume it is). Anyone able to explain it to me? Their solution is first followed by mine.

#include <stdio.h>

#define LOWER_LIMIT 0
#define HIGHER_LIMIT 50000

int main(void) {
    double fahr, cel;
    int limit_low = -1;
    int limit_high = -1;
    int step = -1;
    int max_step_size = 0;

    /* Read in lower, higher limit and step */
    while(limit_low < (int) LOWER_LIMIT) {
       printf("Please give in a lower limit, limit >= %d: ", (int) LOWER_LIMIT);
       scanf("%d", &limit_low);
    }
while((limit_high <= limit_low) || (limit_high > (int) HIGHER_LIMIT)) {
    printf("Please give in a higher limit, %d < limit <= %d: ", limit_low, (int) HIGHER_LIMIT);
    scanf("%d", &limit_high);
}
max_step_size = limit_high - limit_low;
while((step <= 0) || (step > max_step_size)) {
    printf("Please give in a step, 0 < step >= %d: ", max_step_size);
    scanf("%d", &step);
}

/* Initialise Celsius-Variable */
cel = limit_low;

/* Print the Table */
printf("\nCelsius\t\tFahrenheit");
printf("\n-------\t\t----------\n");
while(cel <= limit_high) {
    fahr = (9.0 * cel) / 5.0 + 32.0;
    printf("%f\t%f\n", cel, fahr);
        cel += step;
    }
    printf("\n");

    return 0;
}

My solution:

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

#define LOW 0
#define HIGH 50000


int main(void)
{
int lower, higher, step, max_step;
float cel, fahren;

printf("\nPlease enter a lower limit, limit >= 0: ");
scanf("%d", &lower);

if (lower < LOW)
{
    printf("\nERROR: Lower limit must be >= 0.");
    exit(1);
}


printf("\nPlease enter a upper limit, limit <= 50000: ");
scanf("%d", &higher);

if (higher > HIGH)
{
    printf("\nERROR: Upper limit must be <= 50,000.");
    exit(1);
}

printf("\nPlease enter an increment amount, 0 < step <= 10: ");
scanf("%d", &step);

max_step = higher - lower;

if (step > max_step)
{
    printf("\nERROR: Step size cannot exceed difference between higher and lower limit.");
    exit(1);
}

printf("Celsuis \tFahrenheit\n");
printf("------- \t-----------\n\n");

cel = (float)lower;

while (cel < higher)
{
    fahren = cel * 9/5 + 32;
    printf("%f \t%f\n", cel, fahren);
    cel = cel + step;
}

return 0;

}

  • 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-18T06:55:46+00:00Added an answer on June 18, 2026 at 6:55 am

    Hmm, which is more efficient… before making that claim we need some metrics, what are we talking about here? Run time? Binary size?
    Just a quick example… we can compile both solutions and run them with the “time” command and a worst case (0-50000 with a step of 1) to see what type of times we’re using:


    “Their” solution size:

    text       data     bss     dec     hex filename
    1937        276       8    2221     8ad a.out
    

    “Their” solution running time:

    user  0m 0.024s
    sys   0m 0.601s
    

    Your solution size:

    text       data     bss     dec     hex filename
    2054        276       8    2338     922 a.out
    

    Your solution running time:

    user   0m 0.025s
    sys    0m 1.047s
    

    So your solution takes longer and has a larger image size. Can we say now that “they” have a more efficient program? Not really, the time isn’t totally accurate on this scale (and with other things happening in the system) so we’d need a number of runs. Averaged over four* runs:

    // you
    user    0.0178
    system  0.9015
    // "them"
    user    0.016
    system  0.914
    

    So no, they are not really more “efficient” then you are.

    There are some trivial things we can do to increase the “efficiency” here, but because the solutions are so similar, and the code is so trivial (a single stepping traversal) I’m not really sure if it matters all that much.

    As far as “efficient” code size, you’ll note that your .text size is larger than the other solution. Your messages are more verbose and readable, so you take a hit on size. Is that more efficient? Perhaps if size is important, but personally I think readability is more important unless we’re talking an embedded solution.

    * – you need a lot more runs and a more sensitive timing mechanism, but this is just a quick example

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

Sidebar

Related Questions

I'm doing a bit of coding, where I have to write this sort of
I have decided to start doing small coding projects on my own that focus
This is what i am doing in coding i want to create controls on
I am doing a registration page coding. For that i have so many fields
I am coding a E-commerce website/admin interface for a client. They are doing some
today i was doing some java script coding, in that i need some images
I am doing coding to solve a problem like this. There are about 50
I'm looking at doing embedded coding for a device that's approximately 20MHz, has 6mb
I am doing some weekend coding exercises. I have a table that contains some
I am doing Arithmetic Coding now, and I have got the final start position

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.