Passing an array to a function

Passing an array to a function

Page 1 of 1

13 Replies - 3103 Views - Last Post: 30 January 2007 - 08:17 AM Rate Topic: -----

#1 princejlynn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 15-January 07

Passing an array to a function

Post icon  Posted 29 January 2007 - 02:22 PM

Can anyone help me with this? I have posted the code below.....I was able to compile and run it without the function. However, when I tried to make it a function, I am getting errors. I don't know how to properly pass the array to the function and have it in descending order.


This was the assignment:

Write a program that sorts an array of 20 integers in descending order. Define a function that performs the sort. Display the array before and after the sort. Provide a complete explanation of how your sort routine works.

These are the errors:

40 C:\Dev-Cpp\WK5IA[2].cpp `SIZE' was not declared in this scope

44 C:\Dev-Cpp\WK5IA[2].cpp expected init-declarator before "int"

44 C:\Dev-Cpp\WK5IA[2].cpp expected `,' or `;' before "int"


Any help would be appreciated. Thanks..



#include <stdio.h>
#include <stdlib.h>


	//function declarations
	int sort(const int, int);



int main()
{
	//variable declarations 
	const int SIZE = 20;  
	int a[SIZE] = {23, 69, 2, 8, 44, 99, 72, 30, 10, 4, 95, 36, 22, 77, 16, 1, 80, 12, 5, 120};
	


	//title
	printf("\n\tInteger Sorting Program\n\n");

	
	printf("\nIntegers In Original Order\n");
	for(int i =0; i < SIZE; ++i)
	   printf("%4d", a[i]);
	printf ("\n\n");



	printf("\nIntegers In Descending Order\n");

	for(int j = 0; j < SIZE; ++j)
		printf("%4d", &sort);

	printf("\n\n");
	system("PAUSE");
		   
}//end main

		   //function definition
		   void sort(const int SIZE = 20, int a[SIZE])

		   //variable declarations

		   int hold
		   int numberOfComp = 0
		   int comp
			

		   {
				return	 //Bubble Sort-Descending Order
				comp = SIZE-1;
				for(int pass = 0;pass < SIZE-1;pass++)  //passes
				{
					for( int i =0; i < comp; i++)  //one pass
					{
						numberOfComp++;
						if(a[i] < a[i+1])
						{   //one comparison
							hold = a[i];	//one swap
							a[i] = a[i+1];
							a[i+1] = hold;
						}//end if
					}//end for
					comp--;
				}//end for

		   }//end sort


Is This A Good Question/Topic? 0
  • +

Replies To: Passing an array to a function

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Passing an array to a function

Posted 29 January 2007 - 05:32 PM

int hold
int numberOfComp = 0
int comp


To begin with, each of these statements needs to be terminated with a semi colon.
Was This Post Helpful? 0
  • +
  • -

#3 horacio  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 29-January 07

Re: Passing an array to a function

Posted 29 January 2007 - 05:56 PM

Ok: i see a couple off mistakes:
in main:
printf("%4d", &sort);
the %d parameter in printf is a control caracter, it tells the compiler that this specific printf function will print an integer, thus it espects an integer:
//
int j;
j = 8;
printf("%4d", j);
//
the & has special meaning both in c as in cpp, once applied, it returns the memory address of the variable. there is actually possible to return the address of a funcition, however in this case it is ultimatly wrong.
here is why:
printf expects in this case an integer, thus it cannot print an addres.
your function sort expects two parameters: SIZE, and an array of integers, as you defined your prototipe, so your function call cannot be: sort, it should be sort(SIZE, a);
there is acctually no need for passing the size of an array as an argument to a function, it is however a very good programming practice.
now there is one thing about arrays in c. Any variable that you pass to a function is actually not the same variable, ie, you only pass a copy of thar variable, that is, a function can NOT modify its value:

int function(int number);
int main()
{
int j;
j = 3;
function(j);
printf("%d", j);
}
int function(int j)
{
j = 4;
return 0;
}

j is still going to be 3. Now like any rule, there is an exception. Arrays are passed by reference, ie, there values CAN be modified by functions, so, once you called sort with the apropiate parameters you should print a like this;
for(i = 0; i < SIZE; i++)
printf("%d", a[i]);

so if your sort function does what it is soposed to do, it should modify the array in main.

hopefully this helps you in some way, if you more cuestions i promisse to try and answer them please excuse any and all misspellings, i never took any formal lessons, in c and cpp however i am very fluent..

This post has been edited by horacio: 29 January 2007 - 05:58 PM

Was This Post Helpful? 0
  • +
  • -

#4 princejlynn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 15-January 07

Re: Passing an array to a function

Post icon  Posted 29 January 2007 - 07:29 PM

ok, I think I understand, and I made some changes, but it still isn't working. I have even more errors than before. Any suggestions? I have to turn it in tonight.

#include <stdio.h>
#include <stdlib.h>


//function declarations
int sort(const int, int);



int main()
{
//variable declarations
const int SIZE = 20;
int a[SIZE] = {23, 69, 2, 8, 44, 99, 72, 30, 10, 4, 95, 36, 22, 77, 16, 1, 80, 12, 5, 120};



//title
printf("\n\tInteger Sorting Program\n\n");


printf("\nIntegers In Original Order\n");
for(int i =0; i < SIZE; ++i)
printf("%4d", a[i]);
printf ("\n\n");



printf("\nIntegers In Descending Order\n");

for(int j = 0; j < SIZE; ++j)
printf("%4d", sort(size,a);

printf("\n\n");
system("PAUSE");

}//end main

//function definition
void sort(const int SIZE = 20, int a[SIZE])

//variable declarations

int hold;
int numberOfComp = 0;
int comp;


{
return //Bubble Sort-Descending Order
comp = SIZE-1;
for(int pass = 0;pass < SIZE-1;pass++) //passes
{
for( int i =0; i < comp; i++) //one pass
{
numberOfComp++;
if(a[i] < a[i+1])
{ //one comparison
hold = a[i]; //one swap
a[i] = a[i+1];
a[i+1] = hold;
}//end if
}//end for
comp--;
}//end for

}//end sort























View Posthoracio, on 29 Jan, 2007 - 05:56 PM, said:

Ok: i see a couple off mistakes:
in main:
printf("%4d", &sort);
the %d parameter in printf is a control caracter, it tells the compiler that this specific printf function will print an integer, thus it espects an integer:
//
int j;
j = 8;
printf("%4d", j);
//
the & has special meaning both in c as in cpp, once applied, it returns the memory address of the variable. there is actually possible to return the address of a funcition, however in this case it is ultimatly wrong.
here is why:
printf expects in this case an integer, thus it cannot print an addres.
your function sort expects two parameters: SIZE, and an array of integers, as you defined your prototipe, so your function call cannot be: sort, it should be sort(SIZE, a);
there is acctually no need for passing the size of an array as an argument to a function, it is however a very good programming practice.
now there is one thing about arrays in c. Any variable that you pass to a function is actually not the same variable, ie, you only pass a copy of thar variable, that is, a function can NOT modify its value:

int function(int number);
int main()
{
int j;
j = 3;
function(j);
printf("%d", j);
}
int function(int j)
{
j = 4;
return 0;
}

j is still going to be 3. Now like any rule, there is an exception. Arrays are passed by reference, ie, there values CAN be modified by functions, so, once you called sort with the apropiate parameters you should print a like this;
for(i = 0; i < SIZE; i++)
printf("%d", a[i]);

so if your sort function does what it is soposed to do, it should modify the array in main.

hopefully this helps you in some way, if you more cuestions i promisse to try and answer them please excuse any and all misspellings, i never took any formal lessons, in c and cpp however i am very fluent..

Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Passing an array to a function

Posted 29 January 2007 - 07:47 PM

Please post the errors you are receiving. Also, you've not changed the function as advised by horacio...it should look something like:
void sort(int SIZE , int a[])


Was This Post Helpful? 0
  • +
  • -

#6 princejlynn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 15-January 07

Re: Passing an array to a function

Posted 29 January 2007 - 08:16 PM

Ok, I fixed that..........

These are the errors I am still receiving...........

In function `int main()':
32 `size' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)


32expected primary-expression before ']' token

32 cpp At global scope:

44expected init-declarator before "int"

44 expected `,' or `;' before "int"

49 expected unqualified-id before '{' token

49 expected `,' or `;' before '{' token
Was This Post Helpful? 0
  • +
  • -

#7 princejlynn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 15-January 07

Re: Passing an array to a function

Posted 29 January 2007 - 08:24 PM

Ok, now take a look.....

I believe I fixed those errors. I have two left. I have posted the fixed code below along with the errors. Thanks.

Errors:
49 expected unqualified-id before '{' token


49 expected `,' or `;' before '{' token






#include <stdio.h>
#include <stdlib.h>


//function declarations
int sort(const int, int);



int main()
{
//variable declarations
const int SIZE = 20;
int a[SIZE] = {23, 69, 2, 8, 44, 99, 72, 30, 10, 4, 95, 36, 22, 77, 16, 1, 80, 12, 5, 120};



//title
printf("\n\tInteger Sorting Program\n\n");


printf("\nIntegers In Original Order\n");
for(int i =0; i < SIZE; ++i)
printf("%4d", a[i]);
printf ("\n\n");



printf("\nIntegers In Descending Order\n");

for(int i = 0; i < SIZE; ++i)
printf("%4d", sort(SIZE,a[i]));

printf("\n\n");
system("PAUSE");

}//end main

//function definition
void sort(int SIZE, int a[]);

//variable declarations

int hold;
int numberOfComp = 0;
int comp;


{
return //Bubble Sort-Descending Order
comp = SIZE-1;
for(int pass = 0;pass < SIZE-1;pass++) //passes
{
for( int i =0; i < comp; i++) //one pass
{
numberOfComp++;
if(a[i] < a[i+1])
{ //one comparison
hold = a[i]; //one swap
a[i] = a[i+1];
a[i+1] = hold;
}//end if
}//end for
comp--;
}//end for

}//end sort
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Passing an array to a function

Posted 29 January 2007 - 08:33 PM

void sort(int SIZE, int a[])
{
int hold;
int numberOfComp = 0;
int comp;

comp = SIZE-1;
for(int pass = 0;pass < SIZE-1;pass++) //passes
{
for( int i =0; i < comp; i++) //one pass
{
numberOfComp++;
if(a[i] < a[i+1])
{ //one comparison
hold = a[i]; //one swap
a[i] = a[i+1];
a[i+1] = hold;
}//end if
}//end for
comp--;
}//end for

}//end sort


Was This Post Helpful? 0
  • +
  • -

#9 princejlynn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 15-January 07

Re: Passing an array to a function

Posted 29 January 2007 - 08:49 PM

Thank you so much... that fixed that error..........

Now I have this error....................

In function `int main()':

34 invalid use of void expression




#include <stdio.h>
#include <stdlib.h>




//function declarations
void sort(int, int);



int main()
{
//variable declarations
const int SIZE = 20;
int a[SIZE] = {23, 69, 2, 8, 44, 99, 72, 30, 10, 4, 95, 36, 22, 77, 16, 1, 80, 12, 5, 120};



//title
printf("\n\tInteger Sorting Program\n\n");


printf("\nIntegers In Original Order\n");
for(int i =0; i < SIZE; ++i)
printf("%4d", a[i]);
printf ("\n\n");



printf("\nIntegers In Descending Order\n");

for(int i = 0; i < SIZE; ++i)
printf("%4d", sort(SIZE,a[i]));

printf("\n\n");
system("PAUSE");

}//end main

//function definition
void sort(int SIZE, int a[])
{
int hold;
int numberOfComp = 0;
int comp;

comp = SIZE-1;
for(int pass = 0;pass < SIZE-1;pass++) //passes
{
for( int i =0; i < comp; i++) //one pass
{
numberOfComp++;
if(a[i] < a[i+1])
{ //one comparison
hold = a[i]; //one swap
a[i] = a[i+1];
a[i+1] = hold;
}//end if
}//end for
comp--;
}//end for

}//end sort
Was This Post Helpful? 0
  • +
  • -

#10 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Passing an array to a function

Posted 29 January 2007 - 08:55 PM

Right here:
printf("%4d", sort(SIZE,a[i]));


You are providing the sort function as a parameter to the printf function as if the sort function returns a value. It does not.
I expect what you mean is:
sort(SIZE,a[i]);
for(int i = 0; i < SIZE; ++i)
printf("%4d", a[i]);


Was This Post Helpful? 0
  • +
  • -

#11 princejlynn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 15-January 07

Re: Passing an array to a function

Posted 29 January 2007 - 09:07 PM

I am sorry...........I keep getting different errors everytime I change something.....

How frustrating............


Errors:

In function `int main()':
35 name lookup of `i' changed for new ISO `for' scoping
27 using obsolete binding at `i'

What does this mean??



#include <stdio.h>
#include <stdlib.h>




//function declarations
void sort(int, int);



int main()
{
//variable declarations
const int SIZE = 20;
int a[SIZE] = {23, 69, 2, 8, 44, 99, 72, 30, 10, 4, 95, 36, 22, 77, 16, 1, 80, 12, 5, 120};



//title
printf("\n\tInteger Sorting Program\n\n");


printf("\nIntegers In Original Order\n");

//loop to check the size of the array for the orginal integer order
for(int i =0; i < SIZE; ++i)
printf("%4d", a[i]);
printf ("\n\n");



printf("\nIntegers In Descending Order\n");

sort(SIZE,a[i]);//call function

for(int i = 0; i < SIZE; ++i)//loop to check size of array
printf("%4d", a[i]);

printf("\n\n");
system("PAUSE");

}//end main

//function definition
void sort(int SIZE, int a[])
{
int hold;
int numberOfComp = 0;
int comp;

comp = SIZE-1;
for(int pass = 0;pass < SIZE-1;pass++) //passes
{
for( int i =0; i < comp; i++) //one pass
{
numberOfComp++;
if(a[i] < a[i+1])
{ //one comparison
hold = a[i]; //one swap
a[i] = a[i+1];
a[i+1] = hold;
}//end if
}//end for
comp--;
}//end for

}//end sort
Was This Post Helpful? 0
  • +
  • -

#12 princejlynn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 15-January 07

Re: Passing an array to a function

Posted 29 January 2007 - 09:38 PM

any ideas?
Was This Post Helpful? 0
  • +
  • -

#13 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Passing an array to a function

Posted 30 January 2007 - 04:41 AM

View Postprincejlynn, on 30 Jan, 2007 - 04:38 AM, said:

any ideas?

some C compilers do not allow the definition of the loop control variable within the for(), e.g.
for(int i = 0; i < SIZE; ++i)//loop to check size of array


you must define i before the for() statement

made the changes to the for()s plus a few others (indicated by // ** comments) - it now seems to compile and run
#include <stdio.h>
#include <stdlib.h>




//function declarations
void sort(int, int[]);   // ** second parameter is an array!



int main()
{
//variable declarations
//const int SIZE = 20;   // ** initialise array size in C with this
#define SIZE 20		  // use define
int a[SIZE] = {23, 69, 2, 8, 44, 99, 72, 30, 10, 4, 95, 36, 22, 77, 16, 1, 80, 12, 5, 120};
int i;	// ** define i here - not all C compilers allow for (int i=0;  ...


//title
printf("\n\tInteger Sorting Program\n\n");


printf("\nIntegers In Original Order\n");

//loop to check the size of the array for the orginal integer order
for(i =0; i < SIZE; ++i)
printf("%4d", a[i]);
printf ("\n\n");



printf("\nIntegers In Descending Order\n");

sort(SIZE,a);//call function   ** removed the []

for(i = 0; i < SIZE; ++i)//loop to check size of array
printf("%4d", a[i]);

printf("\n\n");
system("PAUSE");

}//end main

//function definition
void sort(int size, int a[])   // ** avoid using same identifiers are a #define
{
int hold;
int numberOfComp = 0;
int comp, pass, i;  // ** define pass and i here

comp = size-1;
for(pass = 0;pass < size-1;pass++) //passes
{
for(i =0; i < comp; i++) //one pass
{
numberOfComp++;
if(a[i] < a[i+1])
{ //one comparison
hold = a[i]; //one swap
a[i] = a[i+1];
a[i+1] = hold;
}//end if
}//end for
comp--;
}//end for

}//end sort


Was This Post Helpful? 0
  • +
  • -

#14 princejlynn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 15-January 07

Re: Passing an array to a function

Post icon  Posted 30 January 2007 - 08:17 AM

Thanks to everyone who assisted me with my program. I ended up submitting it last night with the compiler problem. I have made the changes suggested to the code, and it now works. Thanks again for all of your patience and assistance.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1