Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/* Write a main function that dynamically allocates space for 100 customer point

ID: 3625181 • Letter: #

Question

/* Write a main function that dynamically allocates space for 100 customer pointers.
    Keep track of the effective size of the array. Use the above function
    to add some customers to the array*/

this is all i have so far...please show me how to do "the function" part

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct{
    char name[40];
    int customerID;
    double amountOwing;
} CUSTOMER;

CUSTOMER* makeACustomer(){
    CUSTOMER* results;
    results = malloc(sizeof(CUSTOMER));

    return results;
}

void getCustomerInfo(CUSTOMER* customer){
    printf("enter the customer name: ");
    scanf("%s", customer->name);
    printf(" Enter the customer ID ");
    scanf("%i", &customer->customerID);
    printf(" Enter amount owing: ");
    scanf("%lf", &customer->amountOwing);

    printf(" ");

}

main(){

system("puase");
}

Explanation / Answer

To dynamically allocate space for 100 CUSTOMER*'s you will want to call malloc(100 * sizeof(CUSTOMER*));

This will allow you to have enough space to hold 100 CUSTOMER*'s and index into the dynamically allocated space as if it was an array.

A nice thing to do for testing purposes is to #define something that lets you dynamically change the number of customers you want to have at any point and time, such as this.

#define NUMBER_OF_CUSTOMERS 100

This will let us use just 100 customers for now.

Then going into our main, because we are in C we want to first declare our variables

/* The variables we will be using in this program, namely the array to hold all the customers in, and the variable we will be using for counting over our loops */
CUSTOMER** customerArray;
unsigned int loopcount;

Then we will move onto dynamically allocating the size of the array to hold the customers, it will be able to hold the number of customers we set in the above #define

customerArray = malloc(NUMBER_OF_CUSTOMERS * sizeof(CUSTOMER*));

This line basically breaks down into malloc(size of(CUSTOMER*)) but we are multiplying it by the amount of customers we need. An example of how this works is, if I wanted 1 char (1 byte) i would malloc(sizeof(char)); but if I want 100 chars (100 bytes) it would be the same as just saying i want 100 * 1 char, thus malloc(100 * sizeof(char));. This is how this line works.

After that, you want to initialize the data for the array, so we can loop through the newly allocated array and call the function's provided on each pointer.

for (loopcount = 0; loopcount < NUMBER_OF_CUSTOMERS; ++loopcount)

Here is where we call the functions on the dynamically allocated array, using it as if it was just any other array.


customerArray[loopcount] = makeACustomer();
getCustomerInfo(customerArray[loopcount]);

And finally at the end, DO NOT forget to call free on the data you malloc, so again we will loop through our data

for (loopcount = 0; loopcount < NUMBER_OF_CUSTOMERS; ++loopcount)

and call free on each element inside our dynamically allocated array.

free(customerArray[loopcount]);

And then at the end, finally call free on the array that we dynamically allocated.

free(customerArray);