# Array Question

Show 80 post(s) from this thread on one page
Page 2 of 4 First 1234 Last
• 11-15-2012
Sorinx
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]'
• 11-15-2012
Click_here
Quote:

Originally Posted by Sorinx
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]); }```
• 11-15-2012
Matticus
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.
• 11-15-2012
Sorinx
Quote:

Originally Posted by Matticus
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...
• 11-15-2012
whiteflags
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).
• 11-15-2012
Sorinx
I'm honestly confused, I thought I could just call 2 parts of the array to the function
• 11-15-2012
Click_here
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 */```
• 11-15-2012
Sorinx
Yes I could use two arrays, but that's not the point of doing this
• 11-15-2012
Click_here
Quote:

Originally Posted by Sorinx
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.
• 11-15-2012
Sorinx
Everything in my notes says this should work the way it is without having to use 2 arrays
• 11-15-2012
Click_here
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...
• 11-15-2012
Sorinx
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]
• 11-15-2012
Click_here
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.
• 11-15-2012
Click_here
And by pointer to the arrays, I mean this:

Code:

```float ave(float (*stu)[][3][4]); float sAve(float (*stu)[][4]);```
• 11-15-2012
Sorinx
I get the same exact error...
Show 80 post(s) from this thread on one page
Page 2 of 4 First 1234 Last