Two Dimensional Array With Random Numbers Doesn't Change

two dimensional array with random numbers doesn't change

you have to seed your engine. try

generator.seed(n);

http://www.cplusplus.com/reference/random/default_random_engine/

define 2D array dimensions by random numbers in C

As of C99, you can define an array's size at runtime using variable length arrays:

int x = some_value();
int y = some_other_value();

int arr[x][y];

Since their size isn't determined until runtime, there are some restrictions on their use - they can't have static storage duration (i.e., they can't declared at file scope or with the static keyword), and you can't use an initializer in the declaration. You also want to be careful if x or y are large.

If you don't have a C99 or later compiler available, then this won't work. You'll have to use dynamic memory allocation instead, and you'll have to allocate it as a "jagged" array:

int x = some_value();
int y = some_other_value();

int **arr = malloc( sizeof *arr * x );
if ( arr )
{
for ( size_t i = 0; i < x; i++ )
{
arr[i] = malloc( sizeof *arr[i] * y );
}
}

When you're done, you'd deallocate as

for (size_t i = 0; i < x; i++ )
free( arr[i] );
free( arr );

This will work like any other 2D array when it comes to accessing elements:

arr[i][j] = some_value();

but be aware that the rows will not be contiguous - there will be gaps from the end of one row to the beginning of the next row. If that matters, then the last option is to declare a 1D array and manually compute the index:

int *arr = malloc( sizeof *arr * x * y );
...
arr[i * x + j] some_value();

When you're done, you'd free it as

free( arr );

how to assign random number with range to 2D array dimensions

The problem in your code is that you are trying to initialize a matrix of double with an int

Types must be equals!

Here are your code fixed.

import java.lang.Math;

public class Homework2 {
public static void main(String[] args){

int d1 = (int) (Math.random()*(10-3+1)+3);
int d2 = (int) (Math.random()*(10-3+1)+3);

double doubMatrix1[][] = new double[d1][d2];
double doubMatrix2[][];
double doubMatrix3[][];


}
}

hope this help

Filling a 2D Array in C with random numbers from 0 - 99, then sorting in descending order

The function fillMatrix retrieves two important values from the user, rows and columns, which are needed throughout the program, but these values are lost when the function exits. You then proceed to use fixed values N1 and M1

You should change the function to void fillMatrix(int A[][M1], int *set_rows, int *set_columns) and pass rows and columns by reference so that their values are saved.

if(rows < 0 && rows >= 100)
{
printf("Invalid Input.\n");
printf("Enter a number of rows: ");
scanf("%i", &rows);
}

The valid range for rows should be from 1 to M1. You should also be prepared in case the user enters the wrong value a second time.

There is no standard method to sort the values in a matrix. Do you want to sort each row? You can use qsort to sort a 1-D array.

#define TOTAL_ROWS 100
#define TOTAL_COLUMNS 100
void fillMatrix(int A[][TOTAL_COLUMNS], int *set_rows, int *set_columns)
{
int rows = 0, columns = 0;

//ASK FOR ROWS
while(1)
{
printf("Enter a number of rows: ");
scanf("%i", &rows);
if(rows > 0 && rows < TOTAL_ROWS)
break;
printf("Invalid Input.\n");
}

//ASK FOR COLUMNS
while(1)
{
printf("Enter a number of columns: ");
scanf("%i", &columns);
if(columns > 0 && columns < TOTAL_COLUMNS)
break;
printf("Invalid Input.\n");
}

for(int i = 0; i < rows; i++)
for(int j = 0; j < columns; j++)
A[i][j] = rand() % 100;

*set_rows = rows;
*set_columns = columns;
}

int compare(const void *a, const void *b)
{
return (*(int*)a - *(int*)b);
}

void sortMatrix(int A[][TOTAL_COLUMNS], int rowsize, int colsize)
{
for(int r = 0; r < rowsize; r++)
qsort(A[r], colsize, sizeof(int), compare);
}

void displayArray(int A[][TOTAL_COLUMNS], int rows, int columns)
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
printf("%3i ", A[i][j]);
printf("\n");
}
}

int main(void)
{
srand((unsigned int)time(NULL));
int array[TOTAL_ROWS][TOTAL_COLUMNS] = { 0 };
int rows, columns;
fillMatrix(array, &rows, &columns);
sortMatrix(array, rows, columns);
displayArray(array, rows, columns);
return 0;
}

If you are to use your own sort, or bubble sort specifically, write a simple bubble sort function for a 1-D array, then sort the matrix 1 row at a time:

void bubble_sort(int *arr, int count)
{
for(int i = 0; i < count - 1; i++)
{
for(int j = 0; j < count - i - 1; j++)
{
if(arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

void sortMatrix(int A[][TOTAL_COLUMNS], int rowsize, int colsize)
{
for(int r = 0; r < rowsize; r++)
bubble_sort(A[r], colsize, sizeof(int), compare);
}

Lastly, if rows and columns are not known ahead of time, declaring int array[100][100] is not best way. In gcc compiler you can use variable array. In Visual Studio you have to use malloc in order to dynamically allocate the array based on what the user chooses for rows and columns.

Putting a number at random spots in 2D array

Having nested loop running 8 times each will iterate it 64 times. You don't need nested loops to do that. One of the easy ways will be using a while-loop and distribute the 8 random spots till all 8 spots are taken:

int occupiedSpots = 0;
Random random = new Random();

while(occupiedSpots < 8){
int x = random.nextInt(array.length);
int y = random.nextInt(array[0].length);
if(battleship[x][y] == 0){
battleship[x][y] = 1;
occupiedSpots++;
}
}

Also ensure you are generating new random numbers in every iteration, else you will always be using the same random values.

Using a while-loop also ensures all 8 spots are on different locations. If you simply implement it with a for-loop without checking, there is a tendency some spots may fall on the same location twice.



Related Topics



Leave a reply



Submit