Matrix programming in Java

Observe the following coding in java & find out the output:

class Matrix{

void display()
{

    int a[]={1,6,3,4,0,12,2,5};
    int l=a.length;
    for(int i=0;i<l;i++){
        switch(i)
        {
            case 0:a[i]=a[i+1];
            case 2:a[i]=a[i+2];
            case 6:a[i++]=a[i];
            break;
            case 7:a[i]=a[i-1];
            default:a[i]=a[i++];
        }}
        System.out.print("{");
        for(int i=0;i<l;i++)
        System.out.print(a[i]+",");

        System.out.println("}");
    }

    public static void main(String args[])
    {
        new Comprogzz().display();
    }
}

Log on to comprogzz.in to learn about programming in Java ;)

{4,5,0,6,6,12,3,4} {3,6,4,0,5,5,12,4,} {6,6,4,4,0,12,5,5,} {4,4,6,6,0,12,5,5}

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Aditya Joshi
Mar 24, 2014

In the first iteration of the f o r \mathsf{for} loop, value of i = 0 \mathsf{i} = 0 . Therefore, we will execute c a s e \mathsf{case} 0 0 .

In this case,

a[0] = a[0 + 1]

In other words, the value of a [ 0 + 1 ] = a [ 1 ] \mathsf{a[0 + 1] = a[1]} is placed in a [ 0 ] \mathsf{a[0]} . 6 6 is placed in a [ 0 ] \mathsf{a[0]} .

That's basically it, we don't have to look at any other options as there is only one option whose first value is 6 6 .

according to me sum syntax error are their on that program .

program

switch(i)

# { # case 0:a[i]=a[i+1]; break; #case 2:a[i]=a[i+2];break;;
# case 6:a[i++]=a[i];break; #case 7:a[i]=a[i-1];break; # default:a[i]=a[i++]; # }

then we will get correct output {6,6,4,4,0,12,5,5,}

Yugandhar Reddy - 7 years, 2 months ago

Log in to reply

yaa break is important or else the output will change

Sid Lodha - 7 years, 2 months ago

First case 0, then case 2 and then case 6 can be executed without a break. It's called fall through. Check this out

http://javascript.about.com/od/hintsandtips/a/fallthrough.htm

Abc Def - 7 years ago

hmm

sasui wagan - 7 years, 2 months ago

Good thing this break thing works the same as in C and that I inferred how the ++ operator worked, so that all further instances of i are affected (but maybe this is identical to C too :) )

Paul Paul - 6 years, 10 months ago

First of all above program won't run and it will give you a compilation error as Comprezz class is not defined. We have to change that to Matrix class. Secondly we are getting {6,6,4,4,0,12,5,5} output because if we don't provide break; statement in switch cases it will continue to run the next cases also.

Amit Kumar Swami - 7 years, 2 months ago

Please give Clear Solution

Daniel Morla - 7 years, 2 months ago

https://ideone.com/fork/CnOCS6 - It's showinjg errors and btw, http://comprogzz.in/ is malicious.

Aryan Gaikwad - 7 years, 2 months ago
Phani Kumar
Oct 10, 2015

first switch case for a[0] a[0]=a[0+1]=a[1]=6 that's the only option

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...