Thread: C Functions how do i put this in a function

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    3

    C Functions how do i put this in a function

    Ive been told to put the following code into a function and yet when i try since it is 3 arrays i cant get it to work at all. Any ideas to get me started on this?
    I tried using a function like this
    Code:
    void funct1(int x[], y[], z[]);
    {
    for (i=0; i<funct1; i++)
    return      x[i] = i;
    return       y[i] = i;
    return       z[i] = i;
    }
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       int x[10], y[15], z[20];
       int i, sumx, sumy, sumz;
    
       for (i=0; i<10; i++)
          x[i] = i;
    
       for (i=0; i<15; i++)
          y[i] = i;
    
       for (i=0; i<20; i++)
          z[i] = i;
    
       sumx = 0;
       for (i=0; i<10; i++)
          sumx = sumx + x[i];
    
       sumy = 0;
       for (i=0; i<15; i++)
          sumy= sumy + y[i];
    
       sumz = 0;
       for (i=0; i<20; i++)
          sumz = sumz + z[i];
    
       printf ("The sum of the x array = %d\n", sumx);
       printf ("The sum of the y array = %d\n", sumy);
       printf ("The sum of the z array = %d\n", sumz);
    
       system("PAUSE");
       return 0;
    }
    It works fine with no function but im told a function would make it easier to read and faster to process although it seems pretty fast to me.

    I'm new to C and working through a workbook and don't understand functions real well yet.

    Thank you for any assistance.
    Last edited by Fyrewynd; 10-23-2003 at 02:28 AM.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    It sounds like you are being asked to make a function to sum the arrays. I could be wrong, but....

    Create a function that you pass into it one of the arrays and the array length, and it should return an integer which is the sum.

    The idea behind functions is to call it in main(), for example
    Code:
    val = test(p1, p2);  // call the function test, passing in p1 and p2, returns val
    Then the function would be defined as:
    Code:
    int test(int var1, int var2)
    {
        int rv;   // the return value
    // code that calculates rv
        return rv;
    }
    int: the type of value returned
    test: the function name
    (int var1, int var2): two integers are passed into the routine. var1 will contain the value of p1 from main, var2 will be p2
    the value rv will be returned and placed in val
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    Also... if you want to write a function which alters mulitple variables from the calling function you can just pass pointers to those variables instead (obviously multiple return statements from a single function aren't allowed).

    Code:
    #include <stdio.h>
    
    void assign_ints( int *a, int *b )
    {
      *a = 1;
    
      b[0] = 10;
      b[1] = 20;
    }
    
    int main()
    {
      int a=0, b[2]={0,0};
    
      assign_ints( &a, b );
    
      printf( "\na: %d", a );
      printf( "\nb: %d %d\n\n", b[0], b[1] );
    
      return 0;
    }
    There is no such thing as a humble opinion

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    3
    Thanks for your help guys it works great!

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int sum(int x[],int length)
    {
    int sum=0;
    int i;
    for (i=0; i<length; i++)
        sum+=x[i];
    return sum;
    }
    
    void fillArray(int x[], int length)
    {
    int i;
    for (i=0; i<length; i++)
        x[i]=i;
    }
    
    int main()
    {
       int x[10], y[15], z[20];
    
       fillArray(x, 10);
       
       fillArray(y, 15);
       
       fillArray(z, 20);
       
    
       printf ("The sum of the x array = %d\n", sum(x, 10));
       printf ("The sum of the y array = %d\n", sum(y, 15));
       printf ("The sum of the z array = %d\n", sum(z, 20));
    
       system("PAUSE");
       return 0;
    }

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by DrZoidberg
    Code:
        system("PAUSE");
    Why do so many people use this command to stop a program? It uses a system call into the OS and may not work on all systems (I think), whereas all C compilers have defined getchar() which would work without OS intervention. And it's also easier to type.

    Is this being taught in schools?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    3
    my teacher is teaching that


    Code:
    system("PAUSE");
    is the way to halt the program and that

    Code:
    getchar()
    is used to end a looping structure but then again he failed C three times so who knows what he truly knows I don't find that I learn much in class (he just confuses me) so i came to this board to learn more he did not like the way i did the array and told me to loop it in a function which we had not learned. Currently I have to write a huge program for next thursday I hope im able to do it. I got 51% on my midterm and 100% on 1 lab 25% on another and 0% on another i find some of the concepts are hard to follow or perhaps I have the wrong instructor.

  8. #8
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    getchar() alone could catch any single stray character which may or may not be desired. Another form. ch is an int.

    while(ch=getchar() != '\n' || ch != EOF);
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM