0

How can we return an array from a function i have no idea how to do this??? the are three cars and each is parked in a parking area for maximum 24 hours we have to find the cost for each car by making a function which wil evaluate the cost..??

#include <stdio.h>
#include <math.h>
#include <conio.h>
int calculateCharges(float hours[]);

int main() {
float hours[3];
int i;
for (i = 0; i <= 2; i++) {
    printf("Enter the hours you parked for car : %d\n", i + 1);     
    scanf_s("%f", &hours[i]);
}
hours[i] = calculateCharges(hours[]);

printf("%-10s%-10s%-10s\n", "Cars", "Hours", "Charge");
for (i = 0;i <= 2;i++) {
    printf("%-10d%-10.2f%-10.2f\n", i + 1, hours[i], calculateCharges(hours));
}

_getch();
return 0;
}

int calculateCharges(float hours[]) {

float cost[3];
int i;
for (i = 0; i <= 2; i++) {

    if (hours[i] <= 3) {          //if car parked for 3 or less hours it cost 2$
        cost[i] = 2;
    }
    else if (hours[i] > 3 && hours[i] < 24) { //if car parked for more than 3 or less then 24 hours it cost 0.5$for each extra hour$
        cost[i] = 2 + ((hours[i] - 3) / 2);
    }
    else if (hours[i] == 24) {   //if hours = 24 hours 10$
        cost[i] = 10;
    }
    else {
        cost[i] = 0;                //else its an error value zero cost
    }

    return cost[i];
}

}

M Ahmad Rana
  • 33
  • 1
  • 9
  • allocate using `malloc()` and return the base address. – Haris Nov 16 '15 at 18:20
  • Or allocate an array before you call the function, and pass in the address of the "output" array to that function. This way you can write the output to whichever array the caller wants the output in. – turbulencetoo Nov 16 '15 at 18:26
  • 2
    What do you need to return an array for in your example? The charges are independent of other cars; the function takes the parking time as input and returns a charge. I think your program makes a simple ting too complicated. – M Oehm Nov 16 '15 at 18:29

2 Answers2

0

You could pass in another array in which you return the cost, instead of making it a local array. Your method would look something like this:

int calculateCharges(float hours[], float costs[], int num) {
    ...
    for(i=0;i<num;i++) {
        ...
        costs[i] = 2;
kcraigie
  • 1,252
  • 6
  • 13
0

Functions cannot return arrays, but they can return pointers, and they can modify caller-visible variables via pointer arguments. If you want a function to create an array and provide it to the caller, then that function would need to allocate the array dynamically (via malloc() or calloc()) and then use one of the methods I named to return a pointer to the first element of that array.

But as commenters also remarked, none of that appears to be needed for the problem you presented. As far as I can tell, you just need a function that computes one cost for one car. You can call such a function from inside a loop (even the same loop in which you read the input) to calculate the cost for each car, which the caller can then handle however is appropriate.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157