Array/Functions dilemma

displaying the max value in an array then calling a function

Page 1 of 1

7 Replies - 734 Views - Last Post: 27 November 2010 - 12:15 PM Rate Topic: -----

#1 alexvl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-November 10

Array/Functions dilemma

Posted 25 November 2010 - 08:27 PM

I'm trying to display the maximum value from an array with two functions.
1. I know that I must put somewhere a code like Max=grades[0], but I don't know exactly where.
2. In the void main the function DisplayMax I'm not sure exactly what parameters does it need to run.
3.The function Y is it an int or a void. At first I thought it was a void, but on a second thought, doesn't it return grades[I] and Names[I] ?

Thank you.

#include<stdio.h>
	char Name[30][50];
	float grades[30];
	
void DisplayTitle()
{
	printf("\tTitle\n");
	printf("\t---------------\n");
}

int ReadNBStudent()
 {
	 int nbstudent;
	 do
	 {
	printf("Enter the number of students ");
	 scanf("%i",&nbstudent);
	 }
	 while(nbstudent>30 || nbstudent<2);
	return nbstudent;
 }

int ReadAllStudent(int nb,int I=0)
{
	
	for(int I=0;I<nb;I++)
	{
	fflush(stdin);
	printf("Student %i\n",I+1);
	printf("Name ");
	gets(Name[I]);
	do {
	printf("Grade ");
	scanf("%f",&grades[I]);
	}
	while(grades[I]>100 || grades[I]<0);
	}
	return Name[I],grades[I];
}

int DisplayClass(int nb,int I=0)
{
	printf("The Class\n");
	printf("Students Names\tGrades\n");
	for(int I=0;I<nb;I++)
	{
	
	printf("%14s\t%6.2f\n",Name[I],grades[I]);
	}
	return Name[I],grades[I];
}

int DisplayMax(int nb,int Max)
{
	for(int I=0;I<nb;I++)
          {        
	if (grades[I]>Max)
	{
	Max=grades[I];
	}
	}
	return Max;
}

void Y(int nb,int I=0)
{
	printf("The winners are\n");
	for(int I=0;I<nb;I++)
	{
	printf("%s %.2f",Name[I],grades[I]);
	}
}

void main()
{
	int nb,Max=0,I=0,X;
	float grades[30];
	DisplayTitle();
	nb=ReadNBStudent();
	ReadAllStudent(nb);
	DisplayClass(nb);
	X=DisplayMax(grades[I],Max);
	Y(X);

	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Array/Functions dilemma

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4334
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Array/Functions dilemma

Posted 25 November 2010 - 10:07 PM

1. It is the first line in the body of DisplayMax (you should rename the function because it really isn't displaying here, it is finding max)
2. DisplayMax should take two parameters, the first one is the array itself, the second is its size.
3. It returns void, the function doesn't return a value. It is just printing. (This is your displaying function)

Think about making DisplayMax taking the array, its size. Then the first line of that function sets max = array[0], then goes into a loop checking each value of the array against max. It ends by returning max, the largest value of the array.

Hope that answers the questions. :)
Was This Post Helpful? 0
  • +
  • -

#3 alexvl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-November 10

Re: Array/Functions dilemma

Posted 25 November 2010 - 11:07 PM



int FindMax(int Max,int nb)
{
	Max=grades[0];
	for(int I=0;I<nb;I++)
          {        
	if (grades[I]>Max)
	{
		Max=grades[I];
	}
		}	
	return Max;
	
}

void Y(int nb)
{
	printf("The winners are\n");
	for(int I=0;I<nb;I++)
	{
	printf("%s %.2f",Name[I],grades[I]);
	}
	
}

void main()
{
	int nb,Max=0,I=0,X;
	float grades[30];
	DisplayTitle();
	nb=ReadNBStudent();
	ReadAllStudent(nb);
	DisplayClass(nb);
	X=FindMax(grades[I],30);
	Y(X);

	
}






You said that DisplayMax(FindMax in this case) should take two parameters, the array and then its size, so it will be FindMax(grades[I],30) ?

I tried this and it's displaying all of them and after that 0.00 0.00 0.00
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4334
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Array/Functions dilemma

Posted 25 November 2010 - 11:16 PM

Not grades[I], that is an index of grades. I am talking the actual array...

int FindMax(int *grades,int size) {
    int max = grades[0];

    for (int i = 0; i < size; i++) {
        if (grades[i] > max) { max = grades[i]; }
    }

    return max;
}



Notice how we are passing in the array itself. Now in FindMax we have full access to the grades and the size of that array. See how we use the size to then establish the loop which will then check each value against max. If it is greater, then we set max to this new value and continue through the loop. The end results in max being the maximum value of the array. We return the int.

Now you might be saying "Why pass the array to this function when the function can see the array because it is a higher scope?" That is because I am hinting to you that perhaps you shouldn't have grades at the higher scope and good practice says you pass around the array as needed, not making it a global var.

But anyways, that should get you moving in the right direction. :)
Was This Post Helpful? 1
  • +
  • -

#5 alexvl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-November 10

Re: Array/Functions dilemma

Posted 25 November 2010 - 11:44 PM

For this line
int FindMax(int *grades,int size)



Don't you mean float for the array of grades ?

Also, I recompiled it, it still doesn't find the max. It is displaying "The winners are "

These are my changes from what I have understood from you.


int FindMax(float *grades,int nb)


X=FindMax(grades,nb);



This post has been edited by alexvl: 25 November 2010 - 11:44 PM

Was This Post Helpful? 0
  • +
  • -

#6 alexvl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-November 10

Re: Array/Functions dilemma

Posted 26 November 2010 - 09:22 AM

I've made it work. The error was putting a loop in the Y function.

#include<stdio.h>
	char Name[30][50];
	float grades[30];
	


void DisplayTitle()
{
	printf("\tCollege Lasalle\n");
	printf("\t---------------\n");
}

int ReadNBStudent()
 {
	 int nbstudent;
	 do
	 {
	printf("Enter the number of students ");
	 scanf("%i",&nbstudent);
	 }
	 while(nbstudent>30 || nbstudent<2);
	return nbstudent;
 }

int ReadAllStudent(int nb,int I=0)
{
	
	for(int I=0;I<nb;I++)
	{
	fflush(stdin);
	printf("Student %i\n",I+1);
	printf("Name ");
	gets(Name[I]);
	do {
	printf("Grade ");
	scanf("%f",&grades[I]);
	}
	while(grades[I]>100 || grades[I]<0);
	}
	return Name[I],grades[I];
}

int DisplayClass(int nb,int I=0)
{
	printf("The Class\n");
	printf("Students Names\tGrades\n");
	for(int I=0;I<nb;I++)
	{
	
	printf("%14s\t%6.2f\n",Name[I],grades[I]);
	}
	return Name[I],grades[I];
}

int FindMax(float *grades,int nb)
{
	int Max=grades[0];
	for(int I=0;I<nb;I++)
          {        
	if (grades[I]>Max)
	{
		Max=grades[I];
	}
		}	
	return Max;
	
}

void Y(int nb,int I=0)
{
	printf("The winners are\n");
	printf("%s %.2f",Name[I],grades[I]);
	
	
}

void main()
{
	int nb,I=0,Max;
	float X;
	DisplayTitle();
	nb=ReadNBStudent();
	ReadAllStudent(nb);
	DisplayClass(nb);
	fflush(stdin);
	X=FindMax(grades,nb);
	Y(X);
	


	
}





This post has been edited by alexvl: 26 November 2010 - 09:26 AM

Was This Post Helpful? 0
  • +
  • -

#7 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4334
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Array/Functions dilemma

Posted 27 November 2010 - 10:53 AM

Glad you got this working. I thought your Y() function was looping through all the grades in the array, but I guess you were looking for something different. One thing I did want to point out is that since grades is an array of floats, in FindMax you also want to make "max" a float. It is working for you right now because it is truncating the floats, but you are dropping data that can make a different. 34.33 is higher than 34.10 but since it truncates, it will see them as equal 34.

Just a tip. :)
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Array/Functions dilemma

Posted 27 November 2010 - 12:15 PM

ARRRRGGGHHH!!!

Who the HELL is teaching you in 2010 to use gets(), fflush(stdin) and void main()?????
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1