Jump to content
Can't remember your login details? Read more... ×
Sign in to follow this  
Shikimaru

Functions and Arrays in C

Recommended Posts

Gday all,

 

I'm writing yet another assignment for uni, and i'm having a little trouble with my functions

#include <stdio.h>

int cleartable(int table[][], int rank_table[][]);
int displaytable(int rank_table[][], int table[][]);

int cleartable(int table[20][20], int rank_table[20][1])
{
	int i, j;
	
	for(i=0;i<=16;i++){
		rank_table[i][1] = 0;
		rank_table[i][0] = 0;
		rank_table[i][0] = i;
		for(j=0;j<=9;j++){
			table[i][j] = 0;

		}
	}
}

int copytable(int table[20][20], int rank_table[20][1])
{
	int i;

	for(i=0;i<16;i++){
		rank_table[i][1] = table[i][8];
	}

}

int sorttable(int rank_table[20][1])
{
	int i, j, copy_team, copy_rank;

	for(i=0;i<20;i++){
		for(j = i+1;j < 20;j++){
			if(rank_table[j][1] < rank_table[i][1]){
				copy_team = rank_table[i][0];
				copy_rank = rank_table[i][1];
				rank_table[i][1] = rank_table[j][1];
				rank_table[i][0] = rank_table[j][0];
				rank_table[j][1] = copy_rank;
				rank_table[j][0] = copy_team;
			}
		}
	}
}

int displaytable(int rank_table[20][1], int table[20][20])
{
	int i;

	printf("  Rank	Team	Games	Wins	Loss's	Draws	PF	PA	Diff	Points\n\n");

	for(i=0;i<16;i++){
		printf("  %d", (rank_table[i][0]+1));
		printf("	MNLY");
			printf("	%d	%d	%d	%d	%d	%d	%d	%d\n", table[rank_table[i][0]][1], table[rank_table[i][0]][2], table[rank_table[i][0]][3], table[rank_table[i][0]][4], table[rank_table[i][0]][5], table[rank_table[i][0]][6], table[rank_table[i][0]][7], table[rank_table[i][0]][8]);
	}
}	

int main(void)
{

	int table[20][20], rank_table[20][1], i, j, k;

	cleartable(table, rank_table);

	table[0][8] = 3;
	table[1][8] = 6;
	table[2][8] = 1;
	table[3][8] = 8;
	table[4][8] = 5;
	table[5][8] = 2;
	table[6][8] = 4;
	table[7][8] = 5;
	table[8][8] = 9;
	table[9][8] = 10;
	table[10][8] = 26;
	table[11][8] = 12;
	table[12][8] = 13;
	table[13][8] = 14;
	table[14][8] = 15;
	table[15][8] = 16;

	copytable(table, rank_table);
	sorttable(rank_table);
	displaytable(rank_table, table);


return (0);
}

I'm having trouble importing the arrays into the functions, and getting the Main to recognise that they functions. Should i change the type to void as there is no return value?

Share this post


Link to post
Share on other sites

You've probably figured out by now but for multidimensional arrays you must always specify the size of each dimension (except the highest dimension, that's optional).

 

This is the only way C has to figure out where each row in your array ends and the next begins. And since usually in bigger programs, parts of your program will only see the function declarations and not the entire functions until the linking stage, it's important this information is also available in your declarations. So in your case you need to change lines 3 and 4 to:

 

int cleartable(int table[20][20], int rank_table[20][1]);
int displaytable(int rank_table[20][1], int table[20][20]);

Since you don't need the size of your highest dimension (or the names of your parameters in your declaration) this can also be:

int cleartable(int [][20], int [][1]);
int displaytable(int [][1], int [][20]);

But it can't hurt to be explicit with array sizes unless there's a particular reason you need to be flexible.

 

Also you're right. Since your function don't return anything their return type should be void. Otherwise weird thing can happen if you try to sample their output and the compiler won't be able to protect you. But in your case it won't break your program.

 

 

Finally, you're going to get a segmentation fault with your current code. You need to take another look at your rank_table size because you're trying to access a value that is out of bounds of your array in some functions.

Share this post


Link to post
Share on other sites

Awesome, that's so much for that, as you guessed i have gone over it and fixed many of those problems, so they are hunkydory. :) Alot of the numbers and sorting algorithms were a little dodgy too, so i've gone and fixed them up

 

This is the current status of the code, with changes so that it works well

 

#include <stdio.h>

void cleartable(int table[20][20], int rank_table[20][2])
{
	int i, j;

	for(i=0;i<=19;i++){
		rank_table[i][1] = 0;
		rank_table[i][0] = 0;
		rank_table[i][0] = i;
		for(j=0;j<=19;j++){
			table[i][j] = 0;

		}
	}
}

void copytable(int table[20][20], int rank_table[20][2])
{
	int i;

	for(i=0;i<19;i++){
		rank_table[i][1] = table[i][8];
	}

}

void sorttable(int rank_table[20][2], int array_size)
{
	int i, j, copy_team, copy_rank;

	while(array_size>=0){
		array_size--;
		for(i=0;i<19;i++){
			j=i+1;
			if(rank_table[i][1] < rank_table[j][1]){
				copy_team = rank_table[i][0];
				copy_rank = rank_table[i][1];
				rank_table[i][0] = rank_table[j][0];
				rank_table[i][1] = rank_table[j][1];
				rank_table[j][0] = copy_team;
				rank_table[j][1] = copy_rank;
			}
		}
	}
}

void displaytable(int rank_table[20][2], int table[20][20])
{
	int i;

	printf("  Rank	Team	Games	Wins	Loss's	Draws	PF	PA	Diff	Points\n\n");

	for(i=0;i<16;i++){
		printf("  %d", (rank_table[i][0]+1));
		printf("	MNLY");
			printf("	%d	%d	%d	%d	%d	%d	%d	%d\n", table[rank_table[i][0]][1], table[rank_table[i][0]][2], table[rank_table[i][0]][3], table[rank_table[i][0]][4], table[rank_table[i][0]][5], table[rank_table[i][0]][6], table[rank_table[i][0]][7], table[rank_table[i][0]][8]);
	}
}

int main(void)
{

	int table[20][20], rank_table[20][2], i, j, k, array_size;

	array_size = 16;
	cleartable(table, rank_table);

	table[0][8] = 3;
	table[1][8] = 6;
	table[2][8] = 1;
	table[3][8] = 8;
	table[4][8] = 5;
	table[5][8] = 2;
	table[6][8] = 4;
	table[7][8] = 5;
	table[8][8] = 9;
	table[9][8] = 10;
	table[10][8] = 26;
	table[11][8] = 12;
	table[12][8] = 13;
	table[13][8] = 14;
	table[14][8] = 15;
	table[15][8] = 16;

	copytable(table, rank_table);
	sorttable(rank_table, array_size);
	displaytable(rank_table, table);


return (0);
}

Share this post


Link to post
Share on other sites

Effectively that i believe changes nothing using "void" regardles the one block-scope step, you should always return variables type

 

e.g. "int" for alike any rank_table[ ] [ ] int

would each have a procedure in those for loops for their base array:

 

 

int element oops earlier! yes last night was friday night
return (rank_table[arrayIdxi][elementIdx]=element);
} _linenums:0'>int set_RTab_Int(int arrayIdx,int elementIdx,int element){ // int element oops earlier! yes last night was friday nightreturn (rank_table[arrayIdxi][elementIdx]=element);}

note: Strings or char generally passed in as pointers to a function and joins the pointers so have no return statement unless in some contexts

of initialising another variable by the function.

 

for returning the array itself the function template signature definiyion would look something alike [don't require to if your only updating the elements that are not an array]

 

int[][] rtabArr(....){....return rank_table;}

Then for morefun there are function pointers.

Edited by nicephotog-jvm.net

Share this post


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
Sign in to follow this  

×