void maximum(double *max, double x[])
{
int i;

for( i = 0; i<100; i++)
{
if(max < x[i])
max=x[i];
}
}

Why isnt this working
Im probrably missing one syntaz that i cant think of at the moment

Thank u

Dani AI

Generated

The bug is that the function compares the pointer itself to a double value instead of the value it points to. A pointer parameter must point at valid storage and you must dereference it to read or write the value (as hinted). Also prefer passing the array length instead of hard-coding 100, and you do not need to malloc a single double — simply pass the address of a local variable (as suggested).

A clearer, less error-prone pattern is to return the maximum value:

double maximum(const double a[], int n)
{
    if (n <= 0) return 0.0; /* handle empty array as you need */
    double m = a[0];
    for (int i = 1; i < n; ++i)
        if (a[i] > m) m = a[i];
    return m;
}

If you prefer an out-parameter, check pointers and initialize before comparing:

void maximum_out(double *max, const double a[], int n)
{
    if (max == NULL || n <= 0) return;
    *max = a[0];
    for (int i = 1; i < n; ++i)
        if (a[i] > *max) *max = a[i];
}

Call example: declare double m; maximum_out(&m, arr, n); — no malloc required for m.

Additional tips: avoid magic constants (use an n argument), always check n for empty arrays, prefer const for input arrays, and use lower-bound checks in chains (e.g., if (score >= 90) ... else if (score >= 80) ...) to make grade ranges simpler and less error-prone. For printing pointers use %p and print the dereferenced value with the matching format specifier. These changes will make the code robust and easier to understand; ’s Grades function can be simplified along these lines.

Recommended Answers

All 4 Replies

When you say double *max, the variable max contains the address. To print the value you have to write *max .

PS I hope you have malloced memory for max

int main();
{
    int x =10;
    int *p = &x;

   printf("%d\n",*p);           // This gives 10
   printf("%u\n",p);            // This gives an address

   return 0;
}

PS I hope you have malloced memory for max

Why? Can't you just create the variable using double max; and pass in the address?

@Walt
Yes this could be another way to do it

Thanks
i did this

void Grades (int *Acounter,int *Bcounter,int *Ccounter,int *Dcounter,int *Ecounter, double x[])
{

int A=0,B=0,C=0,D=0,E=0;
int i;
for(i = 0; i<100 ; i++)
{
if(x[i] >= 90 && x[i] <= 100)
A++;
else if (x[i] >= 80 && x[i] <= 90)
B++;
else if (x[i] >= 70 && x[i] <= 80)
C++;
else if (x[i] >= 60 && x[i] <= 70)
D++;
else if (x[i]<60)
E++;
}
*Acounter = A;
*Bcounter = B;
*Ccounter = C;
*Dcounter = D;
*Ecounter = E;
}

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.