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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:28:52+00:00 2026-06-05T11:28:52+00:00

After searching through the web I didn’t found an answer to help me. My

  • 0

After searching through the web I didn’t found an answer to help me.
My program is in C.

I have a list of items (for example : 37 a, 22 b, 29 c, 13 d, 19 e, 2 f and 0 g) and I have to randomly place all those items on a map (in my case, an int[height][lenght][7]).
I thought to use a rand to place items and to loop until the whole items are placed but it uses way too much time and ressources.

Is there a way to place them easily and properly ?

Here is my code:

/* allocates the tab in order to place the ressources/items */
void            create_map(t_world *world)   
{
  unsigned int  x;
  unsigned int  y;

  x = 0;
  world->map = xmalloc(world->height * sizeof(int**));
  while (x < world->height)
    {
      y = 0;
      world->map[x] = xmalloc(world->lenght * sizeof(int*));
      while (y < world->lenght)
        {
          world->map[x][y] = xmalloc(7 * sizeof(int));
          bzero(world->map[x][y], 7);
          ++y;
        }
  ++x;
    }
}

/* base defining the number of required item */
t_elevation elevation_tab[] =
{
  {1, {0, 1, 0, 0, 0, 0, 0} },
  {2, {0, 1, 1, 1, 0, 0, 0} },
  {2, {0, 2, 0, 1, 0, 2, 0} },
  {4, {0, 1, 1, 2, 0, 1, 0} },
  {4, {0, 1, 2, 1, 3, 0, 0} },
  {6, {0, 1, 2, 3, 0, 1, 0} },
  {6, {0, 2, 2, 2, 2, 2, 1} }
};

/* calculates the number of item required */
unsigned int    *calc_elevation(t_world *world)
{
  unsigned int  i;
  unsigned int  pos;
  unsigned int  *tab;

  i = 0;
  tab = xmalloc(7 * sizeof(int));
  bzero(tab, 7);
  while (i < 7)
  {
    pos = 0;
    while (pos < 7)
    {
      tab[pos] += (world->population /
               elevation_tab[i].required_players +
               world->population %
               elevation_tab[i].required_players) *
        (elevation_tab[i].required_ressources[pos]);
      ++pos;
    }
  ++i;
  }
  return (tab);
}
void            place_ressources(t_world *world, unsigned int *ressources)
{
//here is my missing code
}

/*First called function*/
void            create_world(t_param *params, t_world *world)
{
  unsigned int  *ressources_needed;

  world->lenght = params->lenght;
  world->height = params->height;
  world->population = params->team_size * 2;
  create_map(world);
  ressources_needed = calc_elevation(world);
  place_ressources(world, ressources_needed);
  show_map(world);
  world->players = NULL;
  free(ressources_needed);
}

The world is a grid of a certain size decided by the user with heigth and lenght. Each case of the grid has a int[7] delivering the amount of each item. So I can place multiples item on the same case.

  • 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-05T11:28:53+00:00Added an answer on June 5, 2026 at 11:28 am

    I can think of 2 possible ways to solve this problem, but its going to depend on how many items need to be processed.

    The first thing i thought of was a solution resembling how to shuffle a deck of cards in a computer program. Take the first element, and randomly generate x, y coords. Take the next element and generate x, y coordinates. If there is no item there, place the item in that position, otherwise generate new x, y coords. Continue this for all elements to be placed.

    The other thing i can think of is if you can somehow keep track of which world tiles have been used so randomly generated coordinates are not duplicated.

    The fact that your world object is a 3d array is what is exponentially increasing computations. Is it absolutely necessary? Granted i dont really know exactly what context your program is being designed for

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

Sidebar

Related Questions

I have got a big problem. After searching through the internet, I didn't find
After searching through some existing libraries for JSON, I have finally ended up with
I'm sure this question HAS to have been asked, but after searching through the
I have been searching the web for an answer now for quite a while,
After searching online and going through past stackoverflow posts for a suitable implementation for
After searching the other questions none have answered the specific issue I have. We
I'm going to edit an ImageField using jquery ajax,after searching I found out I
I have been searching through here and google for a few days now, trying
I am using Apache Common Logging library in my standalone application. After searching through
In my web project I have to serve ads through our openx server but

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.