Commit ce28b0b4 by likorn

homework's first working version

parent 15b35c98
Showing with 87 additions and 0 deletions
#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\tOld price\n");
for(int i = 0; i < arraySize; i++) {
printf("%12s \t%.1f \n", *brands, *prices);
brands++;
prices++;
}
} else {
printf("\n TV Brand\tNew 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment