Thread: Array Question

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    126

    Array Question

    Say I have a multidimensional array, I populated it with for's. Now if I just want the colummn and depth parts to be passed to the function is that possible?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can pass parts of an array to a function - what you normally would do is either pass one element/value of the array to the function, or pass one pointer to say, the top 100 elements of the function.

    You're only passing a pointer, in any case. All the data is not copied over. C assumes that you will be modifying the array's values.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    See I tried using pointers but it gives me an error when compiling

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > See I tried using pointers but it gives me an error when compiling
    See?
    We can't see any of your code, so we can't say what you got wrong, or how to fix it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    I'm not writing any code it's more of a question for learning purposes.

    So if I do say int array[][4][5];
    and I want to just take 4 and 5 I would make a prototype

    int Ave(array[j][k]);

    then in int main declare j and k as 4 and 5?

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    See I tried using pointers but it gives me an error when compiling
    I'm not writing any code it's more of a question for learning purposes.
    ... ?

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    ...I was writing code before, but went another route. I'm just trying to figure out how I would go about it correctly via pseudo example

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Ok so for example.

    Code:
    #include <stdio.h>
    float ave(float stu[][3][4]);
    float sAve(float stu[j][k]);
    
    
    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));
    
    }
    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[j][k])
    {
    float sum=0;
    int i, j;
    for(i=0; i<2; i++){
        for(j=0;j<3;j++){
        sum+=stu[i][j];
                }
                    }
    return (sum/12);
                    }
    Last edited by Sorinx; 11-15-2012 at 04:09 PM.

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    J and K undeclared not in a function. How do I point correctly?

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well the way I like to visualize a 3-d array is like a cube. So, think of it like a matrix on one side that represents one students grades, and the depth vector represents all of your students. This way you can slice the cube, in a manner of speaking, and get the grades of each student.

    A sane way to build such a data structure is:
    Code:
    typedef float grades[2];
    typedef grades courses[3];
    typedef courses students[4];
    
    students class_room; // definition
    This way isn't insanely popular, but you have to remember what all the dimensions mean. So to pass in one student, you need a matrix of floats; to pass in the whole class you need a cube, etc. To actually print, it might be easier to code a function that prints one students courses, and then loops over the whole class, calling the function.
    Last edited by whiteflags; 11-15-2012 at 04:24 PM.

  11. #11
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Yeah I know how a 3 dimensional array is however not supposed to do it that way

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Sorinx View Post
    Yeah I know how a 3 dimensional array is however not supposed to do it that way
    It is the same thing. You just have an easier time remembering what each dimension means.

    Plus, I didn't quite get what you were asking. The code you were complaining about here was in a separate post:

    Quote Originally Posted by Sorinx View Post
    J and K undeclared not in a function. How do I point correctly?
    This is about the most incompetent way to ask this question.

    Code:
    #include <stdio.h>
    float ave(float stu[][3][4]);
    float sAve(float stu[j][k]);
    
    ...
    
    float sAve(float stu[j][k])
    {
        float sum=0;
        int i, j;
        for(i=0; i<2; i++)
        {
            for(j=0; j<3; j++)
            {
                sum+=stu[i][j];
            }
        }
        return (sum/12);
    }
    The errors are (as indicated):
    Code:
    -------------- Build: Debug in foo ---------------
    
    mingw32-gcc.exe -Wall -ansi  -g  -pedantic -w -Wextra -Wall -g -std=gnu99 -D_UNICODE -DUNICODE    -c C:\Users\Josh2\Documents\foo\main.c -o obj\Debug\main.o
    C:\Users\Josh2\Documents\foo\main.c:3: error: 'j' undeclared here (not in a function)
    C:\Users\Josh2\Documents\foo\main.c:3: error: 'k' undeclared here (not in a function)
    C:\Users\Josh2\Documents\foo\main.c:48: error: 'j' undeclared here (not in a function)
    C:\Users\Josh2\Documents\foo\main.c:48: error: 'k' undeclared here (not in a function)
    Process terminated with status 1 (0 minutes, 0 seconds)
    4 errors, 0 warnings
    You get these errors because you are specifying the dimensions of a multi-dimensional array. This is restricted to constant integral expressions (i.e. numbers), a variable name here will not pass the compiler.

    Next time, I expect you to provide the actual error text, or I will just ignore you.

  13. #13
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    So how would I do it correctly?
    Last edited by Sorinx; 11-15-2012 at 05:08 PM.

  14. #14
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Here is an example of sending a pointer to a 2D array to a subroutine

    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[][MAX_S] =   {
                                    "string 1",
                                    "string 2"
                                };
    
    
        print_s(&arr2D);
    
    
        return EXIT_SUCCESS;
    
    
    }
    
    
    void print_s(char (*arr2D_ptr)[][MAX_S])
    {
        puts((*arr2D_ptr)[0]);
        puts((*arr2D_ptr)[1]);
    }
    Fact - Beethoven wrote his first symphony in C

  15. #15
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 11-21-2010, 09:46 AM
  2. Array question HELP!!
    By evangela in forum C Programming
    Replies: 7
    Last Post: 11-28-2009, 12:38 PM
  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, 06:33 PM
  5. Array Question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-03-2001, 05:23 PM