I don't have much C experience or knowledge and I got stuck with some homework problem,
I try to build a program to find the min and max of an array using (3/2)*n
comparisons, I went through a lot of Q&A here and it helped a lot, now everything with it is ok other than a weird problem I can't seem to solve.
When I try to compare if(a[0]>a[1]) or if(*(a)>*(a+1))
everything is ok,
when I try to use else after those phrases or try to if(a[1]>a[0])
or if(*(a+1)>*(a))
then the program dies.
#include <stdio.h>
#include <stdlib.h>
void maximum(int *a, int n, int *max, int *min);
int main()
{
int i;
int number;
int *max;
int *min;
int *a;
puts("hello, pls enter the number of numbers");
scanf("%d",&number);
a = (int*)calloc(number,sizeof(int));
puts("enter the numbers");
for(i=0;i<number;i++)
{
printf("%d",i);
puts("before");
scanf("%d",&a[i]);
puts("after");
}
puts("ok");
maximum(a,number,max,min);
printf("min is %d, max is %d",*min,*max);
return(0);
}
void maximum(int *a, int n, int *max, int *min)
{
int i;
int temp;
int tempmax;
int tempmin;
if(n==0)
{
puts("come on, be serius");
return(0);
}
if(n==1)
{
puts("the number you entered is the min and the max but where is the challenge?");
*min=*max=a[0];
}
puts("check3");
**if(*(a+1)>*(a))
{
*max=a[1];
*min=a[0];
}**
/*if(a[0]>a[1])
{
*max=a[0];
*min=a[1];
}else
{
*max=a[1];
*min=a[0];
}
for(i==2;i<(n-2);i=i+2)
{
if(a[i]>a[i+1])
{
tempmax=a[i];
tempmin=a[i+1];
}else
{
tempmin=a[i];
tempmax=a[i+1];
}
if(tempmax>*max)
*max=tempmax;
if(tempmin<*min)
*min=tempmin;
}
puts("check5");
if((n%2)==1)
{
if(a[n-1]<*min)
*min=a[n-1];
if(a[n-1]>*max)
*max=a[n-1];
}/*else
{
if(a[i]>a[i+1])
{
tempmax=a[i];
tempmin=a[i+1];
}else
{
tempmin=a[i];
tempmax=a[i+1];
}
if(tempmax>*max)
*max=tempmax;
if(tempmin<*min)
*min=tempmin;
}
*/
}