C language 1D arrays Write a declaration statement for an ar

C language 1-D arrays

Write a declaration statement for an array with 10 elements. Assume that the array is going to contain double-precision floating-point temperature data in units of Kelvins. Initialize the array with values of 0 in all elements. Write the same declaration, but for an array with n elements, where n is a pre-declared integer variable (In other words, has already been declared in advance.) Do not give the array initial values in this case. (You cannot initialize variable-length arrays in a declaration statement) Write a for loop that will run a total of n times, and in each iteration will set one of the elements of your temperature array to zero. (Remember that arrays in c go from subscript (0) to (n-1). Write a for loop that will run a total of n times, and in each iteration will ask the user for an input value and will store it in a different element of your temperature array. It should also feature a statement that adds the new element to a running sum which is stored in a separate variable called T_sum. Write a statement that calculates the percent error between a variable containing an average measured temperature and a defined symbolic constant containing the boiling point of an unspecified substance. Percent Error = |A-B/B|* 100% (B is the known constant)

Solution

PROGRAM CODE:

/*

============================================================================

Name : C.c

Author : Kaju

Version :

Copyright : This is just an example code

Description : Hello World in C, Ansi-style

============================================================================

*/

#include <stdio.h>

#include <stdlib.h>

int main(void) {

   //Question number: 1

   float kelvinTempUnits[10] = {0.00f}; //creating an array and initializing with zero

   printf(\"%.2f\",kelvinTempUnits[0]);

   //Question number: 2

   const int n = 20;

   float kelvinUnits[n];

   //Question Number: 3

   printf(\"\ [ \"); // to print and show the array

   for(int i=0; i<n; i++)

   {

       kelvinUnits[i] = 0;

       printf(\"%.2f \", kelvinUnits[i]); // to print and show the array

   }

   printf(\"]\ \"); // to print and show the array

   //Question number: 4

   float T_sum;

   for(int i=0; i<n; i++)

   {

       printf(\"Enter Temperature: \");

       scanf(\"%f\", &kelvinUnits[i]);

       T_sum = T_sum + kelvinUnits[i];

   }

   printf(\"\ %.2f\", T_sum); // printing on screen

   //Question Number: 5

   const float BoilingPoint = 100.0f; //constant B

   float AverageTemperature = T_sum/20; //Average of temperatures

   int percentError = ((AverageTemperature - BoilingPoint)/BoilingPoint)*100; //percent error calculation

   printf(\"\ %d%%\", percentError); // printing on screen

   return 0;

}

OUTPUT:

0.00

[ 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 ]

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

Enter Temperature: 123

2460.00

23%

C language 1-D arrays Write a declaration statement for an array with 10 elements. Assume that the array is going to contain double-precision floating-point tem
C language 1-D arrays Write a declaration statement for an array with 10 elements. Assume that the array is going to contain double-precision floating-point tem
C language 1-D arrays Write a declaration statement for an array with 10 elements. Assume that the array is going to contain double-precision floating-point tem

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site