On the following program what is the output and what the method mystery() does?
ID: 3569400 • Letter: O
Question
On the following program what is the output and what the method mystery() does?
import java.util.Arrays;
public class Practice2Dee {
public static void b(int[][] z) {
for (int i = 0; i < z.length; ++i)
for (int j =0; j < z[i].length; ++j)
z[i][j] = (i + 1) * (j + 1);
}
public static double mystery(int[][] z) {
double x = 0;
for (int i = 0; i < z.length; ++i)
for (int j = 0; j < z.length; ++j)
x += z[i][j];
return x / (z.length * z[0].length);
}
public static void main(String[] args) {
int a[][] = new int[3][4];
b(a);
System.out.println(Arrays.deepToString(a));
System.out.println(mystery(a));
}
}
Explanation / Answer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#include<alloc.h>
void Push(int, node **);
void Display(node **);
int Pop(node **);
int Sempty(node *);
typedef struct stack {
int data;
struct stack *next;
} node;
void main() {
node *top;
int data, item, choice;
char ans, ch;
clrscr();
top = NULL;
printf(" Stack Using Linked List : nn");
do {
printf(" The main menu");
printf(" 1.Push 2.Pop 3.Display 4.Exit");
printf(" Enter Your Choice");
scanf("%d", &choice);
switch (choice) {
case 1:
printf(" Enter the data");
scanf("%d", &data);
Push(data, &top);
break;
case 2:
if (Sempty(top))
printf(" Stack underflow!");
else {
item = Pop(&top);
printf(" The popped node is%d", item);
}
break;
case 3:
Display(&top);
break;
case 4:
printf(" Do You want To Quit?(y/n)");
ch = getche();
if (ch == 'y')
exit(0);
else
break;
}
printf(" Do you want to continue?");
ans = getche();
getch();
clrscr();
} while (ans == 'Y' || ans == 'y');
getch();
}
void Push(int Item, node **top) {
node *New;
node * get_node(int);
New = get_node(Item);
New->next = *top;
*top = New;
}
node * get_node(int item) {
node * temp;
temp = (node *) malloc(sizeof(node));
if (temp == NULL)
printf(" Memory Cannot be allocated");
temp->data = item;
temp->next = NULL;
return (temp);
}
int Sempty(node *temp) {
if (temp == NULL)
return 1;
else
return 0;
}
int Pop(node **top) {
int item;
node *temp;
item = (*top)->data;
temp = *top;
*top = (*top)->next;
free(temp);
return (item);
}
void Display(node **head) {
node *temp;
temp = *head;
if (Sempty(temp))
printf(" The stack is empty!");
else {
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
}
getch();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#include<alloc.h>
void Push(int, node **);
void Display(node **);
int Pop(node **);
int Sempty(node *);
typedef struct stack {
int data;
struct stack *next;
} node;
void main() {
node *top;
int data, item, choice;
char ans, ch;
clrscr();
top = NULL;
printf(" Stack Using Linked List : nn");
do {
printf(" The main menu");
printf(" 1.Push 2.Pop 3.Display 4.Exit");
printf(" Enter Your Choice");
scanf("%d", &choice);
switch (choice) {
case 1:
printf(" Enter the data");
scanf("%d", &data);
Push(data, &top);
break;
case 2:
if (Sempty(top))
printf(" Stack underflow!");
else {
item = Pop(&top);
printf(" The popped node is%d", item);
}
break;
case 3:
Display(&top);
break;
case 4:
printf(" Do You want To Quit?(y/n)");
ch = getche();
if (ch == 'y')
exit(0);
else
break;
}
printf(" Do you want to continue?");
ans = getche();
getch();
clrscr();
} while (ans == 'Y' || ans == 'y');
getch();
}
void Push(int Item, node **top) {
node *New;
node * get_node(int);
New = get_node(Item);
New->next = *top;
*top = New;
}
node * get_node(int item) {
node * temp;
temp = (node *) malloc(sizeof(node));
if (temp == NULL)
printf(" Memory Cannot be allocated");
temp->data = item;
temp->next = NULL;
return (temp);
}
int Sempty(node *temp) {
if (temp == NULL)
return 1;
else
return 0;
}
int Pop(node **top) {
int item;
node *temp;
item = (*top)->data;
temp = *top;
*top = (*top)->next;
free(temp);
return (item);
}
void Display(node **head) {
node *temp;
temp = *head;
if (Sempty(temp))
printf(" The stack is empty!");
else {
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.