Thread: Communication, Help homework :S

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    5

    Post Communication, Help homework :S

    The teacher told us to separate our bubblesort code into functions.
    The first one is input, then processing, then output.
    The problem is I have no idea how to communicate so many values between functions, i know that i can return one value, but that isn't enough. I also know that it can be done using pointers (the teacher suggested this.)
    The problem is my understanding of pointers isn't entirely clear. Could someone explain them in the context of my homework?

    Don't mind what i did there, I was just experimenting. Otherwise the bubblesort itself and everything should work, i just can't get the damned values from one function to another...
    All help is greatly appreciated

    Code:
    #include<stdio.h>
    int n,vrd=0,loe=0; float array[];
    
    void sisestus(int *n,float array[] )
    {
    printf("\nPlease enter the number of elements :");
    scanf("%d",  &n);
    int i=0;
      for(i=0;i<n;i++)
        {
         printf("sisesta element nr ");
         printf("%d",i+1);
         printf("   \n");
         scanf("%f",&array[i]);}
    
    }
    
    
    void mullsort(int *n, int *loe, int *vrd,float array[] )
    {
    
        int temp;
        int i,j;
        for(i = 0; i < n-1; i++)
            for(j = i + 1; j < n; j++)
    {       vrd++;
                if(array[i] > array[j])
                {
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                    loe=loe+1;
                }
    }
    }
    
    void v2ljastus(int *n,int *loe,int *vrd,float array[])
    {
        int i=0;
    for(i=0;i<n;i++)
      printf("%f",array[i]);
      printf("The number of switches was \n");
      printf("%d",loe);
      printf("\nThe number of comparisons was : %d",  vrd);
    }
    
    int main()
    {
    sisestus(&n,array);
    mullsort(&n,&loe,&vrd,array);
    v2ljastus(&n,&loe,&vrd,array);
    return 0;
    }
    Last edited by XOliverX; 09-23-2009 at 12:24 PM. Reason: made sum changes,

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    In your function definitions, change the [] to a *. . . as in:
    Code:
    void v2ljastus(int *n,int *loe,int *vrd,float array[])
    becomes
    Code:
    void v2ljastus(int *n,int *loe,int *vrd,float *array)
    That'd be the first thing to look at. [] means give me this and then I loose it. * means give me this and I modify it.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't need to communicate "all those damn values", between functions.

    You have one or two values:

    1) The address of the head of your array or list

    2) The size of the array or list data. That is, how many data items do you have?

    For an array, the name of the array gives you the address of it's head (first) data.

    You should get very comfortable with passing these types of parameters, around functions. They're a mainstay in C, and other programming languages.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    Hm forgot the &. I guess that why it kept asking me for the value endlessly . ( scanf("%d", n)
    So basically i just give it the memory location and change the value within and the value shouldn't be discarded when the function ends(closes?terminates?)..?
    Is it necessary to define the length of the array? what if the user exceeds the length i have set when he wants to sort more values ?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That'd be the first thing to look at. [] means give me this and then I loose it. * means give me this and I modify it.
    That's not true at all. In fact, they both mean the exact same thing (in the context of a function parameter).
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    that just adds to my confusion :P. Now neither option works.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by XOliverX View Post
    that just adds to my confusion :P. Now neither option works.
    That due to what you are passing to the function. Look at your array declaration:
    Code:
    float array[];
    That doesn't declare an array because you haven't given it a size. It should be something like:
    Code:
    float array[100]; /* Declare an array with 100 indexes */
    If you want the array size to be variable, then do it this way:
    Code:
    float* array;
    ...
    array = malloc(sizeof(*array) * number_of_indexes);
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by bithub View Post
    That's not true at all. In fact, they both mean the exact same thing (in the context of a function parameter).
    Hmm, I was taught that (long ago). I was told that was a pass by value not a pass by reference and that anything that was passed that way wasn't modifiable. Wonder if the professor meant that was his way of doing things. . . not that it was the way it is, but the way it should be.

    Ah well, you learn something new every day.

    BTW: I'll still continue to use that nomenclature whenever I do something of the sort.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    Well time to give up, i've been working on it forever and it just wont run. I took my friends advice and tried to do it analogically to his code. His runs, mine doesn't - go figure. This is the final not so finished version.

    Syntax error before '&' token in all the function parameters.
    Can anyone suggest any good guides to pointers?

    Code:
    #include<stdio.h>
    int n,vrd=0,loe=0; int array[100];
    
    void sisestus(int &n,int array[] )
    {
    printf("\nPalun sisestage elementide arv :");
    scanf("%d", &n);
    int i=0;
      for(i=0;i<n;i++)
        {
         printf("sisesta element nr ");
         printf("%d",i+1);
         printf("   \n");
         scanf("%d",&array[i]);}
    
    }
    
    
    void mullsort(int &n, int &loe, int &vrd,int array[] )
    {
    
        int temp;
        int i,j;
        for(i = 0; i < n-1; i++)
            for(j = i + 1; j < n; j++){
            vrd++;
             if(array[i] > array[j])
                {
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                    loe=loe+1;
                }
    }
    }
    
    void v2ljastus(int &n,int &loe,int &vrd,int array[])
    {
        int i=0;
    for(i=0;i<n;i++)
      printf("%d",array[i]);
      printf("number of switches  \n");
      printf("%d",loe);
      printf("\nnumber of comparisons : %d",  vrd);
    }
    
    int main()
    {
    sisestus(n,array);
    mullsort(n,loe,vrd,array);
    v2ljastus(n,loe,vrd,array);
    return 0;
    }
    Last edited by XOliverX; 09-24-2009 at 01:58 AM.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Declaring a reference (The '&' character in the declaration) is C++ -- not C. You have made your application a C++ application, and you are attempting to compile it as C code.
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    Can anyone edit it to work with a C compiler? would be helpful .

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Go back to your first code example, where you were actually using pointers instead of references. The only differences between a pointer and a reference in C++ is that references cannot ever be NULL, and that you don't have to dereference references.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unkown Hang
    By Bladactania in forum C Programming
    Replies: 31
    Last Post: 04-22-2009, 09:33 AM
  2. Looking for communication lib
    By BrownB in forum C Programming
    Replies: 3
    Last Post: 04-27-2005, 10:01 AM
  3. Serial communication packets
    By Roaring_Tiger in forum C Programming
    Replies: 3
    Last Post: 04-26-2003, 08:33 AM
  4. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  5. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM