100k views
2 votes
Write a C++ program that defines three 1D arrays with 21 elements in each array. The arrays will be used to store temperatures in degrees F, degrees C, and Kelvin. Use a loop to initialize the array with degrees C values.

User Bart M
by
5.7k points

1 Answer

1 vote

Answer:

The cpp program is given below.

#include <stdio.h>

#include <iostream>

using namespace std;

int main()

{

//variables and arrays declaration

int size = 21;

float temp_c[size];

float temp_f[size];

float temp_k[size];

for(int j=0; j<size; j++)

{

temp_c[j] = j+1;

}

for(int j=0; j<size; j++)

{

temp_f[j] = (1.8*temp_c[j]) +32;

temp_k[j] = temp_c[j] +273.15;

}

std::cout << "Celcius Fahrenheit Kelvin" << std::endl;

for(int j=0; j<size; j++)

{

std::cout << temp_c[j] << "\t" << temp_f[j] << "\t" << temp_k[j] << std::endl;

}

//program ends

return 0;

}

OUTPUT

Celcius Fahrenheit Kelvin

1 33.8 274.15

2 35.6 275.15

3 37.4 276.15

4 39.2 277.15

5 41 278.15

6 42.8 279.15

7 44.6 280.15

8 46.4 281.15

9 48.2 282.15

10 50 283.15

11 51.8 284.15

12 53.6 285.15

13 55.4 286.15

14 57.2 287.15

15 59 288.15

16 60.8 289.15

17 62.6 290.15

18 64.4 291.15

19 66.2 292.15

20 68 293.15

21 69.8 294.15

Step-by-step explanation:

1. The integer variable holds the number of elements to be stored in each array. All arrays have size of 21.

2. Three arrays of float datatype are declared.

3. Inside first for loop, the elements of temp_c[] are initialized with values beginning from 1, in increasing order.

4. The second for loop is used to convert temperature from Celcius to Fahrenheit and store in temp_f[] array, and convert temperature from Celcius to Kelvin and store in temp_k[] array. The conversion is done from Celcius to Fahrenheit and Kelvin using the standard formulae.

5. Inside third for loop, the temperatures in Celcius, Fahrenheit and Kelvin are displayed.

6. The program successfully executes and terminates with return statement.

7. The values assigned to temp_c[] can be changed for testing.

8. The variable and arrays are declared inside main().

9. Every statement beginning with keyword cout ends with keyword endl.

10. Class is not taken since not mentioned; all other requirements are fulfilled by the program.

User Rick Kierner
by
6.1k points