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

1. Write a C++ code segment to read 50 temperature values in Fahrenheit and to convert...
1. Write a C++ code segment to read 50 temperature values in Fahrenheit and to convert them to Celsius. You convert a Fahrenheit temperature to Celsius by using the following formula: Celsius = 5.0 / 9 * (Fahrenheit - 32). 2.A client has purchased 20 products in a store. Write a C++ code segment to read the unit price and the number of items of each product and to compute and print the total price of all these products.
Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
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
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc,...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc, char **argv) { int n, k, l, r, t, d, i; char str[65]; /* Make the user enter a non-negative integer */ printf("Please enter a non-negative integer: "); scanf("%d", &n); while (n < 0) { printf("Sorry, your input is incorrect.\n"); printf("Please enter a non-negative integer: "); scanf("%d", &n); } /* Convert the integer to reversed binary: e.g. 6 gets converted to 011 */ if...
what is the output? int main ( ) { int a = 3, b= 2, c=...
what is the output? int main ( ) { int a = 3, b= 2, c= 1, d, e, f, g; d = a&b;    e = a | c; f = a >> 1, g = a << 1; cout << “d= “ << d << “ e = “ << e << “ f = “ << f << “g = “ << g << endl; }
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...
Given the code segment: def f1(a, b = 2): if a : print(1) f1(1) what is...
Given the code segment: def f1(a, b = 2): if a : print(1) f1(1) what is the result of executing the code segment? a) Syntax error b) Runtime error c) No error d) Logic error in which scenario(s) is using a dictionary useful? a) To store the dates of lesson that each student is absent for so that warning letters may be issued to student exceeding a limit. b) To store the student numbers for students who sat for an...
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; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT