How to store value entered by the user in 2 different arrays?

0

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]);
}   
}
    
asked by anonymous 21.12.2018 / 16:00

1 answer

0

Two control variables are missing, one for the number of items inserted into the C array, and another for the Kelvin pro array. Try this:

float tC, tK, storesC[11], storesK[11], calC, calK;
int i,j, countC = 0, countK = 0;//Contador para Celsius e Kelvin

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[countC] = tC;
        calK = CalculaC(storesC[countC]);
        countC++;
        printf("value: %f \n",calK); // this is just for testing if the function was working    
    }
    else if(var == 'K' || var == 'k')
    {
        storesK[countK] = tC;
        calC = CalculaK(storesK[countK]);
        countK++;
        printf("value: %f \n",calC);    // this is just for testing if the function was working 
    }
}

and in ShowResults:

void ShowResults(float storesC[], float storesK[], int countC, int countK)
{
    int j,z;

    for(j = 1; j < countC; j++ )
    {
        printf("Value in Celcius: %f \n", storesC[j]);
    }

    for(z = 1; z < countK; z++ )
    {
        printf("Value in Kelvin: %f \n", storesK[z]);
    }
}
    
21.12.2018 / 17:02