By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,414 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,414 IT Pros & Developers. It's quick & easy.

Passing a Multi-dimensional array to a function.

P: 3
Hi, this is my first post here at the forums, and I am looking for assistance with what looks to be a syntax error within my code. I am attending FIU, and looking to broaden my understanding of programming with engineering applications. The class is an introduction to C++ with engineering emphasis.

The bubble_sort function was provided by the professor. The other code was authored by myself.

My question refers to calling a two dimensional array by a function.

The goal of my current assignment is to generate an array with 15 rows and 2 columns. Fill the array with element number (column [0]) and random integers (column[1]). Print them with a 'for' loop, and then sort them with bubble_sort (user defined function). Print the new values with another 'for' loop.

I am using CodeBlocks v1.0 with it's default installer / compiler in Windows XP.

I have also checked two C++ texts(my class text book and my brother's older text) on calling multidimensional arrays to functions. Employed each book's method with no success.

My problem boils down to (what looks like) improperly calling the array to the function bubble_sort.

Here is my compiler directives with global declarations:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <cstdlib>
  3.  
  4. int array[15][2];
  5. int i;
  6. void bubble_sort(int array[][1]);
Here is the applicable function definition (which gives no errors):

Expand|Select|Wrap|Line Numbers
  1. void bubble_sort(int array[][1])
  2. {
  3.     int x,y,t,m;
  4.      for (x=0; x < m-1; x++)
  5.         for (y=0; y < m-x-1; y++)
  6.             if (array[y] > array[y+1])
  7.             {
  8.                 t=array[y][1];
  9.                 array[y][1]=array[y+1][1];
  10.                 array[y+1][1]=t;
  11.             }
  12. }
Here is the reference to use the bubble_sort function:

Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2. {
  3.     for(i=0;i<15;i++)
  4.     {
  5.         array[i][0]=(i+1);
  6.         array[i][1]=rand();
  7.         printf("index[%d], random number[%d], element number[%d]\n", i, array[i][1],i+1);
  8.     }
  9.  
  10.     printf("--------------------------------------------------\n");
  11.     //bubble_sort(int array[][1]);
  12.  
  13.     for(i=0;i<15;i++)
  14.     {
  15.         printf("index[%d], random number[%d], element number[%d]\n", i, array[i][1],i+1);
  16.     }
  17.     return 0;
  18. }
Please note that this is the majority of the code that I have written (except the bubble_sort and rand() functions) for the assignment, and the professor expects us to consult forums concerning problems we encounter.

Everything works with the exception of the line calling the function bubble_sort, which gives me an error: expected primary -expression before "int". When I omit the function call(line 12), the program builds and runs, giving my random values to column [1], element # to column [0], and the correct index values as well.

Thanks in advance,

Brandon

Future EE/CE
Feb 28 '08 #1
Share this Question
Share on Google+
4 Replies


gpraghuram
Expert 100+
P: 1,275
Hi, this is my first post here at the forums, and I am looking for assistance with what looks to be a syntax error within my code. I am attending FIU, and looking to broaden my understanding of programming with engineering applications. The class is an introduction to C++ with engineering emphasis.

The bubble_sort function was provided by the professor. The other code was authored by myself.

My question refers to calling a two dimensional array by a function.

The goal of my current assignment is to generate an array with 15 rows and 2 columns. Fill the array with element number (column [0]) and random integers (column[1]). Print them with a 'for' loop, and then sort them with bubble_sort (user defined function). Print the new values with another 'for' loop.

I am using CodeBlocks v1.0 with it's default installer / compiler in Windows XP.

I have also checked two C++ texts(my class text book and my brother's older text) on calling multidimensional arrays to functions. Employed each book's method with no success.

My problem boils down to (what looks like) improperly calling the array to the function bubble_sort.

Here is my compiler directives with global declarations:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <cstdlib>
  3.  
  4. int array[15][2];
  5. int i;
  6. void bubble_sort(int array[][1]);
Here is the applicable function definition (which gives no errors):

Expand|Select|Wrap|Line Numbers
  1. void bubble_sort(int array[][1])
  2. {
  3.     int x,y,t,m;
  4.      for (x=0; x < m-1; x++)
  5.         for (y=0; y < m-x-1; y++)
  6.             if (array[y] > array[y+1])
  7.             {
  8.                 t=array[y][1];
  9.                 array[y][1]=array[y+1][1];
  10.                 array[y+1][1]=t;
  11.             }
  12. }
Here is the reference to use the bubble_sort function:

Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2. {
  3.     for(i=0;i<15;i++)
  4.     {
  5.         array[i][0]=(i+1);
  6.         array[i][1]=rand();
  7.         printf("index[%d], random number[%d], element number[%d]\n", i, array[i][1],i+1);
  8.     }
  9.  
  10.     printf("--------------------------------------------------\n");
  11.     //bubble_sort(int array[][1]);
  12.  
  13.     for(i=0;i<15;i++)
  14.     {
  15.         printf("index[%d], random number[%d], element number[%d]\n", i, array[i][1],i+1);
  16.     }
  17.     return 0;
  18. }
Please note that this is the majority of the code that I have written (except the bubble_sort and rand() functions) for the assignment, and the professor expects us to consult forums concerning problems we encounter.

Everything works with the exception of the line calling the function bubble_sort, which gives me an error: expected primary -expression before "int". When I omit the function call(line 12), the program builds and runs, giving my random values to column [1], element # to column [0], and the correct index values as well.

Thanks in advance,

Brandon

Future EE/CE

Are u calling the function using this call
//bubble_sort(int array[][1]);
then it is surely a error.
You need not specify the int while calling the function

Raghuram
Feb 28 '08 #2

P: 3
Ok, I've made the recommended change, and now i get an error at the same line.

now the same line appears as follows:

bubble_sort(array[][1]);

with the associated error mesage: error: expected primary-expression before ']' token

edit

I looked up primary-expressions at MSDN and all of what I found didn't help.

Thanks,

Brandon
Feb 28 '08 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Read the article Arrays Revealed in the C/C++ HowTos forum. What you want is covered at the end of the article.
Feb 29 '08 #4

P: 3
I resolved the problem.

It was due to an error in the definition of the function:

Expand|Select|Wrap|Line Numbers
  1.       void bubble_sort(int array[][1])
  2.       {
  3.           int x,y,t,m;
  4.            for (x=0; x < m-1; x++)
  5.               for (y=0; y < m-x-1; y++)
  6.                   if (array[y] > array[y+1])
  7.                   {
  8.                       t=array[y][1];
  9.                       array[y][1]=array[y+1][1];
  10.                       array[y+1][1]=t;
  11.                   }
  12.       }

changed to:

Expand|Select|Wrap|Line Numbers
  1.       void bubble_sort(int array[][1])
  2.       {
  3.           int x,y,t,m=15;
  4.            for (x=0; x < m-1; x++)
  5.               for (y=0; y < m-x-1; y++)
  6.                   if (array[y][1] > array[y+1][1])
  7.                   {
  8.                       t=array[y][1];
  9.                       array[y][1]=array[y+1][1];
  10.                       array[y+1][1]=t;
  11.  
  12.                       t=array[y][0];
  13.                       array[y][0]=array[y+1][0];
  14.                       array[y+1][0]=t;
  15.                   }
  16.       }
it seems that without m=15, the compiler did not know what to define m as. Thus, when the bubble_sort was called by the main code block it would not work. Also, the "if" condition (line 6) was incorrect because the array was 1 dimension, and clearly it is 2 dimensions.

After making these changes, the program compiled and ran just as I wanted it to.

Thanks again for the help!

Brandon

edit:

also, changed function call to:

bubble_sort(array);
Feb 29 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.