# Challenge

Discussion in 'C' started by Ken, Apr 9, 2008.

1. ### KenNew Member

Joined:
Apr 9, 2008
Messages:
5
0
Trophy Points:
0
The program should do the following:

1) dispaly mileage chart from text file

2) create file to put the array towns and distances in a text
file

3) Input started location and finish location i.e. Bath to london

4) Display other mileage chart from array

5) Display total cost of journey at 40 ppm up to 100 miles and
30ppm for mileas in excess of this

6) Exit prgram and display a goodbye message

Here is the program code below

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "node.h"

struct Chart
{
char Town[10][10];
int Distance[10][10];
};

Chart chart=
{
{ "London", "Bath", "Cardiff", "Carlisle", "Durham", "Exeter", "Leeds", "Norwich", "Truro", "York" },
{ 0, 23, 12, 89, 456, 123, 46, 732, 345, 123 },
{ 23, 0, 46, 234, 123, 46, 89, 234, 567, 90 },
{ 12, 46, 0, 767, 456, 46, 234, 123, 732, 35 },
{ 89, 234, 767, 0, 732, 32, 48, 67, 98, 100 },
{ 456, 123, 456, 732, 0, 234, 46, 89, 89, 732 },
{ 123, 46, 46, 32, 234, 0, 123, 46, 123, 234 },
{ 46, 89, 234, 48, 46, 123, 0, 46, 89, 19 },
{ 732, 234, 123, 67, 89, 46, 46, 0, 123, 732 },
{ 345, 567, 732, 98, 89, 123, 89, 123, 0, 78 },
{ 123, 90, 35, 100, 732, 234, 19, 732, 78, 0 },
};

int ReadChart(const char Towns[],const Chart* chart )
{
FILE *fp;

fp = fopen("Towns.txt", "r");
if (fp == NULL)

return 0;
else
{
size_t res = fread(chart, sizeof(Chart), 1, fp);
fclose(fp);

return res;
}
}

int WriteChart(const char Towns[], const Chart* chart)
{
FILE *fp;

fp = fopen("Towns.txt", "w");
if (fp == NULL)
return 0;
else
{
size_t res = fwrite(chart, sizeof(Chart), 1, fp);
fclose(fp);
return res;
}
}

int main(void)
{
char Distance;
int  valid_input;
int row,col;
char des1,des2,des3;
int menu;             /* Holds all chices open to the user */
double total = 0.00;           /* the result of the calculation */

printf("\n\nWhat would you like to do?\n\n");    /* WRITE instructions */
printf("\t1 = Display Mileage chart from text file\n");
printf("\t2 = Input of a Journey between two points via up to 3 intermediate stops\n");
printf("\t3 = Display total cost of journey at 40 ppm up to 100 miles and 30ppm for mileas in excess of this\n");
printf("\t4 = Exit program\n");

valid_input = 0;
while( valid_input == 0 )
{
printf("\t Please enter first destination point\n");
printf("%s",des1);
printf("\t Please enter second destination point\n");
printtf("%s",des2);
printf("\t Please enter thrid destination point\n");
printf("%s",des3);

else
{
printf("\tError: Invalid \n");
}
}
{
case 1:
FILE *infile;
infile = fopen("Towns.dat", "r");       // using relative path name of file
if (infile == NULL)
{
printf("Unable to open file.");
}

for(row=1; row<11;row++) // reads in the data into the array in 2 loops
{
for(col=1;col<11;col++)
{
fscanf(infile,"%d", chart[row-1][col-1]);

}
}

for(row=0; row<10;row++)

{
printf("\n\n");
for(col=0;col<10;col++)
{
printf("%-2d ",chart[row][col]);
}

}
printf("\n\n continue?");

break;

case 2:
prinf("\t Please enter first destination point\n");
printf("%s",des1);
prinf("\t Please enter second destination point\n");
printf("%s",des2);
prinf("\t Please enter thrid destination point\n");
printf("%s",des3);

break;

case 3:
if (Distance => 100)
{
total= 0.40*100;
}

if (Distance < 100)
{
total= 0.30*100;
}
break;

case 4:
printf("Thank you for using my program");
break;

default:
printf("Invalid option selected\n");
}

int search (node *root, int key)
{
if ( root != NULL)
{
if ( key == root->data )
return 1;
else if ( key < root->data )
return search ( root->left, key );
else  if ( key > root->data )
return search ( root->right, key );
}

return 0;
}
}
```

Code:
```/* utilize an ifndef/define mechanism, so nodes will be define exactly once */#ifndef _NODE_H#define _NODE_H /* Maximum length of names */#define strMax 90 /* Define the node structure itself */struct node{   char data [strMax];  struct node * left;  struct node * right;}; #endif/* utilize an ifndef/define mechanism, so nodes will be define exactly once */
#ifndef _NODE_H
#define _NODE_H

/* Maximum length of names */
#define strMax 90

/* Define the node structure itself */
struct node
{
char data [strMax];
struct node * left;
struct node * right;
};

#endif
```
Here is the text file

Code:
```          London   Bath   Cardiff  Carlisle  Durham  Exeter  Leeds   Norwich  Truro    York
London      0     114.66  151.22   309.14    269.11  195.90  195.33  117.87   284.74  209.43
Bath      115.16    0      55.95   288.37    293.02   86.41  219.24  235.72   175.25  233.34
Cardiff   151.61   56.17    0      303.39    308.04  109.81  234.26  272.17   198.65  248.36
Carlisle  309.35  288.23  303.39     0        73.73  348.15  117.24  280.36   436.99  116.36
Durham    269.78  293.42  308.58    73.75      0     353.34   82.10  239.13   442.17   75.13
Exeter    196.04   85.71  109.14   347.90    352.56    0     278.78  316.61    87.49  292.88
Leeds     196.11  219.75  234.91   117.17     80.32  279.67    0     171.06   368.50   24.76
Norwich   118.49  235.75  272.31   283.26    241.67  316.99  173.45    0      405.83  182.76
Truro     284.56  174.22  197.65   436.42    441.07   87.07  367.30  405.12     0     381.39
York      212.84  236.48  251.64   116.53     74.93  296.40   24.21  180.26   385.23    0
```

I Have got this to work in Miracle C but it has to be able to work in unix. Why dosent my program work in unix

The problem that i have found it will not work in unix and has problems with the code but i have tried using alternavtive methord but to no success.

If anyone could a help me find a solution to this that would be super and i would be very grateful.

2. ### SpOonWiZaRdKnow what you can do.

Joined:
May 30, 2007
Messages:
747
8
Trophy Points:
0
Occupation:
Network Engineer/Programmer
Location:
South Africa
Go ask it in the programming section, but welcome to the site!