Since last practice didn’t go well, I was very motivated to let go of the code and start a new one, but I decided to stay and complete the code. I can’t level myself up if i can’t even go pass the loops and arrays.
First I learned about the below function.
#define MAX_NUMBER 7
I had no idea about the differences between char, define, and const function, but Sololearn website explained it well:
The #define directive uses no memory and is used by the preprocessor to manipulate the source code before compilation. The const function is used to prevent the source code from changing the value of a certain identifier, but in doing so it requires memory.
i seeeeeeeee đź‘€
This time I borrowed a lot from ChatGPT but I think now I understand the concept. Below is the code I finished.
#include<stdio.h>
#define MAX 7
int main(){
int hours[MAX]; //array of int to store up to 7
int sum = 0;
printf("==============================================\n");
printf("welcome to weekly average sleep time generator!\n");
printf("write down your sleep hours for each day of %d days: \n", MAX);
for (int i = 0; i < MAX; i++){
printf("Enter hours for day %d: ", i + 1);
scanf("%d", &hours[i]);
sum+= hours[i];
}
printf("\nAmount of hours you entered:\n ");
for (int i= 0; i<MAX; i++){
printf("%d\n", hours[i]);
}
printf("==============================================\n");
printf("The average amount of sleep you had this past week was: ");
printf("%.2f\n", (float)sum/MAX);
printf("==============================================\n");
return 0;
}
I like adding the “ ==========” because it makes the user interaction way easier to see.
below is the total user interaction and the output:
==============================================
welcome to weekly average sleep time generator!
write down your sleep hours for each day of 7 days:
Enter hours for day 1: 5
Enter hours for day 2: 6
Enter hours for day 3: 10
Enter hours for day 4: 8
Enter hours for day 5: 9
Enter hours for day 6: 6
Enter hours for day 7: 6
Amount of hours you entered:
5
6
10
8
9
6
6
==============================================
The average amount of sleep you had this past week was: 7.14
==============================================
Idk why the first number is indented but I’m too lazy to find out. Will do some time later.
Have a nice week and sleep well đź’¤