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 multidimensiona l 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:
Here is the applicable function definition (which gives no errors):
Here is the reference to use the bubble_sort function:
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
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 multidimensiona l 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:
Code:
#include <stdio.h> #include <cstdlib> int array[15][2]; int i; void bubble_sort(int array[][1]);
Code:
void bubble_sort(int array[][1])
{
int x,y,t,m;
for (x=0; x < m-1; x++)
for (y=0; y < m-x-1; y++)
if (array[y] > array[y+1])
{
t=array[y][1];
array[y][1]=array[y+1][1];
array[y+1][1]=t;
}
}
Code:
int main(void)
{
for(i=0;i<15;i++)
{
array[i][0]=(i+1);
array[i][1]=rand();
printf("index[%d], random number[%d], element number[%d]\n", i, array[i][1],i+1);
}
printf("--------------------------------------------------\n");
//bubble_sort(int array[][1]);
for(i=0;i<15;i++)
{
printf("index[%d], random number[%d], element number[%d]\n", i, array[i][1],i+1);
}
return 0;
}
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
Comment