What language is this code in ?

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
#include <stdio.h>
#include <string.h>
#include <limits.h>

#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MAX_LINE_LEN 80

typedef enum { false = 0, true } bool;
int getInt(const char *p);

int main(int argc, char *argv[]){
 int i, x = 1, count = 0, smallest = LONG_MAX, largest = -LONG_MAX;
float sum = 0;

do {
 count = getInt("Enter count of numbers to be entered: ");
 } while (count < 1);
 for (i = 0; i < count; i++) {
 sum += (x = getInt("Enter a number: "));
 smallest = MIN(x, smallest);
 largest = MAX(x, largest);
 }
printf("\nSmallest number: %i \n", smallest);
 printf("Largest number: %i \n", largest);
 printf("Average: %.2f \n", sum / count);

 return 0;
}

int getInt(const char *p) {
 static char line[MAX_LINE_LEN];
 static char junk[MAX_LINE_LEN];
 bool inputOk;
int z;

 for (inputOk = false; inputOk == false; ) {
 printf("%s",p);
 fgets(line, MAX_LINE_LEN, stdin);
 *(strchr(line,'\n')) = '\0';
 inputOk = (sscanf(line,"%d%s",&z,junk) == 1);
 if (inputOk == false) {
 printf("invalid input, try again\n");
 }
 }
 return z;
}
Last edited on
C
As Peter87 says, it's C. There are a number of obvious clues:

1) It uses the old c-style I/O - printf, and sscanf.
2) The standard library header files it includes are C ones
3) The code defines a type bool. This would be illegal in C++, where bool is already a built-in type.

(I appreciate that this is probably a homework question. I'm giving you a detailed answer on the assumption that you're genuinely interested in learning, rather than just looking to cheat on your homework.)
Last edited on
Topic archived. No new replies allowed.