Hi guys,
Again i have been given a partly done program to complete by my lecturer.. I have been sitting at my desk for the whole day and been stuck on one particular part.
the program takes in a number of drivers and the distances they travelled, it uses this info ( an array ) in various functions to show different calcuations.
my problem is not with the various functions needed but with creating the initial array. I'll freely admit to not fully understanding all of this and appreciate any help or pointers.. i already know no one can solve ot for me
so here is the shortened code im stuck on:
Code:
#include <stdio.h>
#include <stdlib.h>
main()
{
int menu(); /* function to select option*/
void input_distances(int distance_array[],int num);
void display_distances(int distance_array[],int num);
int input_num_of_drivers();
float* distances;
int i;
int num_drivers, num_of_bytes;
int option ;
do
{
option=menu();
switch (option)
{
case 0:
break;
case 1:
num_drivers=input_num_of_drivers( );
num_of_bytes=num_drivers*sizeof(float);
distances=malloc(num_of_bytes);
if ( num_drivers == NULL )
printf("Cannot Allocate Memory\n");
input_distances(distances,num_drivers);this is calling my function?
break;
case 2:
display_distances(distances,num_drivers);
break;
}
}
while (option!=0);
}
{
int opt;
printf ("Press 1 to enter number of drivers and distances\n");
printf ("Press 2 to display stored information\n");
.........
.........
scanf("%d", &opt);
return opt;
}
int input_num_of_drivers()
{
int num;
printf( "Enter the number of drivers\n" ) ;
scanf( "%d", &num ) ;
return num;
}
float input_distances(int distance_array[],int num)
/* could also be
void input_distances(int *distance_array,int num)
*/
/*
Purpose: input the distance for each driver into an array
Parameters: distance_array is the arry to hold distances
num is the number of drivers
*/
{
int i;
for (i=0;i<num;i++)
{
printf("Enter distance for driver %d\n",i);cycles through loop but values are lost
scanf( "%f", &distances);
}
return distance_array[distances]; have tried many returns!
}
void display_distances(int distance_array[],int num)
/*
Purpose: display the distance for each driver
Parameters: distance_array is the array of distances
num is the number of drivers
*/
{
int i;
/*float distances;*/
printf("Distances are ");
for (i=0;i<num;i++)
printf("Driver %d: %f ",i,distance_array);tried many combinations of variables etc but got nothing
printf("\n");
}
how do i enter the distances into the array? i though i couldn't pass arrays from functions. when i enter the distances and try display em i get "driver 1 : 000.00000000 or something
I was only getting my head around arrays so i think the combination of arrays and functions + passing values by value or ref is to much all at once.. im just taking stabs in the dark at it instead of knowing what should go where.
Lets just say the notes supplied aren't that satisfactory ie they have nothing worthwhile in them.
i need to get away from my computer before i smash somethin