Store details of walks

Store Details of Walks

 Part 1

————————

The application will allow the user add details of walks and display details of walks.

  1. add walks
  2. list all walks
  3. save walks to a binary file (reload on start-up)
  4. List walks in  20 km radius of location entered by the user

Define a structure walkDetails that contains  a  name, county, level ,distance, heightGain, location.  Location should be in a sub structure containing longitude and latitude.

Use a contiguous block of memory that is allocated at the start using malloc. You can access this using array notation but we want to use pointer notation so that you get practice  with pointers.

Part 2

———————

  1. The program should expect one command line arguments

This contains the name of the file to load/save. This is a binary file.

It the file does not exist it should be created and the walk details are written to it on exit.

  1. The number of spaces allocated grows if required. If all 5 spaces are used another 5 spaces are allocated using re-allocated.

Part 3

——————-

  1. Write a deleteAWalk. When at item is deleted all the pointers should be compacted.
  1. Write a modifyWalk. The user can change the name of a walk.  Note the only details for a walk in this program in the walk name.

Part 4

——————-

Use an assembly method that adds two numbers to calculate total length of all walks. 

#include <stdio.h>

#include <stdlib.h>

#define MAX_NAME_LEN 50

struct walkDetails**  createAListOfWalks(int  maxNoOfWalks);

struct  walkDetails  {

char name[MAX_NAME_LEN + 1];

char county[100];

int level;

double distance;

double heightGain;

char location[100]

} ;

int main()

{

printf(“Hello world!\n”);

const int MAX = 5;

struct walkDetails* * ppWalksList = NULL; // Creates a list of pointers

int numberOfWalks = 0;

//    ppWalksList = createAListOfWalks(MAX);

ppWalksList = malloc( sizeof(struct walkDetails * ) * 5);

printf(“the address of the list of poinsters is %p\n”, ppWalksList);

printf(“this is a list of 5 address that have not been set\n”, ppWalksList);

//  to set teh first address to NULL we would

int i = 0;

while ( i < 5)

{

//   *ppWalksList = NULL;

// ppWalksList++;

i++;

}

addWalk(ppWalksList, &numberOfWalks, MAX);

addWalk(ppWalksList, &numberOfWalks, MAX);

addWalk(ppWalksList, &numberOfWalks, MAX);

addWalk(ppWalksList, &numberOfWalks, MAX);

if (ppWalksList !=NULL)

{

showWalks(ppWalksList, numberOfWalks);

}

//  implement delete walk

//  implement modify walk

printf(“end”);

getchar();

// free ( ppWalksList);

return 0;

}

struct walkDetails**  createAListOfWalks(int  maxNoOfWalks)

{

struct walkDetails* * ppWalksList = NULL;

ppWalksList = malloc( sizeof(struct walkDetails * ) * maxNoOfWalks);

printf(“the address of the list of poinsters is %p\n”, ppWalksList);

return ppWalksList;

}

void addWalk(struct walkDetails** pWalksList, int* pnumberOfWalks, int max)

{

if (*pnumberOfWalks < max)

{

//  allocate  space using malloc for one item

//printf(“—>%p”, pWalksList);

struct walkDetails** pNext = pWalksList + * pnumberOfWalks;

*pNext = malloc(sizeof (struct walkDetails));

// printf(“Added block:—>%p\n”, *pNext);

char walkName[MAX_NAME_LEN + 1];

printf(“Enter the walk name: “);

scanf(“%s”, walkName);

strncpy((*pNext)->name, walkName, MAX_NAME_LEN);

(*pnumberOfWalks)++;

}

}

void showWalks(struct walkDetails* * ppWalksList, int numItems)

{

struct walkDetails** pNext = pFirst;

int i = 0;

while ( i < numItems )

{

//printf(“SW: %p\n”, pNext);

//printf(“SW: %p\n”, *pNext);

printf(“SW: %s \n”, (*pNext)->name);

pNext++;

i++;

}

}

Solution 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <errno.h>

#define MAX_NAME_LEN 50

#define MAX 5

struct location {

double longitude;

double latitude;

};

struct  walkDetails  {

char name[MAX_NAME_LEN + 1];

char county[100];

int level;

double distance;

double heightGain;

struct location loc;

} ;

struct walkDetails** createAListOfWalks(int  maxNoOfWalks);

struct walkDetails** addWalk(struct walkDetails** pWalksList, int* pnumberOfWalks, int *maxNoOfWalks);

struct walkDetails** loadWalks(const char *fname, struct walkDetails** pWalksList, int* pnumberOfWalks, int *maxNoOfWalks);

void saveWalks(const char *fname, struct walkDetails** pWalksList, int pnumberOfWalks);

void deleteWalk(struct walkDetails** pWalksList, int *numItems);

void showWalks(struct walkDetails**  pWalksList, int numItems);

void modifyWalk(struct walkDetails** pWalksList, int numItems);

int main(int argc, char **argv)

{

if (argc < 2)

{

printf(“usage: %s filename\n”, argv[0]);

exit(1);

}

struct walkDetails* *ppWalksList = NULL;

int numberOfWalks = 0;

int walksListSize = MAX;

int isRunning = 1;

char choice;

ppWalksList = createAListOfWalks(walksListSize);

ppWalksList = loadWalks(argv[1], ppWalksList, &numberOfWalks, &walksListSize);

while (isRunning) {

printf(“\n\n”);

printf(“1. Add a walk\n”);

printf(“2. Modify a walk\n”);

printf(“3. Delete a walk\n”);

printf(“4. List all walks\n”);

printf(“5. Quit\n”);

printf(“Enter your choice: “);

scanf(“%d”, &choice);

fflush(stdin);

switch (choice) {

case 1:

ppWalksList = addWalk(ppWalksList, &numberOfWalks, &walksListSize);

break;

case 2:

modifyWalk(ppWalksList, numberOfWalks);

break;

case 3:

deleteWalk(ppWalksList, &numberOfWalks);

break;

case 4:

showWalks(ppWalksList, numberOfWalks);

break;

case 5:

isRunning = 0;

break;

default:

printf(“Invalid menu option\n”);

break;

};

}

saveWalks(argv[1], ppWalksList, numberOfWalks);

printf(“end\n”);

getchar();

return 0;

}

struct walkDetails** createAListOfWalks(int  maxNoOfWalks)

{

struct walkDetails* *ppWalksList = NULL;

ppWalksList = malloc( sizeof(struct walkDetails *) * maxNoOfWalks);

return ppWalksList;

}

struct walkDetails** loadWalks(const char *fname, struct walkDetails** pWalksList, int *numberOfWalks, int *maxNoOfWalks)

{

FILE *fp;

fp = fopen(fname, “rb”);

if (fp != NULL) {

struct walkDetails** pNext = pWalksList;

struct walkDetails buffer;

while (fread(&buffer, sizeof(struct walkDetails), 1, fp)) {

if (*numberOfWalks == *maxNoOfWalks) {

*maxNoOfWalks = *maxNoOfWalks + MAX;

pWalksList = realloc(pWalksList, *maxNoOfWalks * sizeof(struct walkDetails *));

}

*pNext = malloc(sizeof (struct walkDetails));

**pNext = buffer;

printf(“%d: %s\n”, 1, (*pNext)->name);

pNext++;

(*numberOfWalks)++;

}

fclose(fp);

}

return pWalksList;

}

void saveWalks(const char *fname, struct walkDetails** pWalksList, int numItems)

{

FILE *fp;

fp = fopen(fname, “wb”);

struct walkDetails** pNext = pWalksList;

int i = 0;

while (i < numItems)

{

fwrite(*pNext, sizeof(struct walkDetails), 1, fp);

pNext++;

i++;

}

fclose(fp);

}

struct walkDetails** addWalk(struct walkDetails** pWalksList, int* pnumberOfWalks, int *maxNoOfWalks)

{

if (*pnumberOfWalks == *maxNoOfWalks)

{

*maxNoOfWalks = *maxNoOfWalks + MAX;

pWalksList = realloc(pWalksList, *maxNoOfWalks * sizeof(struct walkDetails *));

}

struct walkDetails** pNext = pWalksList + *pnumberOfWalks;

*pNext = malloc(sizeof (struct walkDetails));

char walkName[MAX_NAME_LEN + 1];

printf(“Enter the walk name: “);

scanf(“%s”, walkName);

strncpy((*pNext)->name, walkName, MAX_NAME_LEN);

char countyName[101];

printf(“Enter the county: “);

scanf(“%s”, countyName);

strncpy((*pNext)->county, countyName, 100);

int level;

printf(“Enter the level: “);

scanf(“%d”, &level);

(*pNext)->level = level;

double distance;

printf(“Enter the distance: “);

scanf(“%lf”, &distance);

(*pNext)->distance = distance;

double heightGain;

printf(“Enter the height gain: “);

scanf(“%lf”, &heightGain);

(*pNext)->heightGain = heightGain;

double longitude;

printf(“Enter the longitude: “);

scanf(“%lf”, &longitude);

((*pNext)->loc).longitude = longitude;

double latitude;

printf(“Enter the latitude: “);

scanf(“%lf”, &latitude);

((*pNext)->loc).latitude = latitude;

(*pnumberOfWalks)++;

return pWalksList;

}

void deleteWalk(struct walkDetails** pWalksList, int *numItems)

{

int ind;

printf(“Enter the walk index to delete: “);

scanf(“%d”, &ind);

fflush(stdin);

if (ind < 0 || ind >= *numItems) {

printf(“Walk index %d doesn’t exits\n”, ind);

}

else {

struct walkDetails** pNext = pWalksList;

int i = 0;

while (i < ind)

{

pNext++;

i++;

}

struct walkDetails** pPrev = pNext;

pNext++;

i++;

free(*pPrev);

while (i < *numItems)

{

*pPrev = *pNext;

pPrev = pNext;

pNext++;

i++;

}

(*numItems)–;

}

}

void modifyWalk(struct walkDetails** pWalksList, int numItems)

{

int ind;

printf(“Enter the walk index to modify: “);

scanf(“%d”, &ind);

fflush(stdin);

if (ind < 0 || ind >= numItems) {

printf(“Walk index %d doesn’t exits\n”, ind);

}

else {

char walkName[MAX_NAME_LEN + 1];

printf(“Enter the new walk name: “);

scanf(“%s”, walkName);

fflush(stdin);

struct walkDetails** pNext = pWalksList;

int i = 0;

while (i < numItems)

{

if (i == ind)

{

strncpy((*pNext)->name, walkName, MAX_NAME_LEN);

break;

}

pNext++;

i++;

}

}

}

void showWalks(struct walkDetails* *pWalksList, int numItems)

{

struct walkDetails** pNext = pWalksList;

double total = 0;

int i = 0;

while (i < numItems)

{

total = total + (*pNext)->distance;

printf(“Walk #%d\n”, i);

printf(“Name: %s\n”, (*pNext)->name);

printf(“County: %s\n”, (*pNext)->county);

printf(“Location: (%g, %g)\n”, ((*pNext)->loc).longitude, ((*pNext)->loc).latitude);

printf(“Level: %d\n”, (*pNext)->level);

printf(“Distance: %g\n”, (*pNext)->distance);

printf(“Height Gain: %g\n”, (*pNext)->heightGain);

printf(“\n”);

pNext++;

i++;

}

printf(“\n”);

printf(“Total walks length: %g\n”, total);

}

/*double sum(double val1, double val2) {*/

/*    double val3;*/

/*    __asm__ (  */

/*          mov eax, val1;*/

/*          mov ecx, val2;*/

/*          add eax, ecx;*/

/*    )*/

/*}*/