-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment6-part2
More file actions
215 lines (181 loc) · 4.7 KB
/
assignment6-part2
File metadata and controls
215 lines (181 loc) · 4.7 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Assignment 6 cont'd
// performs all tasks listed in Assign6
import java.util.Random;
import java.util.Scanner;
public class Assign6contd
{
// declare instance variables
int rows = 3, columns = 4;
// declare arrays
int[ ] List1 = new int[12];
int[ ] List2 = new int[12];
int[ ][ ] List3 = new int[12][12];
int[ ] List4 = new int[12];
Scanner kb = new Scanner(System.in);
// implement constructor
public Program6contd()
{
this.List1 = List1;
this.List2 = List2;
this.List3 = List3;
this.List4 = List4;
}
// #1 - read twelve integers and store them in a 1D array, List1. Print the Array
public void num1()
{
List1 = new int[12];
System.out.println("Enter 12 integer values:");
for(int i=0; i < 12; i++)
{
List1[i] = kb.nextInt();
}
System.out.println("\nList1: ");
print1DArray(List1);
}
// #2 - Store List1 in List2
// - Reverse the content of array List2
// - Multiply each element of List1 by List2 and store them in List2
// - Print List 2
public void num2()
{
List2 = new int[12];
for(int i = 12 -1, j=0; i >= 0 && j < 12; i--, j++) // two diff arrays need diff pointers
{
List2[j] = List1[i];
}
for(int i=0; i < 12; i++)
{
List2[i] = List2[i] * List1[i];
}
System.out.println("\nList2: ");
print1DArray(List2);
}
// #3 - delete the third element of the array List1. Print the array
public void num3()
{
for(int i =2; i < 12 - 1; i++)
{
List1[i] = List1[i + 1]; // deletion line
}
System.out.println("\nList1: ");
print1DArray(List1);
}
// #4 - insert the number 22 in the array, List1, at position n. Print the array
public void num4()
{
int n = generateRandomPosition(List1); // ???????
for(int i = 12 - 1; i>n; i--) // decrementing = i>n, i--
// incrementing = i<n, i++
{
List1[i] = List1[i - 1];
}
List1[n] = 22;
System.out.println("\nList1: ");
print1DArray(List1);
}
// #5 - Store the elements of the array List1 as a 2D array, List3, with 3 rows and 4 columns
// - Print the array
// - Calculate the sum of each row and print it
// - Save the sum of each row in an array, List4
public void num5()
{
List3 = new int[rows][columns];
int k = 0;
for(int i =0; i < rows; i++)
{
for(int j=0; j < columns; j++)
{
List3[i][j] = List1[k];
k++;
}
}
System.out.println("\nList3:");
print2DArray(List3);
List4 = new int[rows];
for(int i=0; i <rows; i++)
{
int sum = 0;
for(int j=0; j < columns; j++)
{
sum += List3[i][j];
}
System.out.println("Sum of row #" + (i + 1) + ": " + sum);
List4[i] = sum;
}
}
// #6 - sort the array List4 in an increasing order, using the selection sort
// - print the sorted array
public void num6()
{
for(int i=0; i < rows - 1; i++)
{
int min = i; // points to beginning of array but also keeps
for(int j = i + 1; j<rows; j++) // j always has to be the next number up from i! i+1
{
if(List4[min] > List4[j]) // if greater, min = j
{
min = j; // j is the pointer to the index, index is in this case needs to be the min because we need to remember the min
}
}
if(i != min)
{
// SWAP LOOP
int temp = List4[i]; // temp is where min gets swapped to
List4[i] = List4[min];
List4[min] = temp;
}
}
System.out.println("\nList4: ");
print1DArray(List4);
}
// #7 - Search the array List4 to find the key entered by the user
// - print an appropriate message if the key is not in the array
// - otherwise print the position where the key is found
public void num7()
{
System.out.println("\nEnter key number: ");
int key = kb.nextInt();
boolean found = false;
int index =0; // point to 0th first, index is whatever we are pointing to, like a reference
while(index < rows && !found) // not found
{
if(List4[index] == key)
found = true;
else
index++; // go to next number
}
if(found)
System.out.println(key + " is found at position " + index);
else
System.out.println(key + " is not found");
}
// method to print 1D array
public void print1DArray(int[ ] list)
{
for(int i=0; i<list.length; i++)
{
System.out.print(list[i] + " ");
}
System.out.println();
}
// method to print 2D array
public void print2DArray(int[ ][ ] list)
{
for(int i=0; i < list.length; i++)
{
for(int j=0; j < list[i].length; j++)
{
System.out.print(list[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
// EXTRA - write a method to generate a random number for the postion n for #4
public int generateRandomPosition(int[ ] List1)
{
Random rand = new Random();
int randomIndex = rand.nextInt(List1.length);
return randomIndex;
}
}