Thread: I need some help testing array functions

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    5

    I need some help testing array functions

    Hi I have these arrays in a driver to use for testing:


    insert
    Code:
    #include <stdlib.h>
        #include <stdio.h>
        #include "TwoD.h"
    
    
    
    
        int main()
        {
            /* Initialization of LCV's */
            int i,j, colsum;
            TwoD td;
    
    
    
    
            /* Initialization of A and B matrices */
            int A[5][5] = { { 3, 7, 3, 6, 9 }, { 2, 0, 3, 0, 2 }, { 1, 7, 2, 2, 7 }, { 9, 2, 9, 3, 1 }, {     9, 1, 4, 8, 5 } };
            int B[5][5] = { { 6, 5, 5, 2, 1 }, { 7, 9, 6, 6, 6 }, { 8, 9, 0, 3, 5 }, { 2, 8, 7, 6, 2 }, { 3, 9, 7, 4, 0 } };
    
    
    
    
            /* Displaying A's Values */
            printf("A's Values:\n");
                for(i = 0; i < 5; i++)
                {
                    for(j = 0; j < 5; j++)
                    {
                        printf("%d    ", A[i][j]);
                    }
                printf("\n");
                }
            printf("\n\n");
    
    
    
    
            /* Displaying B's Values */
            printf("B's Values:\n");
                for(i = 0; i < 5; i++)
                {
                    for(j = 0; j < 5; j++)
                    {
                        printf("%d    ", B[i][j]);
                    }
                printf("\n");
                }
    
    
        }
    If I want to test these functions in this header:

    insert
    Code:
        #ifndef TWOD_H_INCLUDED
        #define TWOD_H_INCLUDED
        #define MAX_SIZE 50
    
    
        /* Structure defenition for TwoD */
        typedef struct
        {
            int rows, cols;
            int element[MAX_SIZE][MAX_SIZE];
        }TwoD;
    
    
        /* Computes the sum of values in each column of the matrix, storing each value in the integer array    */
        void columnSum(TwoD, int [], int);
    
    
        /* Computes the sum of values in each row of the matrix, storing each value in the integer array */
        void rowSum(TwoD, int [], int);
    
    
        /* Returns the sum of two matrices */
        TwoD twoDAdd(TwoD, TwoD);
    
    
        /* Returns the difference of two matrices */
        TwoD twoDSubtract(TwoD, TwoD);
    
    
        #endif
    I am trying to perform columnSum and rowSum, as well as twoDadd and twoDSubtract using the arrays defined in my driver. How would I do that using A and B in my driver? Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by lemmiwinks View Post
    I am trying to perform columnSum and rowSum, as well as twoDadd and twoDSubtract using the arrays defined in my driver. How would I do that using A and B in my driver? Thanks!
    You don't. The function requires a TwoD object to be passed to it (or maybe two of them), so you will have to create A and B as TwoD objects and not arrays.

    You can initialize TwoD objects (or any struct) with a list of numbers much like you did with A and B; you will either specify all the variables in order, or use named initialization. So you might have
    Code:
    TwoD A = {5, 5, {{your array}}};

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    5
    Thanks for the reply. So I could do

    Code:
        TwoD A = {5, 5, {{A}}};
        TwoD B = {5, 5, {{B}}};
    and then pass those as my arguments, i.e.

    Code:
    printf("%d\n", columnSum(TwoD A, A[], 5));


    Would this work or is my syntax for the arguments wrong?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't use A in the definition of A -- you'll have to type out all the numbers.

    Note you cannot have two variables with the same name (even if they have different types) so you'll need another name for your array of column sums.

    The type is not used when you call a function.

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    5
    Okay I've done this:

    Code:
    int A[5][5] = { { 3, 7, 3, 6, 9 }, { 2, 0, 3, 0, 2 }, { 1, 7, 2, 2, 7 }, { 9, 2, 9, 3, 1 }, { 9, 1, 4, 8, 5 } };
        int B[5][5] = { { 3, 7, 3, 6, 9 }, { 2, 0, 3, 0, 2 }, { 1, 7, 2, 2, 7 }, { 9, 2, 9, 3, 1 }, { 9, 1, 4, 8, 5 } };
        TwoD A1 = {5, 5, {{ 3, 7, 3, 6, 9 }, { 2, 0, 3, 0, 2 }, { 1, 7, 2, 2, 7 }, { 9, 2, 9, 3, 1 }, { 9, 1, 4, 8, 5 }}};
        TwoD B1 = {5, 5, {{ 3, 7, 3, 6, 9 }, { 2, 0, 3, 0, 2 }, { 1, 7, 2, 2, 7 }, { 9, 2, 9, 3, 1 }, { 9, 1, 4, 8, 5 }}};
    The part I am confused about now is how to call the function properly, as in the syntax and what to put as my arguments. Thanks again for your help

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    A and B are useless and shouldn't exist.

    You should call your functions the way the prototypes say to: columnSum requires three arguments; the first needs to be a TwoD object, the second should be an array of integers (note: not a two-dimensional array), and the third should be an integer.

  7. #7
    Registered User
    Join Date
    Nov 2013
    Posts
    5
    Alright, so if I want to print out the results of columnSum, how could I do that since the return is void?

    I tried:

    Code:
    printf("Col Sum:\n%d", columnSum(A1, a, 5));
    but of course nothing will display because the return is nothing...
    EDIT: it says "invalid use of void function"
    Last edited by lemmiwinks; 11-10-2013 at 09:06 PM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's what the second parameter is for, is to hold the result. Note also that you can't print an array directly (ie by just passing its name to printf) in any event.

  9. #9
    Registered User
    Join Date
    Nov 2013
    Posts
    5
    Oh that was very helpful, I was trying to just do printf("%d", a); but i forgot it should be

    Code:
    printf("Col Sum:\n");
            for(i = 0; i < 5; i++)
            {
                printf("%d\t", a[i]);
            }
            printf("\n");
    Thank you for that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2012, 06:41 AM
  2. testing array
    By chico1st in forum C Programming
    Replies: 6
    Last Post: 08-08-2008, 07:49 AM
  3. testing your program/functions
    By l2u in forum C++ Programming
    Replies: 6
    Last Post: 11-08-2006, 10:58 AM
  4. Array Testing problem
    By kas2002 in forum C++ Programming
    Replies: 7
    Last Post: 10-21-2002, 11:48 AM
  5. Testing Testing 123.. Oh yeah and...
    By minime6696 in forum Windows Programming
    Replies: 0
    Last Post: 08-13-2001, 09:07 AM

Tags for this Thread