Reputation: 3
I am trying to find the average price and mileage for the vehicles in the code. I am using pointers to run the values through a function, however, the outputs of the functions are not correct.
#include <stdio.h>
#include <string.h>
#define SIZE 4
typedef struct {
int year;
char model[11];
} Model;
typedef struct {
float price;
} Price;
typedef struct {
int miles;
} Miles;
typedef struct {
Model year, model;
Price price;
Miles miles;
struct Car *next;
} Car;
float averagePrice(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car* ptr_car4);
int averageMiles(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car *ptr_car4);
int main(void) {
Car f1, im, f2, g;
int i, cnt, avgmile;
float avgprice;
f1.year.year = 56;
strcpy(f1.model.model, "Ford");
f1.price.price = 500.00;
f1.miles.miles = 23000;
im.year.year = 64;
strcpy(im.model.model, "Impala");
im.price.price = 1800.00;
im.miles.miles = 12000;
f2.year.year = 57;
strcpy(f2.model.model, "Ford");
f2.price.price = 1400.00;
f2.miles.miles = 22000;
g.year.year = 65;
strcpy(g.model.model, "Galaxy");
g.price.price = 2600.00;
g.miles.miles = 48000;
avgprice = averagePrice(&f1, &im, &f2, &g);
avgmile = averageMiles(&f1, &im, &f2, &g);
printf("The average price of the vehicle is: %d.\n", avgprice);
printf("The average miles of the vehicle is: %d.\n", avgmile);
getchar();
return 0;
}
float averagePrice(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car *ptr_car4) {
float total;
total = ptr_car->price.price;
total =+ ptr_car2->price.price;
total =+ ptr_car3->price.price;
total =+ ptr_car4->price.price;
total = total / SIZE;
return total;
}
int averageMiles(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car* ptr_car4) {
float total;
total = ptr_car->miles.miles;
total =+ ptr_car2->miles.miles;
total =+ ptr_car3->miles.miles;
total =+ ptr_car4->miles.miles;
total = total / SIZE;
return total;
}
I know I can not add pointer directly together so I added them to a variable to store the value. However, it appears to not store the values. I get 0 for the value of avgprice and 12000 for the value of avgmiles.
Upvotes: 0
Views: 62
Reputation: 557
You've made three mistakes here:
=+
instead of +=
, thus re-assigning the value each time instead of adding it.%d
(decimal) printf specifier, instead of the float specifier : %f
.avgmile
as being an int
, when it is in fact a float
.#include <stdio.h>
#include <string.h>
#define SIZE 4
typedef struct {
int year;
char model[11];
} Model;
typedef struct {
float price;
} Price;
typedef struct {
int miles;
} Miles;
typedef struct {
Model year, model;
Price price;
Miles miles;
struct Car *next;
} Car;
float averagePrice(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car* ptr_car4);
int averageMiles(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car *ptr_car4);
int main(void) {
Car f1, im, f2, g;
int i, cnt;
float avgprice, avgmile;
f1.year.year = 56;
strcpy(f1.model.model, "Ford");
f1.price.price = 500.00;
f1.miles.miles = 23000;
im.year.year = 64;
strcpy(im.model.model, "Impala");
im.price.price = 1800.00;
im.miles.miles = 12000;
f2.year.year = 57;
strcpy(f2.model.model, "Ford");
f2.price.price = 1400.00;
f2.miles.miles = 22000;
g.year.year = 65;
strcpy(g.model.model, "Galaxy");
g.price.price = 2600.00;
g.miles.miles = 48000;
avgprice = averagePrice(&f1, &im, &f2, &g);
avgmile = averageMiles(&f1, &im, &f2, &g);
printf("The average price of the vehicle is: %f.\n", avgprice);
printf("The average miles of the vehicle is: %f.\n", avgmile);
getchar();
return 0;
}
float averagePrice(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car *ptr_car4) {
float total;
total = ptr_car->price.price;
total += ptr_car2->price.price;
total += ptr_car3->price.price;
total += ptr_car4->price.price;
total = total / SIZE;
return total;
}
int averageMiles(Car *ptr_car, Car *ptr_car2, Car *ptr_car3, Car* ptr_car4) {
float total;
total = ptr_car->miles.miles;
total += ptr_car2->miles.miles;
total += ptr_car3->miles.miles;
total += ptr_car4->miles.miles;
total = total / SIZE;
return total;
}
Upvotes: 3