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

The Archive Base Latest Questions

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

Relatively new to C++ and this has been bugging me for a while. I’m

  • 0

Relatively new to C++ and this has been bugging me for a while. I’m trying to write a program that will do different things depending on what random number is generated.

To explain what I’m trying to do simply, lets pretend I’m creating a list of athletes and start by randomly generating their heights within a certain range. Easy to do no problem. Say then I want to generate their weight, based on their height. This is where things get messy. For some reason I can’t figure out, the program is randomly generating the weight based on a different height than the one it returns in the first place. I just don’t get it.

Anyway, here is a piece of (very simplified) sample code that hopefully shows what I’m trying to do. I’m sure I’m missing something obvious but I just can’t seem to figure it out.

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;

int random(int min, int max, int base)
{
    int random = (rand() % (max - min) + base);

    return random;
}

int height()
{
    int height = random(1, 24, 60);

    return height;
}

int weight()
{
    int weight = height() * 2.77;

    return weight;
}

void main()
{
    srand ((unsigned int)time(0));

    int n = 1;

    while (n <= 10)
        { 
        cout << height() << " and " << weight() << endl;
        ++n;
        }

    return;
}
  • 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-27T16:33:57+00:00Added an answer on May 27, 2026 at 4:33 pm

    It’s quite easy. You call height() in your weight() function. That means you’re getting a new random value for the height. What you have to do is to modify your weight() so that it can pass through a height parameter and calculate the weight based on it (and not on a new random value).

    Your new height() function would look as follow:

    int weight(int height)
    {
        int weight = height * 2.77;
    
        return weight;
    }
    

    In your main():

    while (n <= 10)
    {
        int h = height();
        int w = weight(h);
    
        cout << h << " and " << w << endl;
        ++n;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am relatively new to MYSQL and have had an issue that has been
I am relatively new to XSL and I think this is a basic question.
I'm relatively new to web application programming so I hope this question isn't too
Being relatively new to functional programming, I expend lots of energy wondering is this
I'm relatively new to the Python world, but this seems very straight forward. Google
This may be a silly question, but as someone relatively new to PHP, I'm
I just cannot figure this out, it looks really simple but I'm relatively new
I really need your help for this. I am relatively new to programming and
Relatively new to rails and trying to model a very simple family tree with
Being relatively new to the .net game, I was wondering, has anyone had any

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.