Thread: more array / function help needed

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    more array / function help needed

    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

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Try this for your problem functions. I've removed comments, and removed some code. Any code I've added is in red.

    Code:
    void input_distances(int *distance_array,int num)
    {
        int i;
    
        for (i=0;i<num;i++)
        {
    
             printf("Enter distance for driver %d\n",i);
             scanf( "%f", &distances[i]);
        }
    }
    
    void display_distances(int distance_array[],int num)
    {
      int i;
      /*float distances;*/
      printf("Distances are ");
      for (i=0;i<num;i++)
        printf("Driver %d: %f ",i,distance_array[i]);
    
      printf("\n");
    }
    The reason for the array subscripting is that your code needs to make it clear which element of the array it is acting on.

    input_distances() does not need to return anything - the changes made to the distance_array argument are visible to the caller.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by l1ttledb View Post
    i though i couldn't pass arrays from functions.
    You can't return an array from a function. You can pass an array into a function all you want. If you couldn't pass an array to a function it would be impossible to, say, print a string.

    Bonus hint: There is no passing by reference in C. Everything is passed by value. The thing to realize is that when you pass an array, all that you are actually passing is the starting address (which is passed by copy). So your function can't move your array around, but it has the "real" memory address of your array, so it can make changes to the contents of that array just fine.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tabstop View Post
    You can't return an array from a function. You can pass an array into a function all you want. If you couldn't pass an array to a function it would be impossible to, say, print a string.
    Code:
    void printastring( type address ) {
        char c = (*(char *)address);
        if( c != '\0' )
        {
            printachar( c );
            printastring( ++address );
        }
    }
    Here's a ugly work around for a hypothetical situation where we can't actually pass arrays.


    Quzah
    Hope is the first step on the road to disappointment.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by quzah View Post
    Code:
    void printastring( type address ) {
        char c = (*(char *)address);
        if( c != '\0' )
        {
            printachar( c );
            printastring( ++address );
        }
    }
    Here's a ugly work around for a hypothetical situation where we can't actually pass arrays.


    Quzah
    Touché.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    31
    Quote Originally Posted by l1ttledb View Post
    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

    you have defined distances as a pointer to float

    you function should accept a pointer to float

    for example:

    Code:
    void input_distances(float *dist, int n)
    {
            int i;
            for (i = 0; i < n; i++) {
                    printf("Driver %d distance: ", i);
                    scanf("%f", &dist[i]);
            }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  2. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 02:04 AM
  3. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  4. can u help me with pointers?plzzzzzzzz
    By itcs in forum C Programming
    Replies: 7
    Last Post: 07-11-2003, 01:29 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM