Sign in to follow this  

Calling array in a function (C++)

This topic is 3570 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to make a program that read the grades from input file and then calculates the largest grade. I am trying to get this to work.. largestindex = topMark(mark[50], n); anyone know why it wouldn't work? thanks!!
#include <iostream>
#include <fstream>
#include <string>

using namespace std; //introduces namespace std
//Indroduction to Functions//
int getNumberOfSudents ();
int topMark(double mark[50], int n);

int main ( void )
{
	string name[50];
	double mark[50];
	int students = 0;//index for the name/mark array
	int n = 0; //# of students in file
	int largestindex = 0;
	double largest = 0.0;
	
	ifstream inStream;
	
	inStream.open("Marks.txt");
	
	n = getNumberOfSudents ();
	
	while (students < n)//Prints and stores the names and marks to array
	{
		inStream >> name[students] >> mark[students];
		cout << name[students] << " got " << mark[students] <<endl;
		students++;
	}
	
	//largestindex = topMark(mark[50], n); //CAN"T GET THIS TO WORK!!!!!!!!
	cout << name[largestindex] << " got the highest mark of " << largest <<endl;
	
	return 0;
}

int getNumberOfSudents ()
{
	int n = 0;
	cout << "How many students in the file? ";
	cin >> n;
	cout << endl;
	
	return n;
}
	
int topMark(double mark[50], int n)
{
	double largest = 0.0;//keeps track of the larger number between 2 marks
	int largermark = 0;// keep tracks of array index with larger number
	int largestindex = 0;
	
	for (largest = mark[largermark]; largermark < n; largermark++)
	{
		if (mark[largermark+1] > largest)
		{
			largest = mark[largermark+1];
			largestindex = largermark+1;
		}
	}
	return largestindex;
}

Share this post


Link to post
Share on other sites
A function parameter declared as an array is automatically converted into a parameter taking a corresponding pointer instead, so topMark is equivalent to

void topMark(double *mark, int n)

which takes a pointer to a double as first parameter. You, however, is passing a double; the 51:st element of the mark array. Pass the array to it instead of an element in the array.

largestindex = topMark(mark, n);

Share this post


Link to post
Share on other sites
First, mark[50] is the 51st element of array mark. If you wish to refer to the array, use its name.

Second, arrays cannot be passed by value in C or C++. You need to pass them by reference:
int topMark(double (&mark)[50], int n);


Third, you have a buffer overflow, if the number of students entered is greater than 50—good practice would have you detect that case and issue an error, instead of overwriting random memory. Or, better, using a dynamic array for that purpose.

Fourth, as Zahlman explained, you don't need to store the values at all.

Share this post


Link to post
Share on other sites

This topic is 3570 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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