Thread: functions problem

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    functions problem

    First of all hello everyone I have a dilemma about displaying the max and the min using functions and arrays. As you can see I tried something but it doesn't work (the commented lines). Any comment is appreciated, thanks.


    Code:
    #include<stdio.h>
    	char Name[30][50];
    	float grades[30];
    	
    
    
    void DisplayTitle()
    {
    	printf("\tTitle\n");
    	printf("\t---------------\n");
    }
    
    int ReadNBStudent()
     {
    	 int nbstudent[50],I=0;
    	 do
    	 {
    	printf("Enter the number of students ");
    	 scanf("%i",&nbstudent);
    	 }
    	 while(nbstudent[I]>30 || nbstudent[I]<2);
    	return nbstudent[I];
     }
    
    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 Max, float grades[30])
    {
    	if (grades[0]>Max)
    	Max=grades[0];
    	return Max;
    }
    
    void Y(int nb,int I=0)
    {
    	for(int I=0;I<nb;I++)
    	{
    	printf("The winners are\n");
    	printf("%s %.2f",Name[I],grades[I]);
    	}
    }*/
    
    void main()
    {
    	int nb,Max;
    	float grades[30];
    	DisplayTitle();
    	nb=ReadNBStudent();
    	ReadAllStudent(nb);
    	DisplayClass(nb);
    	//Max=DisplayMax(Max,grades[30]);
    	//Y(Max);
    
    	
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by alexvlc
    Code:
    int DisplayMax(int Max, float grades[30])
    {
    	if (grades[0]>Max)
    	Max=grades[0];
    	return Max;
    }
    You're going to have to loop through your array. Here's some pseudo code to help you out:
    Code:
    set Max to the lowest possible value (0?)
    for each element in grades
      if element value is greater than Max
        set Max to the element value
    return max
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    Should have put Grades[I] instead of [0], thank you for that, I didn't see it. Now I'm trying to display them in a function and then call the function in the main and I can't get it to work.

    Normally it would be something like

    Code:
    Min=Max=Grades[0];
    if(grades[I]>Max)
    grades[I]=Max
    else
    grades[I]=Min
    for(int I=0;I<nb;I++) 
    { printf("%s %.2f",Name[I],Grades[I]);
    }
    I can't understand how to put it in a function.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    So I made some changes to it:

    Code:
    int DisplayMax(int Max,int I=0)
    {
    	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++)
    	{
    	fflush(stdin);
    	printf("%s %.2f",Name[I],grades[I]);
    	}
    
    void main()
    {
    	int nb,Max=0,I=0;
    	float grades[30];
    	DisplayTitle();
    	nb=ReadNBStudent();
    	ReadAllStudent(nb);
    	DisplayClass(nb);
    	grades[0]=Max;
    	Max=DisplayMax(grades[I]);
    	Y(Max);	
    
    	
    }
    and that's what it shows...I'm lost

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Again, you need to loop through your array in DisplayMax(). All you're doing right now is checking the first element of the array.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    I've made it work. The error was putting a loop in the Y function.

    Code:
    #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);
    	X=FindMax(grades,nb);
    	Y(X);
    
    	
    }
    Last edited by alexvlc; 11-26-2010 at 10:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning pointers from functions problem.
    By Swerve in forum C++ Programming
    Replies: 9
    Last Post: 03-05-2010, 01:35 AM
  2. Problem with functions in inherited classes
    By cableguy414 in forum C++ Programming
    Replies: 15
    Last Post: 08-30-2009, 10:56 AM
  3. Problem with system(), execl(), etc. functions
    By nickkrym in forum C Programming
    Replies: 5
    Last Post: 05-12-2007, 08:04 AM
  4. Problem: Functions
    By Dmitri in forum C Programming
    Replies: 21
    Last Post: 11-06-2005, 10:40 AM
  5. Problem with pointers and functions
    By Kheila in forum C++ Programming
    Replies: 5
    Last Post: 10-13-2005, 12:40 PM