# Thread: Need help with functions and tables!!

1. ## Need help with functions and tables!!

Hello everyone i just joined the forum and this is my first post so if there is something that i should fix-edit plz tell me.

I just got in college and we have some home work to do.
so the excersice is rly simple for you i guess, but for me its hard.
Here is what i have to do and btw my english are not that great so... :S

I have to write a programm that will have a "menu" for the user manipulate a talbe of 5 "spots" ( i mean like P[5], but dunno how the "spots" are called in english, places or what ever they are)
The commands are: 0= scan a table of 5 "spots"
1=print the table
2=sort the table ( ascending )
3=find minimun,maximun,avg of the numbers given.
4=exits programm
The table is integer only.

The thing is that i have to do each one using funtions like 1 function to scan,one to print,one to sort,one for min,max.
I dont want you guys to write the programm.
I have written itcompiled,and worked perfectly BUT it is without function

The scan and print i have managed,i think.
But i cant understand how to return a table from a function and functions confuse me a lot and thats were i need your help.

below ill list the body of the functions that iv managed to write so far, if you also need the full program let me know and ill put it on.

to scan the table ,the function will be called inside a for loop for i=0-5 :
Code:
```int scan()

{int num;

scanf("%d",&num)

return num;

}```
for the buble sorting:
Code:
```int bubble()

{int i,j,P[i];

for (j=0; j<5; j++)

{

for (i=0; i<4; i++)

{

if (P[i]>P[i+1])

{

temp=P[i];

P[i]=P[i+1];

P[i+1]=temp;

}

}

}

}```
the print and the min,max,avg i havent done yet.

and the questions are : HOW WILL I CALL THE FUNCTION INTO MAIN() TO WORK PROPERLY?? HOW CAN I RETURN A TABLE FROM A FUNC???

~Cursy

(sorry for the long post but wanted to show you that i tryed and not just came here and ask you to do it, and also to give as much info as possible)

EDIT: USING <STDIO.H>

2. if by table you mean array (spots are elements) it is not neccesary to return it from the function as you are passing a reference to it in the first place so changes will be retained back in the calling function,

that is of course unless you create the array local to the function in which case no dice, you will need to pass your array into the function that way you dont have to worry about the return

3. array and elements then

could u give me an example on how to call it in main?? like:
Code:
```int bubble(int t)
int main()
{...bla...
....bla...
...bla...
if (choice==2)
{
????????
}
}```
thanks

4. ## function call

Code:
```#include <stdio.h>
#include <stdlib.h>

void AlterArray(int myarray[5])
{
myarray[2] = 60;
}

int main(int argc, char *argv[])
{
int myarray[5] = { 10,20,30,40,50 };

printf("my array 3rd element: before call > %d \n",myarray[2]);

AlterArray(myarray);

printf("my array 3rd element: After call > %d \n",myarray[2]);

system("PAUSE");
return 0;
}```

5. thanks...ill try it now and ill keep you up to date!