# Thread: Finding Max, Min, Sum using functions (No Arrays)

1. ## Finding Max, Min, Sum using functions (No Arrays)

Can someone please help me with this, i am seriously stuck. I did it with this method but my profs said it was wrong. I dont think Im suppose to use an array as we are not in that chapter yet.
Here is the assignment

1. In the main function you will ask the user to enter 10 positivenumber (between 0 to 100)

2. You need to create three different functions called minNumber,maxNumber and totalSum. The prototypes of the functions has tobe as follows:int minNumber (int x, int y)int maxNumber (int x, int y)int totalSum (int x, int y)

3. Your task is to invoke these above three functions from the mainfunction.

4. Finally in the main function print the minimum and maximumamong these 10 numbers and their total sum.

Code:
#include <stdio.h>

int maxNumber(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j);
int minNumber(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j);
int totalSum(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j);

int main(Void)
{
int n0;
int n1;
int n2;
int n3;
int n4;
int n5;
int n6;
int n7;
int n8;
int n9;

printf("%s", "Enter 10 positive numbers (0 to 100):");
scanf_s("%d%d%d%d%d%d%d%d%d%d", &n0, &n1, &n2, &n3, &n4, &n5, &n6, &n7, &n8, &n9);

printf("Maximum is: %d\n", maxNumber(n0, n1, n2, n3, n4, n5, n6, n7, n8, n9));
printf("Minimum is %d\n:", minNumber(n0, n1, n2, n3, n4, n5, n6, n7, n8, n9));
printf("Total is %d\n:", totalSum(n0, n1, n2, n3, n4, n5, n6, n7, n8, n9));

system("Pause");
}

int maxNumber(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j)
{
int max = a;

if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
if (d > max) {
max = d;
}
if (e > max) {
max = e;
}
if (f > max) {
max = f;
}
if (g > max) {
max = g;
}
if (h > max) {
max = h;
}
if (i > max) {
max = i;
}
if (j > max) {
max = j;
}

return max;
}

int minNumber(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j)
{
int min = a;

if (b < min) {
min = b;
}
if (c < min) {
min = c;
}
if (d < min) {
min = d;
}
if (e < min) {
min = e;
}
if (f < min) {
min = f;
}
if (g < min) {
min = g;
}
if (h < min) {
min = h;
}
if (i < min) {
min = i;
}
if (j < min) {
min = j;
}

return min;
}

int totalSum(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j)
{
int sum = a + b + c + d + e + f + g + h + i + j;
return sum;
}

2. I did it with this method but my profs said it was wrong.
He is correct your functions are all incorrect. Look at the instructions:

The prototypes of the functions has tobe as follows:int minNumber (int x, int y) int maxNumber (int x, int y) int totalSum (int x, int y)
Your functions should only take two parameters, not ten.

You're probably supposed to use some kind of loop to get the data from the user and the call the required functions.

3. Your solution is wrong because you aren't using the prescribed prototypes. You've added a bunch of parameters.

You are supposed to read the numbers one at a time in a loop. Start with the totalSum function and forget about the others for now. How you could use the totalSum function (with the prescribed prototype) to keep track of the sum. (It should be noted that this is a pretty dumb use of a function, but that's typical for assignments.)

4. Originally Posted by john.c
Your solution is wrong because you aren't using the prescribed prototypes. You've added a bunch of parameters.

You are supposed to read the numbers one at a time in a loop. Start with the totalSum function and forget about the others for now. How you could use the totalSum function (with the prescribed prototype) to keep track of the sum. (It should be noted that this is a pretty dumb use of a function, but that's typical for assignments.)
The real mystery is why is the professor asking the students to "In the main function you will ask the user to enter 10 positivenumber (between 0 to 100)"? What are these 10 numbers that are read in main meant to be used for? Decorations?

5. Originally Posted by Hodor
The real mystery is why is the professor asking the students to "In the main function you will ask the user to enter 10 positivenumber (between 0 to 100)"? What are these 10 numbers that are read in main meant to be used for? Decorations?
I assumed they are meant to be passed one at a time to the functions, kind of like:
Code:
scanf("%d", &n);
sum = totalSum(sum, n);
where totalSum simply passes back the sum of the inputs. That's the only sense I could make out of it.

6. Originally Posted by john.c
I assumed they are meant to be passed one at a time to the functions, kind of like:
Code:
scanf("%d", &n);
sum = totalSum(sum, n);
where totalSum simply passes back the sum of the inputs. That's the only sense I could make out of it.
It's a ridiculous assignment in that case

7. Originally Posted by john.c
(It should be noted that this is a pretty dumb use of a function, but that's typical for assignments.)
Pretty dumb? Nah, upon reflection and applying Occam's razor the only solution I can come to is that the professor was dropped in his/her head as an infant and suffered irreparable damage

8. Originally Posted by Hodor
Pretty dumb? Nah, upon reflection and applying Occam's razor the only solution I can come to is that the professor was dropped in his/her head as an infant and suffered irreparable damage
It's in the class of assignments I call "read the prof's mind". I have no idea if I stumbled upon the correct idea, but apropos your comment I did have to bang my head against a wall a few times before it occurred to me.

Popular pages Recent additions