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

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    3

    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. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,552
    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. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    953
    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.)
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,784
    Quote Originally Posted by john.c View Post
    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. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    953
    Quote Originally Posted by Hodor View Post
    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.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,784
    Quote Originally Posted by john.c View Post
    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. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,784
    Quote Originally Posted by john.c View Post
    (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. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    953
    Quote Originally Posted by Hodor View Post
    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.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-11-2013, 10:57 PM
  2. Finding substrings with arrays...need help
    By dcwang3 in forum C Programming
    Replies: 13
    Last Post: 02-20-2008, 01:11 AM
  3. Finding the sum of even numbers using arrays
    By Fox101 in forum C Programming
    Replies: 7
    Last Post: 12-03-2007, 02:20 PM
  4. Finding What Functions Are In A DLL/Lib?
    By Geolingo in forum Windows Programming
    Replies: 2
    Last Post: 09-27-2003, 09:58 PM
  5. Help with finding median with arrays.
    By Niloc1 in forum C Programming
    Replies: 3
    Last Post: 03-26-2002, 03:32 PM

Tags for this Thread