Oh hi. I'm currently trying to level up my C skills and got stuck.
I'm trying to write a program that will allow a user to enter infinite numbers (one at a time) with a way to stop entering values, and once it stops it immediately displays:
Lowest number:
Highest number:
Number of values entered:
Average of numbers:
I'm pretty new to C and programming in general and what I can't figure out is:
-how to end the loop with something other than a number, preferably a char, so that it doesn't affect the average/lowest/highest
-how to output the lowest and highest numbers in a loop (would be easier to figure out were it finite and I could just type a bunch of printf/scanf)
What I have so far:
Code:#include <stdio.h>#include <stdlib.h> #define pause system("pause") main(){ //n = amount of numbers input //x = number given by user //s = smallest number //l = largest number int x = 1, n = -1, s = 999, l = -1; float sum = -1; while(x != 0){ printf("Enter a number: (0 to quit)\n"); scanf("%d", &x); sum += x; n = n + 1; } printf("Numbers entered: %i \n", n); if(x < s){ s = x; printf("Smallest number: %i \n", s); } if(x > l){ l = x; printf("Largest number: %i \n", l); } printf("Average: %.2f \n", sum/n); pause; }



LinkBack URL
About LinkBacks



I used to be an adventurer like you... then I took an arrow to the knee.