filling my array with random values C++ - c++

I'm trying to make a simple battle ship game. I'm stuck on trying to randomly place the ships on my board.I have a feeling it is because I should be using a vector instead of an array. but I'm not sure how to create a 2d vector.
Here's what I have so far:
using namespace std;
void clearBoard(const int row, const int col)
{
int grid[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col;j++) {
grid[i][j] = 0;
cout << grid[i][j] << " ";
}
cout << endl;
}
}
void setShips(int max_ships1, int row, int col)
{
int ship_counter = 0;
while(ship_counter < max_ships1) {
int x = rand() % row;
int y = rand() % col;
int matrix[x][y]
if (matrix[x][y] != 1) {
ship_counter++;
matrix[x][y] = 1;
cout << matrix[x][y] << " ";
}
cout << endl;
}
}
int main(int argc, char* argv[])
{
int _row = atoi(argv[0]);
int _col = atoi(argv[2]);
int max_ships;
if (_row > _col) {
max_ships = _row;
}
else if (_col > _row) {
max_ships = _col;
}
else {
max_ships = _row;
}
cout << "enter the size of the board:";
cin >> _row >> _col;
clearBoard(_row, _col);
setShips(_row,_col,max_ships);
return 0;
}
If the user decides on a 3x3 board, the first function returns:
0 0 0
0 0 0
0 0 0
I'm hoping to randomly generate 1's to represent a battleship's position.
Here's an example on a 3x3 board:
1 0 0
0 1 0
1 0 0
Thanks.

Your main problem is that grid goes out of scope (and is effectively deleted) when clearBoard returns, and matrix (a completely unrelated array to grid) goes out of scope each time through your while loop. That means that you're filling grid with zeroes, then creating a bunch of other arrays with random data and sometimes setting one element to 1. You need to either make grid global, or return it from clearBoard and pass it to setShips as an argument.
Passing around multidimensional raw arrays is kind of complicated, so using a vector might make this a bit easier for you if you don't want to make the array global. To create a two-dimensional vector, you might think that std::vector<std::vector<int>> will work, but that's not quite right. You can do it that way, as mentioned in the comments, but technically, std::vector<std::vector<int>> is what's called a jagged array, meaning that you can have each row be a different length - unlike your raw 2D array, where you know that each row must always be the same length.
The proper way to make a two-dimensional array with a vector is this:
std::vector<int> grid(row * col);
grid[i * row + j] = 1; // Or i * col + j will also work.
As you can see, it's a bit more complicated to get an element out by its (x,y) coordinates. It doesn't matter which of the two calculation methods you use, but in one version i is x and j is y, while in the other, the reverse is true (i is y, etc).

You can also set up a 2D vector like this :
#include<vector>
#include<iostream>
using namespace std;
int main()
{
std::vector< std::vector<int> > grid2D;
//m * n is the size of the grid
int m = 10;
int n = 10;
//Grow rows by m
grid2D.resize(m);
//Grow Columns by n
for(int i = 0 ; i < m ; ++i) grid2D[i].resize(n);
// print out 2D grid on screen
for(i = 0 ; i < m ; ++i){
for(int j = 0 ; j < n ; ++j){
grid2D[i][j]=0;
cout<<""<<grid2D[i][j]<<" ";
}
cout<<"\n";
}
cout<<"\n";
return 0;
}

Related

How do I print out the occurences of each element in an area specified by a bounding box of a 2d matrix?

void searchValidEntries(int arr[101][101], int XL, int YL, int XH, int YH){
int sizeX = (XL - XH) + 1;
int sizeY = (YH - YL) + 1;
for (int i = XH; i < XH + sizeX; i++ ){
for (int j = YL; j < YL + sizeY; j++){
cout << arr[i][j] << ' ';
}
}
}
In the above code, I'm looping through a bounding box in a 2d array.
The coordinates of the array to be assigned a bounding box and the array itself are included as parameters in a function : int arr[101][101], int XL, int YL, int XH, int YH
The commented code cout << arr[i][j] << ' '; prints out the elements in the designated bounding box and I've run it so I know it actually prints out.
However, I want to get the frequency of each element in the bounding box, with the exception of 0, to be printed out.
I'm assuming that the required code will go in the for loop but I have no idea to start as I'm now learning all the semantics of C++.
I'm expecting an output of something like:
5 --> 3
83 --> 2
23 --> 9
There are multiple ways to do this. I suggest using a vector< pair< int, int >>.
Each element of this vector holds two things:
1- a value that has been visited
2- the occurrence of this value
I've added some comments to my code to clarify this method.
#include <iostream>
using namespace std;
#include <vector>
void searchValidEntries(int arr[101][101], int XL, int YL, int XH, int YH) {
vector<pair<int, int>> holder_vec;
int sizeX = (XL - XH) + 1;
int sizeY = (YH - YL) + 1;
for (int i = XH; i < XH + sizeX; i++) {
for (int j = YL; j < YL + sizeY; j++) {
bool thisElementIsNew = true; // By default, it's the first occurence of this element.
for (auto& it : holder_vec) { // Iterate the vector of previously considered elements and chekc this target value.
if (it.first == arr[i][j]) {
it.second++; // Increase the occurence of this element by one.
thisElementIsNew = false; // This value has been previously visited.
break; // Stop iterating vector
}
}
if (thisElementIsNew) { // Add a new pair to holder_vec;
const pair<int, int> newPair(arr[i][j], 1); // newPair.first = The value of this element. // newPair.second = the occurence of this value.
holder_vec.push_back(newPair);
}
}
}
// Print result:
for (const auto& it : holder_vec) {
cout << it.first << " ---> " << it.second << endl;
}
}

I am trying to write a program find maximum element of each column in a matrix but getting errors as cannot convert 'int (*)[m]' to 'int (*)[100]'

#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
//Function to calculate largest column
void largestInColumn(int mat[][MAX], int rows, int cols)
{
for (int i = 0; i < cols; i++) {
// initialize the maximum element with 0
int maxm = mat[0][i];
// Run the inner loop for rows
for (int j = 1; j < rows; j++) {
// check if any element is greater than the maximum element of the column and replace it
if (mat[j][i] > maxm)
maxm = mat[j][i];
}
cout << maxm << endl;
}
}
// Driver code
int main()
{
int n , m ;
cin>>n>>m;
int mat[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>mat[i][j];
}
}
largestInColumn(mat, n, m);
return 0;
}
I will answer the question using valid C++ statements. There are no VLAs in C++. I will use a std::vector instead.
There is also no need to store any value of the matrix for this task. You can make the decision already during reading of the values.
#include <iostream>
#include <vector>
#include <limits>
int main() {
// Read number of rows and columns
if (size_t numberOfRows{}, numberOfColumns{}; std::cin >> numberOfRows >> numberOfColumns) {
// Here we will store the max values per column, so, the result
std::vector<int> maxColumnValue(numberOfColumns, std::numeric_limits<int>::lowest());
// Read all rows and columns
for (size_t row{}; row < numberOfRows; ++row)
for (size_t col{}; col < numberOfColumns; ++col)
// If the current value of the column is greater than the current max value, then use new value instead
if (int value{ std::numeric_limits<int>::lowest() }; std::cin >> value)
if (value > maxColumnValue[col]) maxColumnValue[col] = value;
// Show result to the user
for (const int m : maxColumnValue) std::cout << m << '\n';
}
return 0;
}
The cause of the error is due to you trying to pass a variable-length-array to a function that requires a standard 2D array.
First, variable-length-arrays (VLA's) are not part of standard C++. Arrays in C++ require that the sizes of the array are known at compile-time, not runtime. So pretend they don't exist, because technically, they do not exist in standard C++.
Thus you have two choices:
Declare a non-variable-size 2D array and use that, or
Use a container that is built to have dynamic size, such as std::vector.
Since you did not specify how large n could be, then solution 2 is safer.
Given that, here is your code using std::vector:
#include <vector>
#include <iostream>
using Int1D = std::vector<int>;
using Int2D = std::vector<Int1D>;
//Function to calculate largest column
void largestInColumn(Int2D& mat, int rows, int cols)
{
for (int i = 0; i < cols; i++)
{
int maxm = mat[0][i];
for (int j = 1; j < rows; j++)
{
if (mat[j][i] > maxm)
maxm = mat[j][i];
}
std::cout << maxm << std::endl;
}
}
int main()
{
int n , m ;
std::cin >> n >> m;
Int2D mat(n, Int1D(m));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
std::cin >> mat[i][j];
}
}
largestInColumn(mat, n, m);
}
Using the input:
3 3
1 2 3
1 4 9
76 34 21
The output is:
76
34
21

How to print a grid vector in C++

I am working on C++ printing vectors in grid.
Here, I need to put random numbers in a vector size of 3x * 3y. And then I have to print them out with two dimensional matrix with one array.
I do not understand how to represent two dimensional matrix with one array.
In addition, I am not sure how to print out multidimensional vectors. I have to work on print_vector function which prints vectors with grid form.
Could you please help me to improve this code below?
int main()
{
populate_vector();
return 0;
}
void populate_vector()
{
int x,y;
cout<<"Enter two Vectors x and y \n->";
cin>> x;
cin>> y;
srand((unsigned)time(NULL));
std::vector<int> xVector((3*x) * (3*y));
for(int i = 0; (i == 3*x && i == 3*y); ++i){
xVector[i] = rand() % 255 + 1;
if(i == 3*x){
cout << "\n";
}
}
print_vector(xVector);
}
void print_vector(vector<int> &x) {
}
I'm not an expert, but I like to just have a vector of vectors.. something like:
void print_vector(vector<vector<int>>& m_vec)
{
for(auto itY: m_vec)
{
for(auto itX: itY)
cout << itX << " ";
cout << endl;
}
}
void populate_vector()
{
int x,y;
cout << "Enter Two Vectors x and y \n ->";
cin >> x;
cin >> y;
srand((unsigned)time(NULL));
vector<vector <int>> yVector;
for(auto i = 0; i < y*3; ++i)
{
vector<int> xVector;
for(auto j = 0; j < x*3; ++j)
xVector.push_back(rand()%255+1);
yVector.push_back(xVector);
}
print_vector(yVector);
}
EDIT: Ooh, I'd never seen this site before, thanks Saykou... here is the code working: http://cpp.sh/3vzg
Something like this will clarify your code, there is a procedure where the vector is populated, and another one where the vector is printed
int main()
{
int x,y;
std::cout<<"Enter two Vectors x and y \n->";
std::cin>> x;
std::cin>> y;
srand((unsigned)time(NULL));
int xSize = x * 3; // it is important to have the size of the final grid stored
int ySize = y * 3; // for code clarity
std::vector<int> xVector( xSize * ySize);
// iterate all y
for ( y = 0 ; y < ySize; ++y) {
// iterate all x
for ( x = 0 ; x < xSize; ++x) {
// using row major order https://en.wikipedia.org/wiki/Row-_and_column-major_order
xVector[y * xSize + x] = rand() % 255 + 1;
}
}
// when printing you want to run y first
for ( y = 0 ; y < ySize; ++y) {
for ( x = 0 ; x < xSize; ++x) {
// iterate all y
printf("%d ", xVector[y * xSize + x] );
}
printf("\n");
}
}
I think you want to pay attention to this step, where you can convert x and y position into a one array dimension. It's simple you just have to multiply the y by the size of x and add x.
So something like this in two dimensions
1 2 3
4 5 6
will end up in something like this
1 2 3 4 5 6
you can see it running here

Retrieve index of element of array stored in vector

I have a 2D array used to store non-repeated values and some entries are randomly picked and push_back-ed into a vector as favorite list.
int num[10][10];
vector<int> fav_list;
int retrieved_i, retrieved_j, retrieve_vector_position;
for(i=0;i<10;i++) for(j=0;j<10;j++)
// ...assign numbers to num...
fav_list.push_back(num[2][3]);
fav_list.push_back(num[4][7]);
fav_list.push_back(num[6][2]);
//...push_back more random selected num[...][...] into fav_list...
The problem is, how can I retrieve the i, j index of particular fav_list[...]?
I've tried to make struct struct Num{int value, index_i, index_j;}num[10][10]; so that I can do in this way
retrieved_i = fav_list[retrieve_vector_position].index_i;
retrieved_j = fav_list[retrieve_vector_position].index_j;
but I wish to know is there any other better/ efficient ways?
Using a plain vector to store your 2D array would solve the problem. You could access elements in the vector by calculating absolute index (i * row_len + j) and store in fav_list absolute indices.
Also, you may want to use std::unordered_map for fav_list. Generally, hash tables is the most efficient data structure for such caches.
There are a few possibilities depending on how often you want to access the i & j indices / the favorite number itself.
One approach is, to save the indices instead of the number (or additional to it). With this approach, more memory is required but the the time to access the indices will be constant, regardless how big your array becomes. This uses std::pair to store 2 elements in your favorite vector.
#include <vector>
#include <iostream>
#include <utility>
using namespace std;
int main(int argc, char* argv[]) {
int num[10][10];
vector<std::pair<int, int>> fav_list;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (/* your condition for favorites */) {
fav_list.push_back(num[i][j]);
}
}
}
/* example get the indices of the first favorite */
cout << "i: " << fav_list[0].first << "j: " << fav_list[0].second << endl;
/* example get the first favorite */
cout << num[fav_list[0].first][fav_list[0].second] << endl;
return 0;
}
Another approach is to "lookup" the indices, when you require it: it has the condition, that one number is not multiple times contained in your num[][] array (otherwise the first entry is found). There is no additional memory overhead required, but the time to lookup the indices will increase when your array gets bigger.
#include <vector>
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
int num[10][10];
vector<int> fav_list;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (/* your condition for favorites */) {
fav_list.push_back(num[i][j]);
}
}
}
/* example get the indices of the first favorite */
int indexI = -1, indexJ = -1;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (fav_list[0] == num[i][j]) {
indexI = i;
indexJ = j;
break;
}
}
}
cout << "i: " << indexI << "j: " << indexJ << endl;
/* example get the first favorite */
cout << fav_list[0] << endl;
return 0;
}
Instead of storing 3 variable value, x and y just store a single unsigned int via which you can retrieve x and y.
struct fav_list
{
unsigned int total_rows;
unsigned int total_columns;
fav_list(unsigned int _rows, unsigned int _columns)
{
total_rows = _rows;
total_columns = _columns;
}
unsigned int get_x(unsigned int _index)
{
return v[_index] / total_columns;
}
unsigned int get_y(unsigned int _index)
{
return v[_index] % total_columns;
}
void append_xy_to_list(unsigned int _x, unsigned int _y)
{
v.push_back(_x * total_columns + _y);
}
vector <unsigned int> v;
};
fav_list f(10, 10);
for(x = 0; x < 10; ++x)
{
for(y = 0; y < 10; ++y)
{
//suppose you want to store the indexes of element num[x][y] then:
f.append_xy_to_list(x, y);
}
}
retrieved_i = f.get_x(retrieve_vector_position);
retrieved_j = f.get_y(retrieve_vector_position);

Sorting an array diagonally

I've looked up some websites but I couldn't find an answer to my problem.
Here's my code:
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <time.h>
#include<iomanip>
#include<array>
#include <algorithm>
using namespace std;
const int AS = 6;
int filling(void);
void printing(int[AS][AS]);
int forsorting(int[][AS], int);
int main()
{
int funny = 0;
int timpa = 0;
int counter = 0;
int Array[AS][AS];
srand(time(0));
for (int i = 0; i<AS; i++)
{
for (int j = 0; j<AS; j++)
Array[i][j] = filling();
}
cout << "The unsorted array is" << endl << endl;
printing(Array);
cout << "The sorted array is" << endl << endl;
for (int il = 0; il<AS; il++)
{
for (int elle = 0; elle<AS; elle++)
Array[il][elle] =forsorting(Array, funny);
printing(Array);
}
system("PAUSE");
return 0;
}
int filling(void)
{
int kira;
kira = rand() % 87 + 12;
return kira;
}
void printing(int Array[AS][AS])
{
int counter = 0;
for (int i = 0; i<AS; i++)
{
for (int j = 0; j<AS; j++)
{
cout << setw(5) << Array[i][j];
counter++;
if (counter%AS == 0)
cout << endl << endl;
}
}
}
int forsorting(int Array[AS][AS], int funny)
{
int c, tmp, x;
int dice = 0;
int Brray[AS*AS];
int timpa = 0;
int super = 0;
//Transofrming Array[][] into Brray[]
for (int i = 0; i < AS; i++)
{
for (int k = 0; k < AS; k++)
{
Brray[timpa] = Array[i][k];
timpa++;
}
}
//Bubble sorting in Brray[]
for (int passer = 1; passer <= AS-1; passer++)
{
for (int timon = 1; timon <= AS-1; timon++)
{
if (Brray[timpa]>Brray[timpa + 1])
{
super = Brray[timpa];
Brray[timpa] = Brray[timpa + 1];
Brray[timpa + 1] = super;
}
}
}
//Transforming Brray[] into Array[][]
for (int e = 0; e<AS; e++)
{
for (int d = 0; d<AS; d++)
{
Brray[dice] = Array[e][d];
dice++;
}
}
***There's a part missing here***
}
What I have to do is, write a program using 3 functions.
The 1st function would fill my 2D array randomly (no problem with this part)
the 2nd function would print the unsorted array on the screen (no problem with this part)
and the 3rd function would sort my array diagonally as shown in this picture:
Then I need to call the 2nd function to print the sorted array. My problem is with the 3rd function I turned my 2D array into a 1D array and sorted it using Bubble sorting, but what I can't do is turn it back into a 2D array diagonaly sorted.
If you can convert from a 2D array to a 1D array, then converting back is the reverse process. Take the same loop and change around the assignment.
However in your case the conversion itself is wrong. It should take indexes in the order (0;0), (0;1), (1;0). But what it does is take indexes in the order (0;0), (0;1), (1;1).
My suggestion is to use the fact that the sum of the X and Y coordinates on each diagonal is the same and it goes from 0 to AS*2-2.
Then with another loop you can check for all possible valid x/y combinations. Something like this:
for ( int sum = 0; sum < AS*2-1; sum++ )
{
for ( int y = sum >= AS ? sum-AS+1 : 0; y < AS; y++ )
{
x = sum - y;
// Here assign either from Array to Brray or from Brray to Array
}
}
P.S. If you want to be really clever, I'm pretty sure that you can make a mathematical (non-iterative) function that converts from the index in Brray to an index-pair in Array, and vice-versa. Then you can apply the bubble-sort in place. But that's a bit more tricky than I'm willing to figure out right now. You might get extra credit for that though.
P.P.S. Realization next morning: you can use this approach to implement the bubble sort directly in the 2D array. No need for copying. Think of it this way: If you know a pair of (x;y) coordinates, you can easily figure out the next (x;y) coordinate on the list. So you can move forwards through the array from any point. That is all the the bubble sort needs anyway.
Suppose you have a 0-based 1-dimensional array A of n = m^2 elements. I'm going to tell you how to get an index into A, given and a pair of indices into a 2D array, according to your diagonalization method. I'll call i the (0-based) index in A, and x and y the (0-based) indices in the 2D array.
First, let's suppose we know x and y. All of the entries in the diagonal containing (x,y) have the same sum of their coordinates. Let sum = x + y. Before you got to the diagonal containing this entry, you iterated through sum earlier diagonals (check that this is right, due to zero-based indexing). The diagonal having sum k has a total of k + 1 entries. So, before getting to this diagonal, you iterated through 1 + 2 + ... + (sum - 1) entries. There is a formula for a sum of the form 1 + 2 + ... + N, namely N * (N + 1) / 2. So, before getting to this diagonal, you iterated through (sum - 1) * sum / 2 entries.
Now, before getting to the entry at (x,y), you went through a few entries in this very diagonal, didn't you? How many? Why, it's exactly y! You start at the top entry and go down one at a time. So, the entry at (x,y) is the ((sum - 1) * sum / 2 + y + 1)th entry, but the array is zero-based too, so we need to subtract one. So, we get the formula:
i = (sum - 1) * sum / 2 + y = (x + y - 1) * (x + y) / 2 + y
To go backward, we want to start with i, and figure out the (x,y) pair in the 2D array where the element A[i] goes. Because we are solving for two variables (x and y) starting with one (just i) and a constraint, it is trickier to write down a closed formula. In fact I'm not convinced that a closed form is possible, and certainly not without some floors, etc. I began trying to find one and gave up! Good luck!
It's probably correct and easier to just generate the (x,y) pairs iteratively as you increment i, keeping in mind that the sums of coordinate pairs are constant within one of your diagonals.
Store the "diagonally sorted" numbers into an array and use this to display your sorted array. For ease, assume 0-based indexing:
char order[] = { 0, 1, 3, 6, 10, 2, 4, 7, 11, 15, .. (etc)
Then loop over this array and display as
printf ("%d", Array[order[x]]);
Note that it is easier if your sorted Array is still one-dimensional at this step. You'd add the second dimension only when printing.
Following may help you:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
template<typename T>
class DiagArray
{
public:
DiagArray(int size) : width(size), data(size * size), orders(size * size)
{
buildTableOrder(size);
}
const T& operator() (int x, int y) const { return data[orders[width * y + x]]; }
T& operator() (int x, int y) { return data[orders[width * y + x]]; }
void sort() { std::sort(data.begin(), data.end()); }
void display() const {
int counter = 0;
for (auto index : orders) {
std::cout << std::setw(5) << data[index];
counter++;
if (counter % width == 0) {
std::cout << std::endl;
}
}
}
private:
void buildTableOrder(int size)
{
int diag = 0;
int x = 0;
int y = 0;
for (int i = 0; i != size * size; ++i) {
orders[y * size + x] = i;
++y;
--x;
if (x < 0 || y >= size) {
++diag;
x = std::min(diag, size - 1);
y = diag - x;
}
}
}
private:
int width;
std::vector<T> data;
std::vector<int> orders;
};
int main(int argc, char *argv[])
{
const int size = 5;
DiagArray<int> da(size);
for (int y = 0; y != size; ++y) {
for (int x = 0; x != size; ++x) {
da(x, y) = size * y + x;
}
}
da.display();
std::cout << std::endl;
da.sort();
da.display();
return 0;
}
Thank you for your assistance everyone, what you said was very useful to me. I actually was able to think about clearly and came up with a way to start filling the array based on your recommendation, but one problem now, Im pretty sure that my logic is 99% right but there's a flaw somewhere. After I run my code the 2nd array isnt printed on the screen. Any help with this?
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <time.h>
#include<iomanip>
#include<array>
#include <algorithm>
using namespace std;
const int AS = 5;
int filling(void);
void printing(int[AS][AS]);
int forsorting(int[][AS], int);
int main()
{
int funny = 0;
int timpa = 0;
int counter = 0;
int Array[AS][AS];
srand(time(0));
for (int i = 0; i<AS; i++)
{
for (int j = 0; j<AS; j++)
Array[i][j] = filling();
}
cout << "The unsorted array is" << endl << endl;
printing(Array);
cout << "The sorted array is" << endl << endl;
for (int il = 0; il<AS; il++)
{
for (int elle = 0; elle<AS; elle++)
Array[il][elle] =forsorting(Array, funny);
}
printing(Array);
system("PAUSE");
return 0;
}
int filling(void)
{
int kira;
kira = rand() % 87 + 12;
return kira;
}
void printing(int Array[AS][AS])
{
int counter = 0;
for (int i = 0; i<AS; i++)
{
for (int j = 0; j<AS; j++)
{
cout << setw(5) << Array[i][j];
counter++;
if (counter%AS == 0)
cout << endl << endl;
}
}
}
int forsorting(int Array[AS][AS], int funny)
{int n;
int real;
int dice = 0;
int Brray[AS*AS];
int timpa = 0;
int super = 0;
int median;
int row=0;
int col=AS-1;
//Transofrming Array[][] into Brray[]
for (int i = 0; i < AS; i++)
{
for (int k = 0; k < AS; k++)
{
Brray[timpa] = Array[i][k];
timpa++;
}
}
//Bubble sorting in Brray[]
for (int passer = 1; passer <= AS-1; passer++)
{
for (int timon = 1; timon <= AS-1; timon++)
{
if (Brray[timpa]>Brray[timpa + 1])
{
super = Brray[timpa];
Brray[timpa] = Brray[timpa + 1];
Brray[timpa + 1] = super;
}
}
}
//Transforming Brray[] into sorted Array[][]
for(int e=4;e>=0;e--)//e is the index of the diagonal we're working in
{
if(AS%2==0)
{median=0.5*(Brray[AS*AS/2]+Brray[AS*AS/2-1]);
//We start filling at median - Brray[AS*AS/2-1]
while(row<5 && col>=0)
{real=median-Brray[AS*AS/2-1];
Array[row][col]=Brray[real];
real++;
col--;
row++;}
}
else {
median=Brray[AS*AS/2];
//We start filling at Brray[AS*AS/2-AS/2]
while(row<5 && col>=0)
{real=Brray[AS*AS/2-AS/2];
n=Array[row][col]=Brray[real];
real++;
col--;
row++;}
}
}
return n;
}
Thanks again for your assistance