Need help tweaking a code - c programming

Write a program that asks the user how many numbers she would like to compare; Then accepts her inputs. The program should output the largest, the smallest and the average of all entered numbers. The results should be presented in a tabular form.


I had written a code before very similar to what this problem is asking me to do. I'm just not sure how to change it so that it allows the user to input a custom amount of numbers.

Any help would be much appreciated, thank you for your time in advanced.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
int main (void)
{
    int int1;
    int int2;
    int int3;

scanf("%d", &int1);
scanf("%d", &int2);
scanf("%d", &int3);

printf("Enter three different integers: %d %d %d", int1,int2,int3);

int sum;
sum= int1+int2+int3;

printf("\nSum is %d" , sum);

printf("\nAverage is %d" , sum/3);

printf("\nProduct is %d\n" , (int1*int2)*int3);

if ( int1 < int2 ) {
    if ( int1 < int3 ) {
      printf("Smallest is %d\n", int1 );
    }
  }

  if ( int2 < int1 ) {
    if ( int2 < int3 ) {
      printf("Smallest is %d\n", int2 );
    }
  }

  if ( int3 < int1 ) {
    if ( int3 < int2 ) {
      printf("Smallest is %d\n", int3 );
    }
  }

  if ( int1 > int2 ) {
    if ( int1 > int3 ) {
      printf("Largest is %d\n", int1 );
    }
  }

  if ( int2 > int1 ) {
    if ( int2 > int3 ) {
      printf("Largest is %d\n", int2 );
    }
  }

  if ( int3 > int1 ) {
    if ( int3 > int2 ) {
        printf("Largest is %d\n", int3 );
    }
  }

    return 0;
}
Last edited on
"I'm just not sure how to change it so that it allows the user to input a custom amount of numbers."

First do a scanf to get the number of elements.
You should create an array that is capable of storing all those elements.
Then make a for loop "for(int i = 0; i < numOfElem; ++i)" in which you would use scanf to get each of the your elements.
Btw the easiest way to get the smallest and largest number in an array is to sort it using
http://www.cplusplus.com/reference/algorithm/sort/
and just take the first and the last element of the sorted array.

If you don't know how arrays work or how to use a for loop then go to youtube and search for those tutorials. There are a lot of good videos that explain those topics.
Topic archived. No new replies allowed.