# Help! Function question!

This is a discussion on Help! Function question! within the C Programming forums, part of the General Programming Boards category; Code: main() { int a, b, c, d; double max, min, m, M, x, y; printf("Enter four integers: "); scanf("%d ...

1. ## Help! Function question!

Code:
```main()
{
int a, b, c, d;
double max, min, m, M, x, y;
printf("Enter four integers: ");
scanf("%d %d %d %d", &a, &b, &c, &d);
printf("%d %d %d %d", a, b, c, d);
printf("\n");

max = M;
min = m;

do
{
if(M > max) max = M;
if(m < min) min = m;
printf("%d \t %d\n", max, min);
}while(m>0);

printf("\n\nMaximum of a,b,c,d is: M=%d", max);

printf("\n\nMinimum of a,b,c,d is: m=%d\n\n", min);
}```
(a) In the main program scan four integers a,b,c,d. Call the
functions maximum(a,b,c,d) and minimum(a,b,c,d) to find the
maximum (M) and minimum (m) values among a,b,c,d.

(b) Call the function mult(M,m) which returns the power M^m,
evaluated by multiplying M by itself m times. Impose that M^0=1 for all M. If M=0 and m<0, print that 0^m = infinity.

(Do not evaluate M^m by using pow() function, or by using the for loop).

I definately have part (a) down, but part (b) is the confusing little guy. The DO command won't read the numbers because I don't know what to put before the
Code:
`y_max = M`

2. You have many problems, most of which are found simply by looking at gcc's warning output:
Code:
```gcc -o foo foo.c -Wall -O2
foo.c:4: warning: return type defaults to `int' use int main(void)
foo.c: In function `main':
foo.c:21: warning: int format, double arg (arg 2) use %f to printf doubles
foo.c:21: warning: int format, double arg (arg 3) use %f to printf doubles
foo.c:25: warning: int format, double arg (arg 2) use %f to printf doubles
foo.c:27: warning: int format, double arg (arg 2) use %f to printf doubles
foo.c:6: warning: unused variable `x' What are these for?
foo.c:6: warning: unused variable `y' What are these for?
foo.c:28: warning: control reaches end of non-void function add return 0 for success
foo.c:6: warning: `m' might be used uninitialized in this function You never initialized these
foo.c:6: warning: `M' might be used uninitialized in this function You never initialized these ```
Why do you have a do while loop that is repeating for as long as m>0 when you don't ever assign anything to m?

3. I thought that I had assigned m in min = m, but I know that was incorrect, and I don't know the correct way to assign it.

What program are you using to compile the program?

4. CWR was using gcc compiler - you can specify extra command line
arguments and it will point out more and more potential errors
in your code. I'll walk you through your code and show you
what they mean:

Code:
```main() /*Should be int main (void)...*/
{
int a, b, c, d; /*When variables are created, they are not set to zero by default*/
double max, min, m, M, x, y;
printf("Enter four integers: ");
scanf("%d %d %d %d", &a, &b, &c, &d);
printf("%d %d %d %d", a, b, c, d);
printf("\n");

max = M; /*So naturally that'll cause problems here - setting*/
min = m; /*one uninitiallised variable to another*/

do /*With no actual values in m,  M, max and min, this doesn't make any sense*/
{
if(M > max) max = M;
if(m < min) min = m;
printf("%d \t %d\n", max, min);
}while(m>0);

printf("\n\nMaximum of a,b,c,d is: M=%d", max);

printf("\n\nMinimum of a,b,c,d is: m=%d\n\n", min);
}```
The goal of your program appears to be to find the largest and
smallest of a set of numbers a, b, c, d - but you don't do any
operations. For this task, you would better to look up arrays,
rather that having 4 different variables.

Also, you did set min = m, and many more assignments llike it.
The basic problem is that there is no real value in m to assign
to min. Look over whatever material you are learning from again,
because you have some pretty serious issues with your
understanding of how to manipulate data.

Code:
```main()
{
int a, b, c, d;
double max, min, m, M, x, y;
printf("Enter four integers: ");
scanf("%d %d %d %d", &a, &b, &c, &d);
printf("%d %d %d %d", a, b, c, d);
printf("\n");

max = M;
min = m;

do
{
if(M > max) max = M;
if(m < min) min = m;
printf("%d \t %d\n", max, min);
}while(m>0);

printf("\n\nMaximum of a,b,c,d is: M=%d", max);

printf("\n\nMinimum of a,b,c,d is: m=%d\n\n", min);
}```
Originally Posted by Tarento
(a) In the main program scan four integers a,b,c,d. Call the
functions maximum(a,b,c,d) and minimum(a,b,c,d) to find the
maximum (M) and minimum (m) values among a,b,c,d.

(b) Call the function mult(M,m) which returns the power M^m,
evaluated by multiplying M by itself m times. Impose that M^0=1 for all M. If M=0 and m<0, print that 0^m = infinity.

(Do not evaluate M^m by using pow() function, or by using the for loop).

I definately have part (a) down,
You do??? Where do you "Call the functions maximum(a,b,c,d) and minimum(a,b,c,d)"? I see no functions at all. And I question whether you get correct output. You might want to finish part A before moving on.

6. Hey y'all. I think I've gotten it down a bit okay. I'm still working on Part A (Finding min and max numbers), but it's not working completely yet.

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

int maximum(int a, int b, int c, int d);
int minimum(int a, int b, int c, int d);

int main(void)
{
int a, b, c, d;
double m, M;
printf("Enter four integers: ");
scanf("%d %d %d %d", &a, &b, &c, &d);
M = maximum(a,b,c,d);
m = minimum(a,b,c,d);
printf("\nMaximum of a,b,c,d is: M=%d\n", M);

printf("\nMinimum of a,b,c,d is: m =%d\n\n", m);
}

int maximum(int a, int b, int c, int d)
{
int M;
if(a>b && a>c && a>d)
return a;
else if(b>a && b>c && b>d)
return b;
else if(c>a && c>b && c>d)
return c;
else if(d>a && d>b && d>c)
return d;
}

int minimum(int a, int b, int c, int d)
{
int m;
if (a<b && a<c && a<d)
return a;
else if (b<a && b<c && b<d)
return b;
else if (c<a && c<b && c<d)
return c;
else if (d<a && d<b && d<c)
return d;
}```
I get that M and m are some number over 100,000,000 when my integers are "1 2 3 4". What's going on?

7. Code:
`int m, M;`

8. doh. Much thanks everyone!