Language
C
Compiler
gcc 12.1.0
Options
Warnings
C11
no pedantic
//#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
int get_int(const char* x, ...) {
int a;
scanf("%d", &a);
return a;
}
char get_char(const char* x) {
return getchar();
}
float calc_hours(int hours[], int weeks, char output);
int main(void)
{
int weeks = get_int("Number of weeks taking CS50: ");
int hours[weeks];
for (int i = 0; i < weeks; i++)
{
hours[i] = get_int("Week %i HW Hours: ", i);
}
char output;
do
{
output = toupper(get_char("Enter T for total hours, A for average hours per week: "));
}
while (output != 'T' && output != 'A');
printf("%.1f hours\n", calc_hours(hours, weeks, output));
}
// TODO: complete the calc_hours function
float calc_hours(int hours[], int weeks, char output)
{
float final_hours = 0;
//TODO: Write conditional for 'T'
if (output == 'T')
{
for (int j = 0; j < weeks; j++)
{
final_hours = final_hours + hours[j];
}
return final_hours;
}
//TODO: Write conditional for 'A'
if (output == 'A')
{
for (int k = 0; k < weeks; k++)
{
final_hours = (final_hours + hours[k]) / weeks;
}
return final_hours;
}
}
3 1 2 3 T
$ gcc prog.c -Wall -Wextra -std=c11
6.0 hours
Exit Code:
0