Array Question

This is a discussion on Array Question within the C Programming forums, part of the General Programming Boards category; If I do Code: #include <stdio.h> float ave(float stu[][3][4]); float sAve(float stu[][4]); int main() { int i, j, k; float ...

  1. #16
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    If I do

    Code:
    #include <stdio.h>
    float ave(float stu[][3][4]);
    float sAve(float stu[][4]);
    
    
    int main()
    {
    int i, j, k;
    float stu[2][3][4];
    
    for(i=0; i<2; i++){
        for(j=0;j<3;j++){
            for(k=0; k<4; k++){
                if(k==0){
                    printf("Enter in the grade for student %d in subject %d", j+1, k+1);
                    scanf("%d", &stu[i][j][k]);
                }else
                    scanf("%d", &stu[i][j][k]);
    }
    }
    }
    printf("All Averages: %f", ave(stu));
    printf("Student Averages: %f", sAve(stu));
    
    }
    float ave(float stu[][3][4])
    {
    float sum=0;
    int i, j, k;
    
    for(i=0; i<2; i++){
        for(j=0;j<3;j++){
            for(k=0; k<4; k++){
                sum+=stu[i][j][k];
                
    }
    }
    }
    return (sum/24);
    }
    float sAve(float stu[][4])
    {
    float sum=0, l=3, m=4;
    int i, j;
    for(i=0; i<2; i++){
        for(j=0;j<3;j++){
        sum+=stu[i][j];
                }
                    }
    return (sum/12);
                    }

    C:\Users\J\Desktop\gcc Lab8a.c -o Lab8a
    Lab8a.c: In function 'main':
    Lab8a.c:29:1: warning: passing argument 1 of 'sAve' from incompatible pointer ty
    pe [enabled by default]
    Lab8a.c:9:7: note: expected 'float (*)[4]' but argument is of type 'float (*)[3]
    [4]'

  2. #17
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,376
    Quote Originally Posted by Sorinx View Post
    That doesn't help me much because my issue isn't passing the array, it's when I have a 3 dimensional array I only want to pass the 2nd and 3rd dimension to the function
    Really...

    It does if you think about it...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stddef.h>
    #include <string.h>
    
    
    #define MAX_S 10
    
    
    void print_s(char (*arr2D_ptr)[][MAX_S]);
    
    
    int main (void)
    {
    
    
        char arr2D[2][2][MAX_S] =   {
                                        {
                                             "string 1",
                                             "string 2"
                                        }   ,
                                        {
                                             "string 3",
                                             "string 4"
                                        }
                                    };
    
    
        print_s(&arr2D[0]);
        print_s(&arr2D[1]);
    
    
        return EXIT_SUCCESS;
    
    
    }
    
    
    void print_s(char (*arr2D_ptr)[][MAX_S])
    {
        puts((*arr2D_ptr)[0]);
        puts((*arr2D_ptr)[1]);
    }
    Last edited by Click_here; 11-15-2012 at 04:20 PM. Reason: Underlined the word think.
    Fact - Beethoven wrote his first symphony in C

  3. #18
    Registered User
    Join Date
    Jun 2011
    Posts
    2,362
    1. Pass the whole thing to the function and only use the second and third dimension inside the function.
    2. Create a temporary two dimensional array, copy the desired elements from the real array, and pass the temporary array to the function.

  4. #19
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Quote Originally Posted by Matticus View Post
    1. Pass the whole thing to the function and only use the second and third dimension inside the function.
    2. Create a temporary two dimensional array, copy the desired elements from the real array, and pass the temporary array to the function.
    I shouldn't have to do this though...

  5. #20
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    I think the best that you can do is.

    float sAve(float stu[3][4]);
    sAve(stu[x]); where x is a number of your choice (0 or 1).

  6. #21
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    I'm honestly confused, I thought I could just call 2 parts of the array to the function

  7. #22
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,376
    I'd be tempted to make a subroutine like this:
    Code:
    
    
    void get_y_z_slice(  int const_x_val,                      /* The value of x for the yz plain*/
                         char (*input)[MAX_X][MAX_Y][MAX_Z],   /* The input 3D array */
                         char (*output)[MAX_Y][MAX_Z]);        /* Where the resulting plain is stored */
    Fact - Beethoven wrote his first symphony in C

  8. #23
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Yes I could use two arrays, but that's not the point of doing this

  9. #24
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,376
    Quote Originally Posted by Sorinx View Post
    I'm honestly confused, I thought I could just call 2 parts of the array to the function
    You can like my second string printing example - But if you wanted a dimension (say, z) constant and to vary the other two and store it in another

    Code:
     arr1[x][y] = arr2[x][y][2]
    You are going to have to do it manually.
    Fact - Beethoven wrote his first symphony in C

  10. #25
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Everything in my notes says this should work the way it is without having to use 2 arrays

  11. #26
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,376
    Quote Originally Posted by Sornix
    So if I do say int array[][4][5];
    and I want to just take 4 and 5 I would make a prototype


    Quote Originally Posted by Sornix
    Yes I could use two arrays, but that's not the point of doing this
    What is the point of doing this? What are you trying to achieve?

    Quote Originally Posted by Sornix
    See I tried using pointers but it gives me an error when compiling
    I showed you how to send pointers to arrays into a function/subroutine correctly.
    I'm sorry, but I just can't gauge what you are after...
    Fact - Beethoven wrote his first symphony in C

  12. #27
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    I thought I made that clear my mistake. The point is to use 1 multi dimensional array, then to call that array to functions to compute different averages

    so I want averages of stu[2][3][4] stu[3][4] and stu[4]
    Last edited by Sorinx; 11-15-2012 at 05:12 PM.

  13. #28
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,376
    scanf's with %d need to be float (%f)


    main didn't return 0 at the end


    Variables m and l and unused


    You can NOT pass a multidimensional array to a function/subroutine -> You must use a pointer.


    I got your code working changing these things.
    Fact - Beethoven wrote his first symphony in C

  14. #29
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,376
    And by pointer to the arrays, I mean this:

    Code:
    float ave(float (*stu)[][3][4]);
    float sAve(float (*stu)[][4]);
    Fact - Beethoven wrote his first symphony in C

  15. #30
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    I get the same exact error...

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 11-21-2010, 08:46 AM
  2. Array question HELP!!
    By evangela in forum C Programming
    Replies: 7
    Last Post: 11-28-2009, 11:38 AM
  3. Array question
    By Karmachrome in forum C Programming
    Replies: 4
    Last Post: 10-27-2005, 09:33 PM
  4. array question
    By KAchE in forum C Programming
    Replies: 4
    Last Post: 02-18-2002, 05:33 PM
  5. Array Question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-03-2001, 04:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21