Thread: Need help with functions and tables!!

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    21

    Exclamation 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???


    thank you in advance
    ~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>
    Last edited by Cursius; 11-25-2009 at 08:41 AM.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    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
    Last edited by rogster001; 11-25-2009 at 08:47 AM.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    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. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    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. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    thanks...ill try it now and ill keep you up to date!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. double hashing not filling 100% of big tables
    By simpatico_qa in forum C Programming
    Replies: 10
    Last Post: 04-13-2009, 02:42 PM
  2. program that prints two tables showing...
    By Cyberman86 in forum C Programming
    Replies: 8
    Last Post: 04-08-2009, 01:55 PM
  3. Sin Tables (and rotations)
    By Epo in forum Game Programming
    Replies: 15
    Last Post: 05-29-2005, 05:41 PM
  4. Help with time.h functions please.
    By Ifurita. in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2003, 03:58 AM
  5. Hash tables
    By PJYelton in forum C++ Programming
    Replies: 0
    Last Post: 11-27-2002, 10:42 AM