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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:53:19+00:00 2026-06-03T03:53:19+00:00

Using this program, I’m trying to set a 6×15 array to 0, then place

  • 0

Using this program, I’m trying to set a 6×15 array to 0, then place a random number x amount of times in a random slot of the array. However, it doesn’t go as planned…

Note that MAX_ROWS is 6, MAX_COLS is 15, and OCEAN is 0

#include <stdio.h>
#include <time.h>
#include "util.h"

int rand_number(int param);

main()
{
int map[MAX_ROWS][MAX_COLS]; //initializes an array, map, with the dimensions 6 and 15.

//sets all values in the array to 0
int a,b;

for (a = 0; a < MAX_ROWS; a++)
{
for (b = 0; b < MAX_COLS; b++)
{map[a][b]=OCEAN;}
}

int shipnum = 6;

This SHOULD place the random numbers. (shipnum is just a value I use to limit the number of ships I place):

while(shipnum > 0)
{
map[rand_number(MAX_ROWS)][rand_number(MAX_COLS)] = 3;
shipnum -= 1;
map[rand_number(MAX_ROWS)][rand_number(MAX_COLS)] = 2;
shipnum -= 2;
map[rand_number(MAX_ROWS)][rand_number(MAX_COLS)] = 1;
shipnum -= 3;
}

However, when I run

/*This will print the array*/
for (a = 0; a < MAX_ROWS; a++)
{
for (b = 0; b < MAX_COLS; b++)
{printf("%d ", map[a][b]);}
printf("\n");
}
}

I am given

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

When I actually want to get something like

0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 2 0 0 0 0 0 3 0 0 
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 

This is the function I use to generate a random number.

/*This will generate a random number*/
int rand_number(int param)
{
srand((unsigned int)time(NULL));
int x = param;
int rn = rand() % x;
return rn;
}
  • 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-03T03:53:20+00:00Added an answer on June 3, 2026 at 3:53 am

    You are reseeding your random generator on every call which will make it not random. Remove this line:

    srand((unsigned int)time(NULL));
    

    Your code also allows writing a ship where there already was a ship. If you don’t want to allow this, you should check that the cell is empty before placing a ship in that cell.

    int x = rand_number(MAX_ROWS);
    int y = rand_number(MAX_COLS);
    if (map[x][y] == 0) {    /* Add this check! */
        map[x][y] = 1;
        shipnum -= 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get console output from a program using this library uZpRunConsoleApp .
I'm trying to compile this program using MinGW on Windows 7. On my first
I need to write this program using a loop https://i.stack.imgur.com/gko6T.jpg this is what i
For this program #include <iostream> using std::cout; struct C { C() { cout <<
I have this program in c++: #include <iostream> using namespace std; int main() {
I created this program: #include <iostream> #include <fstream> using namespace std; int main ()
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I'm working on a picture select/crop program and using this code below: http://www.androidworks.com/crop_large_photos_with_android/comment-page-1#comment-969 As
I'm using a SQL Server database in my program with Linq-to-SQL I'm using this
Anyone know how to do this without using a third party program? If there

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.