# Thread: Calculating the lowest and highest values

1. ## Calculating the lowest and highest values

What can I inject to make this program calculate the youngest entry and the oldest entry. So far it can calculate average and number of entries.

Code:
```#include <stdio.h>
#include <stdlib.h>

#define MAXARR 100
void main (void)
{
int
mode[MAXARR], i, entry;
int counter = 0;
int total = 0;
int total2 = 0;
int mostfreq = 0;
float average, stdev;
printf("Student Age Calculator \n");
printf("======================\n\n");
printf("Input Student's Age:\n");
scanf("%d",&entry);
while (entry >= 0)
{
total = total + entry;
total2 = total2 + entry * entry;
mode[entry]++;
counter = counter +1;
printf("Input the Student's age ; Negative number for Calculations: \n");
scanf("%d",&entry);
}
average = (float)total / (float)counter;
for(i=0;i<100;i++)
{
if (mode[i] > mostfreq);
{
mostfreq = mode[i];
}
}
printf("There were %d ages recorded. \n", counter);
printf("The average age is: %6.2f \n", average);
getchar();

system("PAUSE");
}```

2. in a loop
Code:
```for( entry = 0 ; entry < MAX ; ++entry)
{
if( mode[entry] >= largest)
largest = mode[entry]
else if(mode[entry] < lowest )
lowest = mode[entry];
}```

3. cleaned it up a bit, but having some issues:

Code:
```#include <stdio.h>
#include <stdlib.h>
#define MAXARR 100

void main (void)
{
int mode[MAXARR], i, entry, lowest, largest;
int counter = 0;
int total = 0;

float average, stdev;
printf("Student Age Calculator \n");
printf("======================\n\n");
printf("Input Student's Age: ");
scanf("%d",&entry);
while (entry >= 0)

{
total = total + entry;
mode[entry]++;
counter = counter +1;
printf("Input Student's age ; Negative number for Calculations: ");
scanf("%d",&entry);
}

average = (float)total / (float)counter;

for( entry = 0 ; entry < MAX ; ++entry)
{
if( mode[entry] >= largest)
largest = mode[entry]
else if(mode[entry] < lowest )
lowest = mode[entry];
}

printf("\nThere were %d ages recorded. \n", counter);
printf("The average age is: %6.2f \n", average);
printf("The Youngest age is: %d \n", lowest);
printf("The Oldest age is: %d \n", largest);

system("PAUSE");
}```
32. syntax error before "else"
6. return type of 'main' is not `int'
28 MAX' undeclared (first use in this function)

4. 32. Statements still need semicolons after them, even inside if statements.
6. Yes, the return type of main is supposed to be int. Make it so.
28. You've got nothing called MAX. How many times do you want the loop to run?

5. it compiles but the number it generates isnt it

Code:
```#include <stdio.h>
#include <stdlib.h>
#define MAXARR 100

int main (void)
{
int mode[MAXARR], entry, lowest, largest;
int counter = 0;
int total = 0;

float average, stdev;
printf("Student Age Calculator \n");
printf("======================\n\n");
printf("Input Student's Age: ");
scanf("%d",&entry);
while (entry >= 0)

{
total = total + entry;
mode[entry]++;
counter = counter +1;
printf("Input Student's Age ; Negative number for Calculations: ");
scanf("%d",&entry);
}

average = (float)total / (float)counter;

for( entry = 0 ; entry < 100 ; ++entry)
{
if( mode[entry] >= largest)
largest = mode[entry];
else if(mode[entry] < lowest )
lowest = mode[entry];
}

printf("\nThere were %d ages recorded. \n", counter);
printf("The Average age is: %.2f \n", average);
printf("The Youngest age is: %d \n", lowest);
printf("The Oldest age is: %d \n", largest);

system("PAUSE");
}```
For example:

Input Student Age: 5
Input Student Age: 10
Input Student Age: 19
Input Student Age: -1

There were 3 ages recorded.
The Average age is 11.33.
The Youngest age is: -1779614522
The Oldest age is: 1997798193
press any key to continue...

6. New changes, everything works BUT the lowest entry output doesnt work, it keeps saying 0 when no 0 was inputted:
Code:
```#include <stdio.h>
#include <stdlib.h>

int main ()
{
int mode[100], entry;
int counter = 0;
int total = 0;
int largest = 0;
int lowest = 0;

float average, stdev;
printf("Student Age Calculator ; -1 for Calculations \n");
printf("============================================\n\n");
printf("Input Student's Age: ");
scanf("%d", &entry);
while (entry >= 0)

{
total = total + entry;
mode[100] = entry;
counter = counter +1;
printf("Input Student's Age: ");
scanf("%d", &entry);
}

average = (float)total / (float)counter;

for( entry = 0 ; entry < 100 ; ++entry)
{
if( mode[100] >= largest)
largest = mode[100];
else if(mode[100] <= lowest )
lowest = mode[100];
}

printf("\nThere were %d ages recorded. \n", counter);
printf("The Average age is: %.2f \n", average);
printf("The Oldest age is: %d \n", largest);
printf("The Youngest age is: %d \n", lowest);

system("PAUSE");
return 0;
}```

7. Because you initialized it to zero. Change the initialization of lowest to something large.

8. still unable to get lowest/largest to work

Code:
```#include <stdio.h>
#include <stdlib.h>

int main()

{
int mode[100], entry;
int counter = 0;
int total = 0;
int largest = 0;
int lowest = 100;

float average, stdev;
printf("Student Age Calculator ; -1 for Calculations \n");
printf("============================================\n\n");
printf("Input Student's Age: ");
scanf("%d", &entry);
while (entry >= 0)

{
total = total + entry;
mode[counter] = entry;
counter = counter +1;
printf("Input Student's Age: ");
scanf("%d", &entry);
}

average = (float)total / (float)counter;

for( entry = 0 ; entry < 100 ; ++entry)
{
if( mode[entry] >= largest)
largest = mode[entry];

else if(mode[entry] < lowest)
lowest = mode[entry];
}

printf("\nThere were %d ages recorded. \n", counter);
printf("The Average age is: %.2f \n", average);
printf("The Oldest age is: %d \n", largest);
printf("The Youngest age is: %d \n", lowest);

system("PAUSE");
return 0;
}```

9. Code:
`entry < 100`
Psychic? How do you know they entered 100 numbers?