Use of Array

Use of a 1D array


Write a program that calculates the number of hours worked within a specific period and prints the total number of hours, the average length of a day and an itemization of the hours entered. First, the program must ask how many days of working hours shall be entered (max 30 days). After this, the program asks for the daily working hours. The program output shall have one decimal place of precision.


#include <stdio.h>

int main()
{
    printf("The program calculates the total hours worked during\na specific period and the average length of a day.\n");

    int days,i;
    float array[30],sum; //initilize array which can store 30 values or 120 bytes (4*30)
    printf("How many days:");
    scanf("%d",&days);

    for (i=0; i<days; ++i)
    {
       printf("Enter the working hours for day %d:",i+1);

       scanf("%f",& array[i]); //this line stores the values entered by the user. 
       //when i=0, it stores the first element, i=1 second element and so on......


       sum+=array[i]; //same as above gets the respective element and adds on until the for loop                                          ends
    }
    printf("Total hours worked:%0.1f\n",sum);
    printf("Average length of day:%0.1f\n",sum/days);
    printf("Hours entered:");
    for (i=0; i<days; ++i)
    {
        printf("%0.1f"" ", array[i]);
       //similarly it gets the first element when i=0, second when i=1 and so on... and prints the entered hours
    }

    return 0;

}

Comments

Popular posts from this blog

What is LDR and how it works with circuit to try on?

Use of 4 Strain Gauge Sensors and LabView DAQ to Measure and Calculate Weigth

POWER SUPPLY (5A Constant Current Source)