Click here to Skip to main content
15,925,255 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
create 5x5 massive with incidental elements from 0 to one(a[5][5] rand() % 100.Then change the places of elements which are located in the right and left of a diagonal of massive.I tried with several methods but none of them helped(

What I have tried:

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

void main() {
	const int	S = 5;
	int arr[S][S] = {};
	for (int i = 0; i < S; i++) {
		for (int j = 0; j < S; j++) {
			arr[i][j] = rand() % 100;
			cout << arr[i][j] << "\t";

		}
		cout << "\n";

	}
	cout << "\n\n\n\n";
	for (int i = 0; i < S; i++) {
		for (int j = 0; j < S; j++) {
			arr[i][j] = rand() % 100;
			if (i < j)
				int m = 0;
			int m = j - i;
			arr[i][j] = arr[i + m][j - m]; { cout << arr[i + m][j - m] << "\t"; }
			if (j < i)
				int n = 0;
			int n = i - j;
			arr[i][j] = arr[i - n][j + n]; { cout << arr[i - n][j + n] << "\t"; }
			
			
		}
		cout << "\n";
	}
}
Posted
Updated 22-Dec-17 6:25am
v2
Comments
Member 13589920 22-Dec-17 6:54am    
from 0 to 100*
Patrice T 22-Dec-17 7:33am    
Use Improve question to update your question.
Richard MacCutchan 22-Dec-17 7:24am    
Draw the array on a piece of paper and you can see the address co-ordinates of the cells in the diagonals.

Your changing process isnt correct, because you fill your matrix with new values.

I would make a copy and than use the transform mechanism to fill the target matrix.
C++
int source[S][S] = {};
memcpy( source, arr, 5*5*sizeof(int));//make a memory copy of the source data
//apply transformation algorithm
for (int i = 0; i < S; i++) {
    for (int j = 0; j < S; j++) {
      int k = i; // best guess
      int n = ... //TODO
      arr[i][j] = source[k][n];

Tip: fill your with simple test data (like ordered numbers) to debug and test the trasformation.
 
Share this answer
 
Comments
Member 13589920 22-Dec-17 14:41pm    
Can you write the whole code, please?
As Richard mentioned, drawing this out on paper will help you see the pattern. It it is very simple.

Also - have a look at the function swap in the standard library. It will take of that aspect of things for you.

Another thing - remember to seed the random number generator (RNG) with srand(). You have two options to seed it, at minimum. One is to use a predetermined value. Prime numbers are good for that like 47 or 43 and bigger values are OK too. With a predetermined seed you will get the same sequence of random values. The other option is to use a random value for the seed. Using time is common for this. Here is an example :
C++
int seed = time( NULL ) % RAND_MAX;
This will cause the RNG to return a more random sequence of values. It's up to you to choose what kind of sequence you want.

Lastly, you might to implement a function to display the matrix for you. You will need to do it twice so you might as well use one piece of code to do it. It might take a little while to figure out how to pass the matrix to a function so this is optional. It will likely be educational to figure it out though.

Here's a hint - you have the size defined as a constant and that's a good start. You can use typedef and define the matrix as a type and that will make it easier. ;)

Best of luck.
 
Share this answer
 
v2
Comments
Member 13589920 22-Dec-17 14:40pm    
sorry for being annoying but can you please write the code?:)
Rick York 22-Dec-17 15:41pm    
I will for the typedef but you need to figure out the rest of it.

const int MatrixSize = 5;
typedef int MyMatrix[MatrixSize][MatrixSize];

MyMatrix source; // declare an instance of the matrix

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900