-1

I want to be able to print out the largest age inputted by the user as well as the smallest age.

Also, I noticed my program doesn't include the numbers after the decimal point. It would just say 25.00 for example, rather than 25.25.

Any help is greatly appreciated!

#include "stdafx.h"
#include "stdio.h"

void main()
{
int age[11];
float total = 0;
int i = 1;
float average;

do
{
    printf("# %d: ", i);
    scanf_s("%d", &age[i]);
    total = (total + age[i]);
    i = i + 1;

} while (i <= 10);

average = (total / 10);
printf("Average = %.2f", average);
}
5
  • 1
    Possible duplicate of Finding Max Number in an Array C Programming Commented Oct 5, 2016 at 15:24
  • 3
    Indexes start from 0 in C. Not accepting this is not just waste of memory, but also confusing and error-prone. Commented Oct 5, 2016 at 15:31
  • Apart from not starting your array at index 0 (which BTW doesen't have any impact here) your program works fine on my computer. Commented Oct 5, 2016 at 15:33
  • What output do you get for what input ? Commented Oct 5, 2016 at 15:34
  • Please accept the answer that has helped the most. Doing so encourages users to keep helping others by being rewarded with reputation points. Commented Oct 5, 2016 at 19:50

3 Answers 3

1

I think it will be helpful. For decimal point you have to declare your array as float. FLT_MAX These macros define maximum value of a float. Before useing FLT_MAX you should inclue float.h header file.

#include <stdio.h>
#include <float.h>

int main(void)
{
float age[11];
float total = 0;
int i = 1;
float average;
float largestInput = 0.0;
float smallestInput = FLT_MAX;

do
{
    printf("# %d: ", i);
    scanf("%f", &age[i]);
    total = total + age[i];
    //here i am checking the largest input
    if (age[i]> largestInput){
     largestInput = age[i];
    }
    //here i am checking the smallest input
    if (age[i] < smallestInput) {
      smallestInput = age[i];
    }
    i = i + 1;
} while (i <= 10);

average = (total / 10);
printf("Average = %.2f\n", average);
printf("Largest Input Is = %.2f\n", largestInput);
printf("Smallest Input IS = %.2f", smallestInput);
return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Pseudo-code. Keep two extra variables. long largestInput, smalltestInput.

set largestInput = smalltestInput = age[0];

for (i = 0; i < 10; i++) {
  if age[i]> largestInput{
     largestInput = age[i];
  }
  if age[i] < smallestInput {
      smalltestInput = age[i];
  }
}

Print it as you like

2 Comments

Sorry I don't think so. What is the output? What's the content of smallestInput? What is the code you really do have?
Prosen Ghosh has used nearly exactly that code and that just works as it should So the code is quite ok
0

Your code works fine on my system. I've modified your code to not be Windows specific and shown you how to calculate the smallest and largest of the numbers entered. The idea is to constantly compare the current entry to the current smallest and largest number and change smallest and largest as needed. The code below also shows how to start at array index 0.

#include <stdio.h>


int main()
{
    int age[10];
    float total = 0;
    int i = 0;
    float average;
    int large = -9999;
    int small = 9999;

    do
    {
        printf("# %d: ", i+1);
        scanf("%d", &age[i]);       
        total = (total + age[i]);

        if( age[i] < small )
        {
            small = age[i];
        }

        if( age[i] > large )
        {
            large = age[i];
        }

        i++;

    } while (i < 10);

    average = (total / 10.00);
    printf("Smallest = %d Largest = %d \n", small, large);
    printf("Average = %.2f\n", average);
}


# 1: 30
# 2: 40
# 3: 50
# 4: 2
# 5: 3
# 6: 4
# 7: 6
# 8: -1
# 9: 4
# 10: 5
Smallest = -1 Largest = 50 
Average = 14.30

4 Comments

I'm just curious as to what #include <limits.h> means. We have notgone on to using that yet, is there another work around way of doing it? Thanks so much!
@davidbrogan It provides the INT_MIN and INT_MAX macros/defines.
@davidbrogan You could set large = -1 and small = 9999999 and not use INT_MIN or INT_MAX and not include limits.h
@davidbrogan I modified the code to not use limits.h.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.