Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
kurs_java_alx_20240321
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
Patryk Czarnik
kurs_java_alx_20240321
Commits
a37f8b4b
Commit
a37f8b4b
authored
Apr 10, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
funkcje na tablicach
parent
8bf646d1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
89 additions
and
0 deletions
+89
-0
FunkcjeNaTablicach.java
src/p09_funkcje/FunkcjeNaTablicach.java
+89
-0
No files found.
src/p09_funkcje/FunkcjeNaTablicach.java
0 → 100644
View file @
a37f8b4b
package
p09_funkcje
;
public
class
FunkcjeNaTablicach
{
static
int
suma
(
int
[]
t
)
{
int
suma
=
0
;
for
(
int
i
=
0
;
i
<
t
.
length
;
i
++)
{
suma
+=
t
[
i
];
}
return
suma
;
}
static
int
suma_v2
(
int
[]
t
)
{
int
suma
=
0
;
for
(
int
x
:
t
)
{
suma
+=
x
;
}
return
suma
;
}
// dodaj i przetestuj funkcje:
// - suma_parzystych
static
int
suma_parzystych
(
int
[]
t
)
{
int
suma
=
0
;
for
(
int
x
:
t
)
{
if
(
x
%
2
==
0
)
{
suma
+=
x
;
}
}
return
suma
;
}
// - ile_parzystych
static
int
ile_parzystych
(
int
[]
t
)
{
int
ile
=
0
;
for
(
int
x
:
t
)
{
if
(
x
%
2
==
0
)
{
ile
+=
1
;
}
}
return
ile
;
}
// - srednia_parzystych
// dwie strategie:
// 1) napiszę od nowa funkcję, w której przeglądam tablicę i wszystko liczę
// zalety: większa wydajność (dane przeglądamy tylko raz), niezależność od innych definicji
static
double
srednia_parzystych_v1
(
int
[]
t
)
{
double
suma
=
0
;
int
ile
=
0
;
for
(
int
x
:
t
)
{
if
(
x
%
2
==
0
)
{
suma
+=
x
;
ile
++;
}
}
return
suma
/
ile
;
}
// 2) korzystamy z już zdefiniowanych funckji
// zalety: unikanie duplikacji kodu, lepsza struktura projektu, podział odpowiedzialności między metody itd - "dobre praktyki"
// DRY
static
double
srednia_parzystych_v2
(
int
[]
t
)
{
return
(
double
)
suma_parzystych
(
t
)
/
ile_parzystych
(
t
);
}
public
static
void
main
(
String
[]
args
)
{
// Tutaj zapiszemy przykładowe wywołania i wyświetlimy ich wyniki
int
[]
a
=
{
5
,
10
,
15
,
0
};
int
[]
b
=
{
3
,
5
,
7
,
9
,
11
,
13
};
int
[]
c
=
{
3
,
2
,
1
,
2
,
4
,
11
,
13
};
System
.
out
.
println
(
"suma a = "
+
suma
(
a
));
System
.
out
.
println
(
"suma_v2 a = "
+
suma_v2
(
a
));
System
.
out
.
println
(
"suma_p a = "
+
suma_parzystych
(
a
));
System
.
out
.
println
(
"suma_p b = "
+
suma_parzystych
(
b
));
System
.
out
.
println
(
" ile_p a = "
+
ile_parzystych
(
a
));
System
.
out
.
println
(
" ile_p b = "
+
ile_parzystych
(
b
));
System
.
out
.
println
(
"średnia_v1 a = "
+
srednia_parzystych_v1
(
a
));
System
.
out
.
println
(
"średnia_v1 b = "
+
srednia_parzystych_v1
(
b
));
System
.
out
.
println
(
"średnia_v1 c = "
+
srednia_parzystych_v1
(
c
));
System
.
out
.
println
(
"średnia_v2 a = "
+
srednia_parzystych_v2
(
a
));
System
.
out
.
println
(
"średnia_v2 b = "
+
srednia_parzystych_v2
(
b
));
System
.
out
.
println
(
"średnia_v2 c = "
+
srednia_parzystych_v2
(
c
));
}
}
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