# Array/Functions dilemma

Page 1 of 1

## 7 Replies - 1153 Views - Last Post: 27 November 2010 - 12:15 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=202116&amp;s=17fdc2563227743d437e4b6f35fddd5d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 alexvl

Reputation: 0
• 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];

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

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

{

for(int I=0;I<nb;I++)
{
fflush(stdin);
printf("Student %i\n",I+1);
printf("Name ");
gets(Name[I]);
do {
}
}
}

int DisplayClass(int nb,int I=0)
{
printf("The Class\n");
for(int I=0;I<nb;I++)
{

}
}

int DisplayMax(int nb,int Max)
{
for(int I=0;I<nb;I++)
{
{
}
}
return Max;
}

void Y(int nb,int I=0)
{
printf("The winners are\n");
for(int I=0;I<nb;I++)
{
}
}

void main()
{
int nb,Max=0,I=0,X;
DisplayTitle();
DisplayClass(nb);
Y(X);

}

```

Is This A Good Question/Topic? 0

## Replies To: Array/Functions dilemma

### #2 Martyr2

• Programming Theoretician

Reputation: 5180
• Posts: 13,903
• 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.

### #3 alexvl

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

## Re: Array/Functions dilemma

Posted 25 November 2010 - 11:07 PM

```
int FindMax(int Max,int nb)
{
for(int I=0;I<nb;I++)
{
{
}
}
return Max;

}

void Y(int nb)
{
printf("The winners are\n");
for(int I=0;I<nb;I++)
{
}

}

void main()
{
int nb,Max=0,I=0,X;
DisplayTitle();
DisplayClass(nb);
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

### #4 Martyr2

• Programming Theoretician

Reputation: 5180
• Posts: 13,903
• 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) {

for (int i = 0; i < size; 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.

### #5 alexvl

Reputation: 0
• 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)

```

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

### #6 alexvl

Reputation: 0
• 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];

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

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

{

for(int I=0;I<nb;I++)
{
fflush(stdin);
printf("Student %i\n",I+1);
printf("Name ");
gets(Name[I]);
do {
}
}
}

int DisplayClass(int nb,int I=0)
{
printf("The Class\n");
for(int I=0;I<nb;I++)
{

}
}

{
for(int I=0;I<nb;I++)
{
{
}
}
return Max;

}

void Y(int nb,int I=0)
{
printf("The winners are\n");

}

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

}

```

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

### #7 Martyr2

• Programming Theoretician

Reputation: 5180
• Posts: 13,903
• 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.

• Saucy!

Reputation: 6246
• Posts: 24,014
• 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()?????