Well, this code is part of attempting to resolve an exercise. My goal is to convert all Kelvin values to Celcius or vice versa and display all values entered by the user whether they are temperature values in Celcius or Kelvin. For this I have already created 2 arrays where I keep the value entered. The problem is in the output the values pass positions ahead. Print the output: output . Ex: Entering 3 temperatures in Celcius in the output will appear in the 0.1.2 place and then if we insert Kelvin temperature they will start to appear in position 3 instead of starting again.
#include <stdio.h>
#include <stdlib.h>
float CalculaC(float tK);
float CalculaK(float tC);
void ShowResults(float storesC[], float storesK[], int elementNumbers);
int main(int argc, char *argv[])
{
float tC, tK, storesC[11], storesK[11], calC, calK;
int i,j;
char var;
for(i = 0; i < 10; i++)
{
printf("Insert 10 temperatures values and identify it as Celsius or Kelvin with K or C respectively:");
scanf("%f", &tC);
var = getchar();
if(var == 'C' || var == 'c')
{
storesC[i] = tC;
calK = CalculaC(storesC[i]);
printf("value: %f \n",calK); // this is just for testing if the function was working
}
else if(var == 'K' || var == 'k')
{
storesK[i] = tC;
calC = CalculaK(storesK[i]);
printf("value: %f \n",calC); // this is just for testing if the function was working
}
}
ShowResults(storesC,storesK,i);
getchar();
}
float CalculaC(float tK)
{
float temp;
temp = tK - 273.15;
return temp;
}
float CalculaK(float tC)
{
float temp;
temp = tC + 273.15;
return temp;
}
void ShowResults(float storesC[], float storesK[], int elementNumbers)
{
int j,z;
for(j = 1; j < elementNumbers; j++ )
{
printf("Value in Celcius: %f \n", storesC[j]);
}
for(z = 1; z < elementNumbers; z++ )
{
printf("Value in Kelvin: %f \n", storesK[z]);
}
}