Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
likorn
/
iax0583
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
ce28b0b4
authored
Dec 11, 2017
by
likorn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
homework's first working version
parent
15b35c98
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
87 additions
and
0 deletions
homework2/main2.c
homework2/report.docx
homework2/main2.c
0 → 100644
View file @
ce28b0b4
#include <stdio.h>
#include <stdlib.h>
char
**
generate_brands
(
int
);
float
*
set_prices
(
int
,
char
**
,
float
*
);
float
read_discount
();
void
display_data
(
char
**
,
float
*
,
float
*
,
int
,
int
);
float
*
calculate_prices
(
float
*
,
float
*
,
float
,
int
);
int
main
(
void
)
{
int
arraySize
=
8
;
char
**
brands
=
generate_brands
(
arraySize
);
float
*
prices
=
malloc
(
arraySize
*
sizeof
(
float
*
));
prices
=
set_prices
(
arraySize
,
brands
,
prices
);
float
discount
=
read_discount
();
float
*
new_prices
=
malloc
(
arraySize
*
sizeof
(
float
*
));
display_data
(
brands
,
prices
,
new_prices
,
arraySize
,
0
);
new_prices
=
calculate_prices
(
prices
,
new_prices
,
discount
,
arraySize
);
display_data
(
brands
,
prices
,
new_prices
,
arraySize
,
1
);
}
char
**
generate_brands
(
int
arraySize
)
{
char
**
brands
=
(
char
**
)
malloc
(
arraySize
*
sizeof
(
char
*
));
brands
[
0
]
=
"Samsung"
;
brands
[
1
]
=
"LG"
;
brands
[
2
]
=
"Panasonic"
;
brands
[
3
]
=
"Toshiba"
;
brands
[
4
]
=
"Philips"
;
brands
[
5
]
=
"Sharp"
;
brands
[
6
]
=
"Visio"
;
brands
[
7
]
=
"Mitsubishi"
;
return
brands
;
}
float
*
set_prices
(
int
arraySize
,
char
**
brands
,
float
*
prices
)
{
for
(
int
i
=
0
;
i
<
arraySize
;
i
++
)
{
printf
(
"Enter the price for %s
\n
"
,
*
brands
);
scanf
(
"%f"
,
prices
);
while
((
int
)
*
prices
<=
0
)
{
printf
(
"No, the price can be neither zero nor negative. Be realistic: "
);
scanf
(
"%f"
,
prices
);
}
brands
++
;
prices
++
;
}
return
(
float
*
)((
int
)
prices
-
arraySize
*
sizeof
(
float
*
));
}
float
read_discount
()
{
float
input
;
printf
(
"Enter the discount: "
);
scanf
(
"%f"
,
&
input
);
if
(
input
<
10
||
input
>
60
)
{
printf
(
"Invalid discount.
\n
"
);
return
read_discount
();
}
return
input
;
}
void
display_data
(
char
**
brands
,
float
*
prices
,
float
*
new_prices
,
int
arraySize
,
int
if_new
)
{
if
(
if_new
==
0
)
{
printf
(
"
\n
TV Brand
\t
Old price
\n
"
);
for
(
int
i
=
0
;
i
<
arraySize
;
i
++
)
{
printf
(
"%12s
\t
%.1f
\n
"
,
*
brands
,
*
prices
);
brands
++
;
prices
++
;
}
}
else
{
printf
(
"
\n
TV Brand
\t
New price
\n
"
);
for
(
int
i
=
0
;
i
<
arraySize
;
i
++
)
{
printf
(
"%12s
\t
%.1f
\n
"
,
*
brands
,
*
new_prices
);
brands
++
;
new_prices
++
;
}
}
}
float
*
calculate_prices
(
float
*
prices
,
float
*
new_prices
,
float
discount
,
int
arraySize
)
{
for
(
int
i
=
0
;
i
<
arraySize
;
i
++
)
{
*
new_prices
=
*
prices
*
(
1
-
(
discount
/
100
.
0
));
prices
++
;
new_prices
++
;
}
return
(
float
*
)((
int
)
new_prices
-
arraySize
*
sizeof
(
float
*
));
}
\ No newline at end of file
homework2/report.docx
0 → 100644
View file @
ce28b0b4
File added
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