That is harsh :) Yet amusing. Luckily for everyone else on the forum I am equal opportunistic with both help and insults.
Printable View
also i wanted INT_MIN and INT_MAX to print out the smallest and largest of the inputted numbers.
So you can, as you read in a number, say "Hey! Is this the smallest number?" If it is, keep hold. If not, don't. (And of course the same for largest number.) You can't have INT_MIN and INT_MAX for variable names, just like you can't have if and for. Already taken, sorry.
No arrays eh? That is even easier to code and less overhead :)
Example:
Code:/*
* This program prints the smallest number, the largest number,
* and the average of all the numbers.
*/
#include <stdio.h>
#include <limits.h>
int main(void) {
int input;
int total;
int avg;
int count;
int min = INT_MAX, max = INT_MIN;
total = 0;
count = 0;
printf("enter some numbers, and then anything else to the end:\n");
while (scanf("%d", &input) == 1) {
if(input > max)
max = input;
if(input < min)
min = input;
total += input;
// count = ++count;
++count;
printf("%d\n", input);
}
if(count == 0)
{
fputs("You are no fun. Thank you for using my program anyway...", stdout);
return 0;
}
avg = total/count;
printf("smallest value is %d\n", min);
printf("largest value is %d\n", max);
printf("total is %d\n", total);
printf("count is %d\n", count);
printf("avg is %d\n", avg);
}
Well if u arent using something as an array, everytime u use scanf for the input the previous data is deleted. So there is Abosolutely no way that ud be able to reference the back to the numbers u entered. So there, u need something to keep track of the nums, something for beginers an array id say
Actually, I don't know what has gotten into me that I am being so trusting of the user. I added the correct code to fix the above program from user error.
I initially recommended the same thing thinking that was what the professor was wanting. But luckily he just needed something simple. The making sure count is not zero is VERY important. It will not only prevent the two dummy min/max values from being printed, it will also prevent a division by zero.
^ If I wrote it I would probably have done it more than this *shrugs* its not my homework anyway.Code:/*
* This program prints the smallest number, the largest number,
* and the average of all the numbers.
*/
#include <stdio.h>
#include <limits.h>
int main(void) {
int input, total, count;
int min = INT_MAX, max = INT_MIN;
printf("enter some numbers or 'Q' to quit:\n"); /* Again... idiot proofing */
for(total = 0, count = -1, input = 0; scanf("%d", &input) == 1; ++count, total += input)
{
if(input > max)
max = input;
if(input < min)
min = input;
}
if(!count)
{
fputs("You are no fun. Thank you for using my program anyway...", stdout);
return 0;
}
printf("smallest value is %d\nlargest value is %d\ntotal is %d\ncount is %d\ncount is %davg is %d\n",
min, max, total, count, total / count);
return 0;
}