1. ## assignment help

I'm pretty new to C programming and have just started a C programming module on my uni course and I am looking for some guidance on an assignment I have recently been set.

The specification is as follows:

You are required to write a C program suitable for aiding the calculation of travel expenses by the finance department of a small company. Your application is to be produced in simple form (Part 1) and then modified (Part 2) once examination and approval by your tutor is given.
Requirements for part 1
Your application should allow the population of a chart containing the distances in miles (or kilometers) between points. The chart should cater for 10 separate points. On completion of the entry of the distances the application should display a menu from which options can be selected. These options are:
1. The user should be able to display the distance between two valid points (e.g. point 1 and point 6) input by the user
2. The user should be able to calculate the cost of a journey between two valid points e.g. point 1 and point 6 at a predefined rate of 40 pence per mile. The output should be displayed in a suitable format.
3. The user should be able to display to the screen the complete mileage chart in a format similar to the one below( see fig 1).

I am just looking for some general pointers in the right direction if possible as I am struggling starting it.

Thanks for looking.

2. Make a starting attempt and work from there.
Code:
```#include <stdio.h>

int main(void)
{
return 0;
}```

3. Hmmm. Well, why don't you start out with the code you have and ask questions about code related?

4. don't think this makes much sense but this is what i have so far:

Code:
```#include <stdio.h>

#define COST 40
#define ROWS 10
#define COLS 10

void dispDistance(void);
void journeyCost(void);
void dispChart(void);
int array[ROWS][COLS];

int main(void)
{
int choice;

while(choice !='q')
{
switch()
{
case '1':/*display distance*/
dispDistance();
break;
case '2':/*calculate cost of journey*/
journeyCost();
break;
case '3':/*display the chart*/
dispChart();
break;
default:
}/*end switch*/

}/*end while*/

return 0;
}

{
Puts("1) Display distance between two places");
Puts("2) Calculate the cost of a journey");
Puts("3) Display the full mileage chart");
Puts("q) Quit\n");

return choice;

}

void dispDistance()
{
int x,y;

printf("Enter starting point: ");
scanf("%d",&x);

printf("Enter end point: ");
scanf("%d",&y);

if(x>ROWS-1 || x<COLS+1 || y>ROWS-1 || y<COLS+1)
{
printf("The distance between point x and point y is: %d \n", array[x-1][y-1]);
return array[x-1][y-1];
}
else
{
printf("Invalid entry");

}/*end if*/

return 0;

}/*end display distance*/

void journeyCost()
{
float cost;
cost = dist * 0.4;
printf("Cost of the journey is: %f\n", cost);

}/*end journey cost*/

void dispChart()
{
int x,y;
for(x=0;x<ROWS;x++)
{
printf("\n");

for(y=0;y<COLS;y++)
{
printf("\t%d", array[x][y]);
}/*end for*/
}/*end for*/

}/*end dispay chart*/```
any help would be much appreciated.

Thanks

5. I've taken a look at this. Honestly, I think if you tried to compile it you would find a lot of your problems.

You use Puts() instead of puts() in several places.
You have a prototype for int main().
You never define or initialize dist in journeyCost().
You don't have a value that you're switching on in main().

6. Originally Posted by mb86
don't think this makes much sense but this is what i have so far:

Code:
```#include <stdio.h>

#define COST 40
#define ROWS 10
#define COLS 10

void dispDistance(void);
void journeyCost(void);
void dispChart(void);
int array[ROWS][COLS]; //is this a global variable declaration?

int main(void)
{
int choice;

while(choice !='q')
{
switch()
{
case '1':/*display distance*/
dispDistance();
break;
case '2':/*calculate cost of journey*/
journeyCost();
break;
case '3':/*display the chart*/
dispChart();
break;
default:
}/*end switch*/

}/*end while*/

return 0;
}

{
Puts("1) Display distance between two places");
Puts("2) Calculate the cost of a journey");
Puts("3) Display the full mileage chart");
Puts("q) Quit\n");

return choice;

}

void dispDistance()
{
int x,y;

printf("Enter starting point: ");
scanf("%d",&x);

printf("Enter end point: ");
scanf("%d",&y);

if(x>ROWS-1 || x<COLS+1 || y>ROWS-1 || y<COLS+1)
{
printf("The distance between point x and point y is: %d \n", array[x-1][y-1]);
return array[x-1][y-1];
}
else
{
printf("Invalid entry\n");

}/*end if*/

return 0;

}/*end display distance*/

void journeyCost()
{
float cost;
cost = dist * 0.4;
printf("Cost of the journey is: %f\n", cost);

}/*end journey cost*/

void dispChart()
{
int x,y;
for(x=0;x<ROWS;x++)
{
printf("\n");

for(y=0;y<COLS;y++)
{
printf("\t%d", array[x][y]);
}/*end for*/
}/*end for*/

}/*end dispay chart*/```
any help would be much appreciated.

Thanks
I didn't look at the logic behind the code, I just look for any obvious errors.

7. Originally Posted by mb86
don't think this makes much sense but this is what i have so far:

Code:
```#include <stdio.h>

#define COST 40
#define ROWS 10
#define COLS 10

int menu(void); /* You shouldn't have a prototype for main */
void dispDistance(void);
void journeyCost(void);
void dispChart(void);
int array[ROWS][COLS]; /* If you can, avoid global variables */

int main(void)
{
int choice;

while(choice !='q') /* choice is uninitialized, so it can be any value.
Yes, even 'q', so your entire loop is skipped. Undefined behavior. */
{
switch() /* Missing switch expression here */
{
case '1':/*display distance*/
dispDistance();
break;
case '2':/*calculate cost of journey*/
journeyCost();
break;
case '3':/*display the chart*/
dispChart();
break;
default:
}/*end switch*/

}/*end while*/

return 0;
}

{
/* No such function as Puts. C is CaSe SeNsItIvE */
Puts("1) Display distance between two places");
Puts("2) Calculate the cost of a journey");
Puts("3) Display the full mileage chart");
Puts("q) Quit\n");

return choice;

}

void dispDistance() /* Function is missing void in argument list.
Other functions have it, so why not this one? */
{
int x,y;

printf("Enter starting point: ");
scanf("%d",&x);

printf("Enter end point: ");
scanf("%d",&y);

if(x>ROWS-1 || x<COLS+1 || y>ROWS-1 || y<COLS+1)
{
printf("The distance between point x and point y is: %d \n", array[x-1][y-1]);
return array[x-1][y-1]; /* Function returns void, so you can't return anything */
}
else
{
printf("Invalid entry");

}/*end if*/

return 0;

}/*end display distance*/

void journeyCost() /* Another missing void */
{
float cost;
cost = dist * 0.4;
printf("Cost of the journey is: %f\n", cost);

}/*end journey cost*/

void dispChart() /* Another missing void */
{
int x,y;
for(x=0;x<ROWS;x++)
{
printf("\n");

for(y=0;y<COLS;y++)
{
printf("\t%d", array[x][y]);
}/*end for*/
}/*end for*/

}/*end dispay chart*/```
any help would be much appreciated.

Thanks
Some corrections in red.

8. Hi guys, as you can see from the posts above this is the code i have at the minute.

Code:
```#include <stdio.h>

#define COST 40
#define ROWS 10
#define COLS 10

void dispDistance(void);
void journeyCost(void);
void dispChart(void);
int array[5][5];

int main(void)
{
int choice;
while(choice !='q')
{
switch(choice)
{
case '1':/*display distance*/
dispDistance();
break;
case '2':/*calculate cost of journey*/
journeyCost();
break;
case '3':/*display the chart*/
dispChart();
break;
default:
}/*end switch*/
}/*end while*/

return 0;
}

{
puts("1) Display distance between two places");
puts("2) Calculate the cost of a journey");
puts("3) Display the full mileage chart");
puts("q) Quit\n");

return choice;

}

void dispDistance(void)
{
int x,y;

printf("Enter value x: ");
scanf("%d",&x);

printf("Enter value y: ");
scanf("%d",&y);

if(x>ROWS-1 || x<COLS+1 || y>COLS-1 || y<COLS+1)
{
printf("The distance between point x and point y is: %d \n", array[x-1][y-1]);
return array[x-1][y-1];
}
else
{
printf("Invalid entry");

}/*end if*/

return 0;

}/*end display distance*/

void journeyCost(void)
{
float cost;
cost = dist * 0.4;
printf("Cost of the journey is: %f\n", cost);

}/*end journey cost*/

void dispChart()
{
int x,y;
for(x=0;x<ROWS;x++)
{
printf("\n");

for(y=0;y<COLS;y++)
{
printf("\t%d", array[x][y]);
}/*end for*/
}/*end for*/

}/*end dispay chart*/```
This code is supposed to:

1) Initialise: Read data from [the keyboard]
a) application should allow the population of a chart containing the distances in miles between 10 seperate points. This is entered by the user
a) DispChart: Display to the screen a complete mileage chart of the distances of the 10 seperate points entered by the user in grifd form
b) CalcDistance: Allow input of a Journey between two points and display the distance
c) Totalcost: Allow input of two points and calculate the cost of the journey at 40ppm
d) Exit: to exit the program

As you can probably see the program doesn't do this at the moment and i'm a bit stuck so any help would be much appreciated.

Thanks.

9. You've made a big mistake with this generalized "doesn't do this" approach.

Here's how it works:

1) You TELL us the SPECIFIC problem you're having.

2) We make advice based on those SPECIFIC (and perhaps a few general one's too), problem.

Posting your homework requirements and saying the program doesn't meet it's requirements, doesn't really show you're engaged in this. It shows that you're dumping this here, in the hope somebody will just make it work for you.

I doubt that will work.

If you're stuck, what are you stuck on? What is the output of your program that's wrong? Be specific about the problem, the code block or function that's failing, etc.