# Thread: Max&Min values of a given array

1. ## Max&Min values of a given array

I have been using this function to calculate the maximum and minimum value of integers of a given array. However this code only prints the last integer that I put in. I think it is a problem with where I have the loop but I am not sure.

int max_function()

{

char std_array3[20];
int x;
int a=0;
int b=0;
int max;
bool quit = false
do

{

scanf("%s",std_array3);
for(x=0;x<1;x++)
if (!isdigit((int)std_array3[x]))
{
quit=true;
break;
}
if(!quit)
{
b=atoi(std_array3);
if(b>a){
max=b;
}
else
max=a;
}
}
while(!quit);
printf("The maximum value of integers is %i\n",max);
return 0;
}

I thought that this would work but as I said it only gives me the last integer that I typed in.....I almost missed it as the largest digit that I put in was always the biggest so I thought I was right.
Any ideas as to how to fix this error

2. >I think it is a problem with where I have the loop but I am not sure.
The problem is how you are solving the problem. If you want an array of integers, then simply declare the array as such and input integers instead of strings. It would be so much simpler if you did this. But you obviously haven't been listening to my advice since your first post, so I'll offer one last suggestion that is roughly what you wanted and then leave you to your own erroneous solutions.
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define true  1
#define false 0

int max_function ( void )
{
char std_array3[20];
int b = 0, max = 0;
int quit = false;
do
{
printf ( "Enter a number (type end to quit): " );
scanf ( "%s", std_array3 );
if ( strcmp ( std_array3, "stop" ) == 0 )
quit = true;
else {
b = atoi ( std_array3 );
if ( b > max ) max = b;
}
}
while ( !quit );
printf ( "The maximum value of integers is %i\n", max );
return 0;
}

int main ( void )
{
max_function();
return 0;
}```
-Prelude

3. You beat me to it Prelude..Here is another solution. it isn't pretty but I think it works,alittle bit of a kludge. There is stil problems if the user enters something that isn't a number. My advice would be that if you want to work with ints get the input as an array of ints

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

int max_function();

int main()
{
max_function();
return 0;
}

int max_function()
{
char std_array3[20];
char Char;
char *pChar = &Char;
int x;
int b=0;
int max = 0;
bool quit = false;

do
{
scanf("%s",std_array3);
for(x=0;x<sizeof(std_array3);x++)
{
if (!isdigit((int)std_array3[x]))
{
quit=true;
break;
}
else
{
Char = std_array3[x];
b=atoi(pChar);

if(b > max)
{
max = b;
}
}
}
}
while(!quit);

printf("The maximum value of integers is %d\n",max);
return 0;
}```

4. No prelude I completely agree with you in this case with the strings and integers as it is far easier, but the reason I am asking is because it is a specification that is required of me.
And you are right it makes it much harder, but that is the way it is, thanks for your help though.

5. >but the reason I am asking is because it is a specification that is required of me.
Okay, that makes more sense. If mine and Barjor's most recent solutions don't work for you, please show us the exact requirements you were given so that we can better solve the problem.

-Prelude

Popular pages Recent additions