Skip to content

Instantly share code, notes, and snippets.

@peterooch
Created July 23, 2018 18:42
Show Gist options
  • Save peterooch/97e08c808d9ac8c2596ebd2129a8f5ed to your computer and use it in GitHub Desktop.
Save peterooch/97e08c808d9ac8c2596ebd2129a8f5ed to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
bool condition = false;
double *scores = NULL;
double score = 0.0;
double sum = 0.0;
int counter = 0;
int i = 0;
printf("%s", "How many scores you want to enter?\n");
scanf_s("%d", &counter, sizeof(int));
scores = malloc(sizeof(double) * counter);
if (!scores)
return false; // Memory error
counter = (counter < 0) ? -counter : counter; //To avoid PEBKAC issues
/* Part 2 of "Abstraction" */
do {
condition = true;
/* Part 1 ("Abstraction") "unknown" to the outside of the program, though the variables are known... */
do
{
printf("%s", "Please enter a score:\n");
scanf_s("%lf", &score, sizeof(double));
/* Check if the number is outside its bounds */
if (score < (double)0 || score > (double)100)
printf("%s", "Please put a number between 0 to 100\n");
else // Good number
condition = false;
} while (condition);
/* End of Part 1 */
printf("Received score %d out of %d\n", i + 1, counter);
scores[i] = score;
sum += score;
i++;
} while (i < counter);
/* End of Part 2 */
/* Dump scores */
for (i = 0; i < counter; i++)
{
printf("%.2lf\t", scores[i]);
}
printf("\nAverage is %.2lf", sum / counter);
getchar();
getchar(); //cuz bugs
free(scores);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment