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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:42:36+00:00 2026-05-25T21:42:36+00:00

Every time this code runs in UNIX, it hangs and displays a new line

  • 0

Every time this code runs in UNIX, it hangs and displays a new line without a prompt. It compiles without error. Anyone know what’s up here?

/*
This program uses a Monte Carlo simulation to maximize widget profits.
 */

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

// constant values used in the simulation
#define MIN_WIDGETS 1000 // minimum widgets per month
#define MAX_WIDGETS 1500 // maximum widgets per month

#define MIN_COST 0.25 // minimum cost per widget
#define MAX_COST 0.82 // maximum cost per widget

#define MIN_CONVERSION 0.01 // minimum converstion rate
#define MAX_CONVERSION 0.05 // maximum converstion rate

#define MIN_PROFIT 38 // minimum profit per sale
#define MAX_PROFIT 43 // maximum profit per sale

#define FIXED_OVERHEAD 400 // fixed overhead cost

#define NUM_SIMULATIONS 100 // number of simulations performed

void main()
{

  // to keep track of inputs in highest-profit simulation
  int bestW = 0; // widgets per month
  float bestC = 0; // cost per widget
  float bestR = 0; // rate of converstion
  float bestP = 0; // profit per sale
  float bestProfit = 0; // highest profit

  srand(time(0)); // initialize the random number generator
  float h = FIXED_OVERHEAD;

  int i; // loop index 
  for(i = 0; i < NUM_SIMULATIONS; i++)
    {
        // initialize inputs for this individual simulation
        int w = 0; // quantity of widgets bought
        float c = 0; // cost of a widget
        float r = 0; // conversion rate
        float p = 0; // profit per sale
        float profit = 0; // profit

        // simulate quantity of widgets bought per month, between MIN_WIDGETS and MAX_WIDGETS
        w = random() % (MAX_WIDGETS + 1); // to set the maximum value of w at MAX_WIDGETS
        while (w < MIN_WIDGETS) {
            w = random() % (MAX_WIDGETS + 1); // fetch new random number that may fit parameters
        }

        // simulate cost per widget, between MIN_COST and MAX_COST
        c = random() % 100; // to convert random number into an integer between 0 and 99
        while (c < (MIN_COST*100) || c > (MAX_COST*100)) {
            c = random() % 100; // fetch new random number that may fit parameters
        }
        c = c / 100.0; // convert cost back from cents into dollars

        // simulate conversion rate, between MIN_CONVERSION and MAX_CONVERSION
        r = random() % 100; // to convert random number into an integer between 0 and 99
        while (r < (MIN_CONVERSION*100) || r > (MAX_CONVERSION*100)) {
            r = random() % 100; // fetch new random number that may fit parameters
        }
        r = r / 10.0; // convert back into fraction

        // simulate profit per sale, between MIN_PROFIT and MAX_PROFIT
        p = random() % ((MAX_PROFIT + 1)*100); // to convert random number into an integer between 0 and 4300
        while (p < MIN_PROFIT*100) {
            p = random() % (MAX_PROFIT + 1); // fetch new random number that may fit parameters
        }
        p = p / 100.0; // convert back into floating point with two decimal places after period

        profit = (w * r * p) - (h + c * w);

        printf("Current profit is $%.2f, with %d widgets at a %.2f cost per widget with a %.1f conversion rate and %.2f profit/sale.\n",
            profit, w, c, r, p);

        if (profit > bestProfit) {
            bestW = w;
            bestC = c;
            bestR = r;
            bestP = p;
            bestProfit = profit;
        }

    }

  printf("Maximum profit is $%.2f, with %d widgets at a %.2f cost per widget with a %.1f conversion rate and %.2f profit/sale.\n",
    bestProfit, bestW, bestC, bestR, bestP);

}
  • 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-25T21:42:37+00:00Added an answer on May 25, 2026 at 9:42 pm

    I think the error is here:

    while (p < MIN_PROFIT*100) {
        p = random() % (MAX_PROFIT + 1); // fetch new random number that may fit parameters
    }
    

    If the loop body is entered p will be set to a number between 0 and MAX_PROFIT (= 43). It will never be greater than or equal to MIN_PROFIT*100 (= 3800), so it will go into an infinite loop.

    As a side-note, you might want to consider using do { } while loops here instead of using a while loop and writing the loop body twice. Writing code twice is an excellent way to make errors, because when you change one of the implementations you always have to remember to change the other. If you forget to update both you will introduce a bug. And that seems to be what happened here.

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

Sidebar

Related Questions

I seem to make this mistake every time I set up a new development
I'm sure this is a newbie question, but every time I've compiled/dl'ed a new
The code below handles a stack of CALayers . Every time a new layer
This code is C/C++ and runs without warnings or debug messages. I'm using Code::blocks
If there was ever a time to hate IE, this is it. This code
Every time I call this method my NSMutableData is leaking and I cannot figure
Every time in PHP when I make a variable such as this one: $date
I have this warning every time I run my CGI-script (output is rendered by
I tried decoding this base64 string so many times but every time i get
I find this syntax astoundingly annoying. Every time I rename my class, I have

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.