Question

In: Computer Science

Given int B[4][4]={...}; Write a code segment that exchange the values about the main diagonal of...

Given
int B[4][4]={...};

Write a code segment that exchange the values about the main diagonal of the matrix.

Solutions

Expert Solution

//C program


#include <stdio.h>

int main()
{   
    //variable initialization
    int B[4][4],i,j,m,n;
    int row, col, temp;
    //enter user input values to matrix B
    printf("\nEnter values to the matrix :: \n");
       for (i = 0; i < 4; i++)
        {
            for (j = 0; j < 4; j++)
            {
                 printf("\nEnter a[%d][%d] value :: ",i,j);
                 scanf("%d", &B[i][j]);
        }
    }
    //for loop show original matrix
    printf("\nThe given matrix is :: \n\n");

        for (i = 0; i < 4; ++i)
        {
            for (j = 0; j < 4; ++j)
            {
                printf("\t%d", B[i][j]);
            }
            printf("\n\n");
        }
    //Interchanges diagonal of the matrix
     
    for(row=0; row < 4; row++)
    {
        col = row;

        temp = B[row][col];
        B[row][col] = B[row][(4 - col)-1];
        B[row][(4 - col)-1] = temp;
    }

    // Prints the interchanged diagonals matrix
    printf("\nMatrix after diagonals interchanged: \n");
    for (i = 0; i < 4; ++i)
        {
            for (j = 0; j < 4; ++j)
            {
                printf("\t%d", B[i][j]);
            }
            printf("\n\n");
        }

    return 0;
}

Output::

Note:: If you have any queries regarding this please comment.


Related Solutions

Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num ==...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num == 0) return 0; else if (num > 100) return -1; else     return num + tq( num – 1 ); } Group of answer choices: 0 4 -1 10 Q2: Given that values is of type LLNode<Integer> and references a linked list (non-empty) of Integer objects, what does the following code do if invoked as mystery(values)? int mystery(LLNode<Integer> list) {    if (list.getLink() ==...
What is the Θ( ) for each code segment below? (a) for( int i = 1,...
What is the Θ( ) for each code segment below? (a) for( int i = 1, i<= n, i = i*2){       some constant operation } (b) for( int i = 1, i<= n, i++){      for( int j = 2*i, j<=n, j++){           some constant operation      } } (c) for( int i = 1, i<= n, i = i*2){      for( int j = 1, j<=n, j = j*2){           some constant operation      } }
int main() { float A[4] = { 0.51, 1.23, 7.4, 10.88}; float B[4] = { -11.1,...
int main() { float A[4] = { 0.51, 1.23, 7.4, 10.88}; float B[4] = { -11.1, 78.044, 12.009, -9.99}; float s = 0.0; for (int i=0; i<4; i++) { s = s + A[i]*B[i]; } } change this c languange to arm assembly languange extension of vfp registor
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
Given the list values = [], write code that fills the list with each set of...
Given the list values = [], write code that fills the list with each set of numbers below. Note: use loops if it is necessary 1 2 3 4 5 6 7 8 9 10 0 2 4 6 8 10 12 14 16 18 20 1 4 9 16 25 36 49 64 81 100 0 0 0 0 0 0 0 0 0 0 1 4 9 16 9 7 4 9 11 0 1 0 1 0...
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code should print the numbers in descending order.
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
1)What is the output of the following code? struct someType { int a; int b; };...
1)What is the output of the following code? struct someType { int a; int b; }; void f1(someType &s) { s.a = 1; s.b = 2; } someType f2(someType s) { someType t; t = s; s.a = 3; return t; } int main() { someType s1, s2; f1(s1); s2 = f2(s1); cout << s1.a << '-' << s1.b << '-' << s2.a << '-' << s2.b << '-' << endl; return 0; } ------------------------------------------------------------------------------------------------------------------ 2) struct dateType { int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT