0

I'm fairly new to Objective C and have got an assignment that requires me to accept a series of integers from the user and give 5 pieces of information from it:

  1. Number of inputs
  2. Sum of the inputs
  3. Average of the inputs
  4. Smallest of the inputs
  5. Largest of the inputs

I've figured out the first 3 parts, but cannot think of a way to get the min/max numbers. The only way I know how to get min/max is with "if" statements, but not sure how to incorporate them into my code. Any help would be appreciated!

#include <stdio.h>

int main(int argc, const char * argv[])
{

    //variables
    int input;
    int count = -1;
    int sum = 0;
    float average = 0;

    do 
    {
        printf("Enter a number.\n");
        scanf("%i", &input);
        count++;
        sum += input;
        average = sum / count;

    }   while (input != 0);

    printf("You entered %i numbers.\n", count);
    printf("The sum of your numbers are %i.\n", sum);
    printf("The average of your numbers is %f.\n", average);

    return 0;
}

Edit: Created a new variable "prevnum" at the front of the "do...while" loop to equal the preceeding input. Used the "prevnum" variable to compare against new inputs and saved min/max values using "if...else" statements. Not sure if it's the most efficient or 'correct' way to do it, but it's functional and gives the right output. Code is as follows:

include

int main(int argc, const char * argv[]) {

//variables
int input = 0;
int count = -1;
int sum = 0;
int average = 0;
int max = 0;
int min = 0;
int prevnum;

do 
{
    prevnum = input;
    printf("Enter a number.\n");
    scanf("%i", &input);
    count++;
    sum += input;
    average = sum / count;
    if(input >= prevnum && input >= max)
        max = input;
    else if(input < max)
        max = max;
    if(input <= min && input <= max)
        min = input;
    else if(input > min)
        min = min;
}   while (input != 0);

printf("You entered %i numbers.\n", count);
printf("The sum of your numbers are %i.\n", sum);
printf("The average of your numbers is %i.\n", average);
printf("The largest number entered was %i.\n", max);
printf("The smallest number entered was %i.\n", min);
return 0;

}

1 Answers1

1

Well you could definitely do it with if statements as you explained and just have variables that track the lowest and highest values they have seen so far and update them if the current value is more appropriate.

You can also use a macro. See this answer: How would you define a simple "min" method in obj-c

Community
  • 1
  • 1
Nabren
  • 582
  • 1
  • 7
  • 17
  • Thanks! I actually figured that out before I saw your response, although I'm having a problem crafting it out. I added a "prevnum" variable that resets with every loop to be what was entered the time before. I then used "if" statements to compare the previous entry to the next one and set a min/max that way. The only problem I'm having now is that it includes the "0" that is used to stop the loop. Also, it didn't recognize a negative number as being lower than 0 either. – user2864995 Oct 10 '13 at 00:46
  • Unsure of how you are using prevnum? Storing the last value entered? If I understand your goal you only care about the lowest and biggest numbers entered period - it would have nothing to do with keeping track of the previous number. – Nabren Oct 10 '13 at 00:58
  • Had to create a variable to compare the new inputs to! Not sure if it's the 'correct' way to do it, but it's functional to the point where I get the right outputs. Will update the OP. – user2864995 Oct 10 '13 at 01:11
  • I guess the only issue I have now is the division in the 'average' result. Say I entered 4 numbers that add up to 19...the output I get is 4. I know remainders get cut off on 'int' variables, but I tried using a 'float' variable and it also gave me the same answer. Is there anyway to get a decimal answer from dividing 2 'int' variables? – user2864995 Oct 10 '13 at 01:19
  • If you are using floats, make sure you are also dividing by floats. If you divide by two integers and store it in a float, the stored value will still be an integer. Also in your example you could remove prevnum and it would function the same exact way. You don't care that the current num was bigger than the last number, you just care that it is bigger than your current max value. Also, where you are setting max = max and min = min that isn't really necessary. They will retain their value until something bigger or smaller comes along. – Nabren Oct 10 '13 at 04:07