Skip to content

Instantly share code, notes, and snippets.

@only-entertainment
Forked from anonymous/gist:3543037
Created August 30, 2012 22:27
Show Gist options
  • Save only-entertainment/3543126 to your computer and use it in GitHub Desktop.
Save only-entertainment/3543126 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int main(int argc, char * argv[]) {
float accumulator = 0.0;
int num_of_times = 0;
int ii = 1;
float average = 0.0;
// Ask until we have a valid input
while (1) {
// Get the user input
printf("How many?: ");
scanf("%d", &num_of_times);
printf("\n");
// If number of times to run is less than 0,
// tell them, and try again
if (num_of_times <= 0) {
printf("Please enter a number greater than 0.\n\n");
} else{
// We have a valid number now, break out of this loop
break;
}
}
// Now that we have the number of times to ask
// the user for a number, do it
for (ii = 0; ii < num_of_times; ii++) {
float user_input = 0.0;
// Get the user input to add to
// the accumulator
printf("User Number: ");
scanf("%f", &user_input);
// Add the input
accumulator += user_input;
}
// Get the average
average = accumulator / (float)num_of_times;
// Display it to the user
printf("\nAverage = %f\n", average);
return 0;
}
@only-entertainment
Copy link
Author

include <stdio.h>

int main(int argc, char * argv[])

{
float acc = 0.0;
float inputN = 0.0;
int i = 0;
int N = 0;

    printf("How many?: ");
    scanf("%d", &N);
    for (i = 0; i < N; i++) {
            printf("Number: ");
            scanf("%d", &N);
            acc += inputN;
    }

    acc /= (float)N;
    printf("acc = %f\N", acc);
    return 0;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment