Game Development Community

C programming question

by Michael Cozzolino · in Technical Issues · 01/17/2001 (8:20 pm) · 4 replies

Back to school yesterday and we are doing a little review. I forgot how to set up a function and pass an array through it. I got an A in the class. 3 weeks later I can't remember how to do this. Doh!!!
Here is the problem in case anyone would like to help.
All I have to do is give a prototype, a definition, and a function call. I don't need a complete program.

float scores[10] [6] . This represents ten students with six grades each. Write a function that will compute the average grade for any student in the array and return the average to the main program. The formal parameters for the function must include: a 2 dimension array, maximum # of columns, and student to be averaged(student # 1-10) Assume that the number for the student will be input by the user of the program in main.

Any help would be greatly appreciated. I feel like such a moron that I forgot already. I am going to feel a little embarrased if I have to go to my teacher for help. I think she expects me to have no trouble with it. I'm just drawing a blank for some reason.

About the author

Indie Developer in the Albany NY area. iOS, PC, Mac OSX development. http://itunes.apple.com/us/artist/michael-cozzolino/id367780489


#1
01/17/2001 (9:30 pm)
Here you go.

void foobar(float argscores[10][6])
{
   argscores[0][0] = 1.0f;   
}   


void test()
{
   float scores[10][6];

   scores[0][0] = 0.0f;   
   printf("Score 0,0 = %f\n", scores[0][0]);

   foobar(scores);
   printf("Score 0,0 = %f\n", scores[0][0]);
}   

[b]OUPUT:[/b]
Score 0,0 = 0.000000
Score 0,0 = 1.000000
Arrays are handled a little different than other variables in that they are always passed by reference -- more specifically, they are never copied. The above program demonstrates this.

Generally you need to place an ampersand (&) before the variable name to declare it as a variable reference. void foobar(in &ref).

--Rick
#2
01/18/2001 (9:43 am)
Wow , you are up early. Garage games is keeping you busy huh. Thanks for the help. I hope I can eventually help someone else rather than being the one being helped :D

P.S. I think I heard you were engaged or just got married; if so congrats!!!
#3
01/18/2001 (9:48 am)
Engaged to be married in March. Happy I could help, that's what we are all about.

--Rick
#4
01/18/2001 (11:10 am)
Congradulations, Rick!
-Pat