Jump to content

Help with functions in C

SpaceAgentX

Hello I have this C program using an array that ask the user to enter up to 20 numbers. It displays the numbers entered, removes duplicates and also puts them into ascending order. Here is a link to my code http://pastebin.com/zYGW7v4c

I want to turn each step into a function. A function for getting the numbers, displaying them, displaying them with dupes removed and displaying them in ascending order. Can anyone show me how I can convert those steps into functions that I can call? Like what are some basics to functions? 

Link to comment
Share on other sites

Link to post
Share on other sites

Well splitting something like this can be fairly easy (especially for outputting)

 

So I won't show you all the stuff, but maybe the sorting and displaying info....that should give you a good idea how to adapt your code (although it won't require too much).

 

Anyways for displaying stuff

Your original code

printf("The numbers you entered are:\n"); // outputs the numbers you entered one number per linefor(i=0;i<count;++i){    printf("%d\n", nums[i]);}

And the code now in a function

//Returns void (ie returns nothing)//message is a const char* so you can change the message to what you want//nums is an integer pointer, pointers can be used as arrays//count self explanitoryvoid displayNums(const char* message, int *nums, int count) //Not much to it{    int i;    printf("%s", message); //"%s" is just for safety...incase message had an %s or %d in it    for(i=0;i<count;++i)    {        printf("%d\n", nums[i]); //A pointer can be treated as an array when accessing     }}//e.g. displayNums("The numbers you entered are:\n", nums, count);

 

 

So as you can see there isn't much changed and you now have a function that will print out the numbers (as well as display a message you choose)

 

 

So now for your sorting

Yours

for(i=0; i<count -1; i++){	for(j=0; j < count -1 - i; j++)	{		if (nums[j] > nums[j+1])		{			swap = nums[j];			nums[j] =nums[j+1];			nums[j+1] = swap;		}	}}

 

Now as a function

//Notice this is very similar to the output functionvoid bubbleSort(int *nums, int count){	int i,j,swap;	for(i=0; i<count -1; i++)	{		for(j=0; j < count -1 - i; j++)		{			if (nums[j] > nums[j+1])			{				swap = nums[j];				nums[j] =nums[j+1];				nums[j+1] = swap;			}		}	}}//e.g. bubbleSort(nums, count);

 

 

Anyways not going to guarantee that this will work, but I think it should.  You are mostly there, hopefully these two examples show how easy it can be to convert them into separate functions.

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

bear in mind to create functions using some criteria, you have to look at your program and go "oh, this thing does a bunch of things, like

- reads numbers

- prints numbers

- sorts numbers

- removes duplicates"

 

so it might be a good idea to create 4 functions which are able to, guess what

- read numbers

- print numbers

- sort numbers

- remove duplicates

 

another thing to keep in mind is that you don't have to just copypaste your sorting code into a function and leave it like that, but the function itself should make sense: even without looking at the rest of the program, it should still be a block of code which is able to accomplish a certain role with a certain degree of flexibility

this will make the program easier to read, and the function more reusable both in this program and in every C program that you will write from now on

 

so, it's a bad idea to create a function that reads 20 numbers, it's a good idea to create a function that reads n numbers, with n as parameter

the "read numbers" function will also present the problem of the counter, which is a variable that is used and altered inside of the function but is needed outside of it too

think about it a bit before starting to code

 

proper indentation helps A LOT when coding, just a tip there
Link to comment
Share on other sites

Link to post
Share on other sites

Okay I see. Ty for the help. One question once I turn one of those processes into a function how do I call it? Like how do I change my program so it's like oh you want to use that function ok I'll use it and do whats inside. 

Link to comment
Share on other sites

Link to post
Share on other sites

just like you call all other functions: printf, scanf and setbuf are functions, and your function will be used in the exact same way

 

be careful with just diving into code without studying it before, you may get bad habits and learn wrong things that could (will) become an obstacle when your projects grow in complexity

i don't want to sound annoying here, it's just a suggestion

first study functions, then use them

first study how something work, at least the basics, then practise on it

Link to comment
Share on other sites

Link to post
Share on other sites

Sounds good Ciccioo. So I tried that function WonderingFool gave me on its own. I put it in a new program to test it and I get a too few arguments to function error when I try and call the function. Why am I getting that?

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×