Created
April 22, 2015 08:37
-
-
Save tudorconstantin/74c2bd8bb687c1405238 to your computer and use it in GitHub Desktop.
average_of_ten_numbers_with_validation.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Write that calculates an avereage of an | |
array of an array of 10 floating points values*/ | |
#include <stdio.h> | |
#include <stdbool.h> | |
int main(void) | |
{ | |
float array[10], array_elements_sum = 0, array_elements_avreage = 0, array_elements_temp ; | |
int i; | |
bool flag = true; | |
printf("Give the elements of the array\n"); | |
for( i = 0; i < 10; ++i) | |
{ | |
if (scanf("%f", &array_elements_temp) == 1){ | |
printf("Read elem (as float): %f\n", array_elements_temp); | |
array_elements_sum += array_elements_temp; | |
} else { | |
flag = false; | |
break; | |
} | |
} | |
if(flag) | |
{ | |
array_elements_avreage = array_elements_sum / 10; | |
printf("Average: %f\n", array_elements_avreage); | |
} | |
else | |
{ | |
printf(" This is not a number. \n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment