Morgan Stanley Interview Question
Java DevelopersCountry: India
Interview Type: Written Test
#include<iostream>
using namespace std;
int check(int x,int y,int ar[],int pos)
{ int r;
for(int t=2;t<pos+2;t=t+2)
{
if((ar[t]==x && ar[t+1]==y)||(ar[t]==y && ar[t+1]==x))
{
r=1;
break;
}
else
{
r=0;
}
}
ar[pos]=x;
ar[pos+1]=y;
return r;
}
int main()
{
int n,i,j,ar[40],arr[5][5],temp,flag=0;
cout<<"Enter n"<<endl;
cin>>n;
cout<<"Enter elements"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cin>>arr[i][j];
}
cout<<"You have entered the following array"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<arr[i][j]<<" ";
cout<<endl;
}
cout<<"Mirroring array"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{ int m;
if(i==j)
{
m=check(i,j,ar,flag);
flag=flag+2;
}
else
{
m=check(i,j,ar,flag);
flag=flag+2;
if(m==1)
continue;
else
{
temp=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}
}
}
cout<<"Your array has become"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<arr[i][j]<<" ";
cout<<endl;
}
system("pause");
}
public static void printMirrorReflext(int[][]arr){
for(int i=0;i<arr.length;i++){
for(int j=i;j<arr.length;j++){
if(i!=j){
int temp = arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}
}
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length;j++){
System.out.print(" "+arr[i][j]);
}
System.out.println();
}
}
/*
* Given a Input Matrix of NXN . Print the matrix after mirroring on its primary diagonal
if Input is 1,2,3,4,5.6,7,8,9
Output is 1,4,7,2,5,8,3,6,9
This program is also called the transpose of a matrix
*/
package careercup;
import java.util.*;
public class ArrayDiagnol {
public static void main(String[] args) {
int rowSize, colSize,temp;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the size of matrix");
rowSize = scan.nextInt();
colSize = scan.nextInt();
if (rowSize == colSize) {
System.out.println("Row Size = " + rowSize);
System.out.println("Column Size = " + colSize);
} else
System.out
.println("OOPS!!! Wrong input. You must enter same vale for row and column size\n Run the program again to input the values");
int matrix[][] = new int[rowSize][colSize];
System.out.println("Enter " + rowSize * colSize + " elements");
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
matrix[i][j] = scan.nextInt();
}
}
System.out.println("Array elements are:");
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
for(int i = 0;i<rowSize;i++){
for(int j=i+1;j<colSize;j++){
temp = matrix[i][j];
matrix[i][j]=matrix[j][i];
matrix[j][i]=temp;
}
}
System.out.println("Array elements are:");
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
public class Matrix_Mirroring {
public static void displayMatrix(int[][] arr) {
for (int i = 0; i < arr[0].length; i++) {
for (int j = 0; j < arr[0].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println("");
}
}
public static void matrixMirror(int [][]arr)
{
int temp = 0;
for(int i=0; i< arr.length; i++)
{
for(int j=0; j<=i-1; j++)
{
temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
}
public static void main(String[] args) {
int[][] arr = {{1, 2, 3,4}, {5, 6, 7,8}, {9, 10, 11,12},{13,14,15,16}};
displayMatrix(arr);
matrixMirror(arr);
System.out.println("---");
displayMatrix(arr);
}
}
public class MatrixMirror {
private static int[][] scores = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
private static void mirror() {
for(int i=0; i< scores.length ; i++) {
for(int j=0; j<scores.length ; j++) {
System.out.print("\t");
System.out.print(scores[j][i]);
}
}
}
public static void main(String[] args) {
mirror();
}
}
- Anonymous February 19, 2014