问题
I'm in a beginner C course and I was wondering if there's a way to input integers straight across and averages them together? I'm trying to make my program nice and tidy as possible.
I want to input integers straight across like:
Enter the temperatures and Enter 00 when finished:
60 80 97 42
Average is: 69.75
I don't want to input integers like shown below:
Enter the temperatures and Enter 00 when finished: 75
Enter the temperatures and Enter 00 when finished: 80
Enter the temperatures and Enter 00 when finished: 46
Enter the temperatures and Enter 00 when finished: 91
Average is: 73
回答1:
#include <stdio.h>
#include <string.h>
int main(void){
char input[64];
double ave = 0.0, value;
int count = 0;
printf("Enter the temperatures and Enter 00 when finished:\n");
while(1){
if(1==scanf("%63s", input)){
if(strcmp(input, "00") == 0)
break;
if(1==sscanf(input, "%lf", &value))
ave += (value - ave) / ++count;
}
}
if(count)
printf("Average is: %g\n", ave);
else
printf("Input one or more values\n");
return 0;
}
回答2:
Using the scanf
function any white space character is seen as the end of input for each integer. Thus using scanf
within a loop you can continuously input values within the same line.
If you want it to work for a different number of entries each time you must modify the code to use a while loop and have a dynamically allocated array, since the size is unknown. Then check for an escape sequence like 00.
All the values are stored into an array where you can do the averaging calculations
#include <stdio.h>
#define NUM_OF_ENTRIES 5
int main()
{
printf("Enter numbers: ");
int i = 0;
int value_set[NUM_OF_ENTRIES];
for (i = 0; i < NUM_OF_ENTRIES; i++ )
{
scanf("%d", &value_set[i]);
}
回答3:
I believe that you shall change your terminating condition from enter 00
to something like enter x
.
So, your code shall look like this::
int n;
int sum = 0, count = 0;
while(scanf("%d", &n)) {
sum = sum + n;
count++;
}
printf("%lf", double(sum/count));
scanf
returns the number of successfully taken inputs. Since n
is declared as int
, so everytime you enter some integer value, scanf
will return 1
and if you enter some value which is not of type int
like if you enter x
(which is a char
) scanf
will return 0
, because x
is not an integer, and this way you can calculate the average.
回答4:
Code can use scanf("%d", &number)
to read an integer. The trouble is that "%d"
first scans and discards leading white-space which includes '\n'
before scanning for an int
. '\n'
is needed to know when to stop as OP wants "input integers straight across". So instead code should look for white-space one character at a time first. Upon finding the end-of-line '\n'
, scanning is complete.
With this approach there are no practical limits to the count of numbers.
#include <ctype.h>
#include <stdio.h>
double Line_Average(void) {
double sum = 0;
unsigned long long count = 0;
while (1) {
int ch;
while (isspace(ch = fgetc(stdin)) && ch != '\n')
;
if (ch == '\n' || ch == EOF) {
break; // End-of-line or End-if file detected.
}
ungetc(ch, stdin); // Put back character for subsequent `scanf()`
int data;
if (scanf("%d", &data) != 1) {
break; // Bad data
}
sum += data;
count++;
}
return sum/count;
}
// sample usage
puts("Enter the temperatures");
double Average = Line_Average();
printf("Average is: %.2f\n", Average);
回答5:
One possibility:
double sum = 0;
double val;
size_t count = 0;
char follow;
while( scanf( "%lf%c", &val, &follow ) == 2 )
{
sum += val;
count++;
if ( follow == '\n' )
break;
}
printf( "average = %f\n", sum/count );
This will read each number plus the character immediately following, until it sees a newline or a non-numeric string. It's not perfect; if you type a number followed by a space followed by a newline, then it won't break the loop. But it should give you some ideas.
回答6:
since u did not post the code. i have given a sample code... from here u can build what u require with some tweaks
#include<stdio.h>
main()
{
int one, two, thr, four, five, avg;
printf("\nEnter the temperatures and Enter 00 when finished:");
scanf ("%d %d %d %d %d", &one, &two, &thr, &four, &five);
avg=(one+two+thr+four+five)/5;
printf("Average value is %d", avg);
}
来源:https://stackoverflow.com/questions/31664158/inputing-integers-straight-across-in-c