4. new method

The code I did on day 3 ended up like this:
#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;
}
It didn’t have any counter-measure for input errors. What if the user inputs a string or nothing?
…
…
It's been two weeks since I last wrote the code above, and I've since switched to a different approach for learning C.
I created a new file, new.c, where I test out all the functions I've learned from YouTuber Bro Code. After experimenting with them, I delete the file and start over, incorporating improved and alternative functions. This method helps me reinforce concepts through muscle memory as well.
Below is a simple program that calculates the slope of a line given two points. I'm currently focusing on ultra-basic programs, but this practice has significantly improved my thinking speed and ability to translate ideas into code.
#include<stdio.h>
#include<math.h>
int main(){
double x1, y1, x2, y2;
printf("===========================================\n");
printf("Welcome to slope calculator!\n");
printf("===========================================\n");
printf("Enter the x value for the first point: ");
scanf("%lf", &x1);
printf("Enter the y value for the first point: ");
scanf("%lf", &y1);
printf("Enter the x value for the second point: ");
scanf("%lf", &x2);
printf("Enter the y value for the second point: ");
scanf("%lf", &y2);
double slope = (y1 + y2)/(x1 + x2);
printf("===========================================\n");
printf("The slope of the line is: %.2f\n", slope);
printf("===========================================\n");
return 0;
}




