Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
Maths1
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
samuel.boulton
Maths1
Commits
bf9ffad1
Commit
bf9ffad1
authored
Dec 11, 2018
by
samuel.boulton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1 left
parent
c4fffa49
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
21 deletions
+59
-21
Program.java
src/Program.java
+59
-21
No files found.
src/Program.java
View file @
bf9ffad1
...
...
@@ -74,11 +74,10 @@ public class Program {
// DONE AND PASSED TEST
int
[]
resultsArray
=
new
int
[
binaryArray
.
length
];
// for loop to go around the binary array
for
(
int
i
=
0
;
i
<
binaryArray
.
length
;
i
++)
{
// use the 'ConvertASingleWordToInt' method to work out each character
// then add each character to the resultsArray
resultsArray
[
i
]
=
ConvertASingleWordToInt
(
binaryArray
[
i
]);
}
...
...
@@ -90,20 +89,19 @@ public class Program {
int
result
=
0
;
int
binCM
=
1
;
// making the characters from said 'someWord'
char
[]
binChar
=
someWord
.
toCharArray
();
// looping through the characters
for
(
int
i
=
0
;
i
<
binChar
.
length
;
i
++)
{
// making the program go through the characters backwards to get the correct result
int
binIndex
=
binChar
.
length
-
i
-
1
;
// if statement seeing if the character is a 1
if
(
binChar
[
binIndex
]
==
'1'
)
{
// if it is a 1 then do this calcualtion
result
=
result
+
binCM
;
}
// each time the loop goes round binCM doubles
binCM
=
binCM
*
2
;
}
...
...
@@ -115,11 +113,9 @@ public class Program {
public
static
int
[]
ConvertHexToInt
(
String
[]
hexArray
)
{
// DONE AND PASSED TEST
int
[]
resultsArray
=
new
int
[
hexArray
.
length
];
// for loop to go around the hex array
for
(
int
i
=
0
;
i
<
hexArray
.
length
;
i
++)
{
// use the 'ConvertHexToInt' method to work out each character
// then add each character to the resultsArray
resultsArray
[
i
]
=
ConvertHexToInt
(
hexArray
[
i
]);
}
...
...
@@ -133,12 +129,11 @@ public class Program {
{
int
result
=
0
;
int
hexCM
=
1
;
// making the characters from said 'hexWord'
char
[]
hexChar
=
hexWord
.
toCharArray
();
// looping through the characters
for
(
int
i
=
0
;
i
<
hexChar
.
length
;
i
++)
{
// if the character is a certain character then do this calculation
int
hexIndex
=
hexChar
.
length
-
i
-
1
;
if
(
hexChar
[
hexIndex
]
==
'1'
)
{
result
=
result
+
(
hexCM
*
1
);}
if
(
hexChar
[
hexIndex
]
==
'2'
)
{
result
=
result
+
(
hexCM
*
2
);}
...
...
@@ -577,40 +572,83 @@ public static int Range (int[] input) {
public
static
double
[]
GenerateRandomDistributionForSpecificDie
(
int
iterations
)
{
// DONE AND PASSED TEST
double
[]
resultsArray
=
new
double
[
5121
];
Random
rng
=
new
Random
(
1234
);
for
(
int
i
=
0
;
i
<
iterations
;
i
++)
{
int
numIGot
=
0
;
numIGot
=
RollASingle256SidedDie20Times
(
rng
);
resultsArray
[
numIGot
]
=
resultsArray
[
numIGot
]
+
1
;
}
for
(
int
j
=
0
;
j
<
resultsArray
.
length
;
j
++)
{
resultsArray
[
j
]
=
resultsArray
[
j
]
/
iterations
;
}
return
resultsArray
;
}
public
static
int
RollASingle256SidedDie20Times
(
Random
rand
)
{
int
result
=
0
;
for
(
int
i
=
0
;
i
<
20
;
i
++)
{
int
number
=
rand
.
nextInt
(
256
)
+
1
;
result
+=
number
;
}
return
result
;
}
public
static
double
[]
GenerateRandomDistribution
(
int
dieSides
,
int
rolls
,
int
iterations
)
{
// DONE AND PASSED TEST
double
[]
resultsArray
=
new
double
[(
dieSides
*
rolls
)+
1
];
Random
rng
=
new
Random
(
1234
);
for
(
int
i
=
0
;
i
<
iterations
;
i
++)
{
int
numIGot
=
0
;
numIGot
=
RollADieNTimes
(
rng
,
dieSides
,
rolls
);
resultsArray
[
numIGot
]
=
resultsArray
[
numIGot
]
+
1
;
}
for
(
int
j
=
0
;
j
<
resultsArray
.
length
;
j
++)
{
resultsArray
[
j
]
=
resultsArray
[
j
]
/
iterations
;
}
return
resultsArray
;
}
public
static
int
RollADieNTimes
(
Random
rand
,
int
dieSides
,
int
rolls
)
{
int
result
=
0
;
for
(
int
i
=
0
;
i
<
rolls
;
i
++)
{
int
number
=
rand
.
nextInt
(
dieSides
)
+
1
;
result
+=
number
;
}
return
result
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment