Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing an array as function paramater problem.

Status
Not open for further replies.

Valandil

Programmer
Jul 11, 2006
2
US
Ok, so im working on a little program that I need to prompt the user to input integers, which are stored in an array. The data in the array is then passed into another function which adds all the array location together, and divides it by the number of numbers entered to give an average of the numbers input by the user.

Heres my code...
Code:
#include <stdio.h>

#define MAXSTUDENTS 25

int input(int numgrades[]);
float average(int numgrades[], int num_grades);

int main(void) {
	float mean = 0.0;
	int numgrades[MAXSTUDENTS];
	int grades_entered = 0;
	
	grades_entered = input(numgrades);
	mean = average(numgrades, grades_entered);
}

//sub functions

int input(int numgrades[]) {
	int i = 0;

	printf("Enter the grades you would like curved.\nEnter -1 to finish.\n");
	scanf("%d%*c", &numgrades[i]);

	while (i < MAXSTUDENTS - 1 && numgrades[i] != -1) {
		i++;
		scanf("%d%*c", &numgrades[i]);
	}

	if (i == MAXSTUDENTS - 1) {
		printf("Maximum number of grades allowed is 25.\nNo more grades allowed.");
		return(i + 1);
	} else {
		return (i);
	}
}

float average(int numgrades[], int num_grades) {
	int i = 1;
	int num = 0;
	int sum = 0;
	float average = 0.0;

	for (i = 1; i <= num_grades; i++) {
		numgrades[i] = num;
		sum = sum + num;
	}

	average = sum / num_grades;
	return (average);
}

So the problem I'm having is the data is being stored properly in the function that gets the data from the user, but when I pass it to the average() function its not passing the array data correctly. What is causing this, and what do I need to do to fix it?

Thanks,
Val
 
Well I guess I fixed it myself.

Code:
#include <stdio.h>

#define MAXSTUDENTS 25

int input(int [COLOR=red]*numgrades[][/color]);
float average(int [COLOR=red]*numgrades[][/color], int num_grades);

int main(void) {
	float mean = 0.0;
	int numgrades[MAXSTUDENTS] [COLOR=red]= {0}[/color];
	int grades_entered = 0;
	
	grades_entered = input(numgrades);
	mean = average(numgrades, grades_entered);
	printf("\n%f", mean);
}

// sub functions

int input(int numgrades[]) {
	int i = 0;

	printf("Enter the grades you would like curved.\nEnter -1 to finish.\n");
	scanf("%d%*c", &numgrades[i]);

	while (i < MAXSTUDENTS - 1 && numgrades[i] != -1) {
		i++;
		scanf("%d%*c", &numgrades[i]);
	}

	if (i == MAXSTUDENTS - 1) {
		printf("Maximum number of grades allowed is 25.\nNo more grades allowed.");
		return(i + 1);
	} else {
		return (i);
	}
}

float average(int numgrades[], int num_grades) {
	int i = [COLOR=red]0[/color];
	int num = 0;
	int sum = 0;
	float average = 0.0;

	for (i = [COLOR=red]0[/color]; i < num_grades; i++) {
		[COLOR=red]num = numgrades[i];[/color]
		sum = sum + num;
	}

	average = sum / num_grades;
	return (average);
}
 
I fixed it myself?
No, you can't compile this code!
Compare input() prototype:
Code:
int input(int *numgrades[]); /* array of pointers to int? */
and definition:
Code:
int input(int numgrades[]) { ... } /* array of int */
Think about DaveSinkula's remark...

So the problem I'm having is the data is being stored properly in the function that gets the data from the user
No, you have absolute different problem: incorrect data input into array (revise index value in the input loop: take a pencil and a paper sheet). Think about this loop condition with unitialized data test...

Think about integer (not float) division in average calculation too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top