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

Asked

Viewed 72 times

0

Okay, this code is part of an attempt to solve an exercise. My goal is to convert all values from Kelvin to Celcius or vice versa and to present all values entered by the user whether they are temperature values in Celcius or Kelvin. I have already created 2 arrays where I store the inserted value. The problem is in output the values pass forward positions. Output print: output. Ex : Inserting 3 temperatures in Celcius in the output appear in the 0.1.2 place and then if we insert temperature in Kelvin these will start to appear in the 3 position 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]);
}   
}
  • 1

    As you put the temperatures in one or another array, depending on the unit of temperature, but with the variation of the same index for the 2 arrays then you will have absence of data in one or another array. I believe that it would be better for you to control the indexes separately for each of the arrays (I believe that this would be the purpose given to the use of the elementNumbers parameter.

  • @Right anonymity and how would it be possible to control the indices separately? I have to create 1 Loop FOR before each "IF" for each temperature thus getting 2 FOR Loops ?

1 answer

0

Two control variables are missing, one for the number of items inserted in the C array, and the other for the Kelvin array. Try this way:

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]);
    }
}
  • Thank you very much , it worked perfectly ! I had never thought of this alternative and was complicating trying in other ways without being necessary . Once again I thank you for your help .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.