Link to home
Start Free TrialLog in
Avatar of NatashaMac
NatashaMac

asked on

problem with c program

Hi
i have written a program but am stuck with one last bit.
i need to display minuses and pluses for the temps entered. see below for example
-5 -----

-1 -

0

1 +

10 ++++++++++

12 ++++++++++++

14 ++++++++++++++

22 ++++++++++++++++++++++

I have it working so for all positives numbers the pluses will be displayed but i cant get the minuses to display
If you run the program and enter the temps both positive and negative you will see only the pluses display and not the minuses.

Can someone help me get the minuses to display.  Code is attached

Thanks
#include <stdio.h>
#include <stdlib.h>

main()
{
    int menu();
    void input_temps(int temp_array[], int n);
    void display_temps(int temps[], int num);
    void calculate_avg( int temps[], int no_of_temps);
    void calculate_min_max(int temps[], int no_of_temps);
    void convert_to_fahrenheit( int temps[], int no_of_temps);
    void sort_temps(int temps[], int no_of_temps);
    void temps_graph(int temps[], int no_of_temps);

    int* temps;
    int no_of_temps, no_of_bytes;
    int option;

        printf("Enter the Number of Temperature Readings  ");
        scanf("%d", &no_of_temps);
        no_of_bytes=no_of_temps*sizeof(int);
        temps=(int*)malloc(no_of_bytes);

        input_temps(temps, no_of_temps);


        display_temps(temps,no_of_temps);

 do
 {
    option=menu();

    switch (option)
    {
    case 0:
      break;
    case 1:
        calculate_avg(temps,no_of_temps);
        break;
    case 2:
        calculate_min_max(temps,no_of_temps);
        break;
    case 3:
        convert_to_fahrenheit(temps, no_of_temps);
    break;
    case 4:
        sort_temps(temps, no_of_temps);
    break;
    case 5:
        temps_graph(temps, no_of_temps);
    break;

    }
}
    while (option!=0);

}

int menu()
{
    int opt;
    printf("\n");
    printf ("1. Calculate the average temperature \n");
    printf ("2. Calculate the minimum and maximum temperature \n");
    printf ("3. Display the temperatures in Fahrenheit \n");
    printf ("4. Sort the temperatures \n");
    printf ("5. Display the Celcius temperatures graphically \n");
    printf ("0. Exit\n\n");
    scanf  ("%d",&opt);
    printf("\n");
    return opt;
}
void input_temps(int temp_array[], int n)
{
    int i;
    printf("Enter %d temperatures \n\n",n);
    for (i=0; i<n; i++)
      scanf("%d", &temp_array[i]);
}
void display_temps(int temps[], int num)
{
    int i;
    printf("\n\nThe temperatures you entered are ");
    for (i=0;i<num;i++ )
    printf("%d, ",temps[i]);

    printf("\n\n");
}

void calculate_avg(int temps[], int no_of_temps)
{
int i, total;
float avg;
total = 0;

for(i = 0; i <no_of_temps; i++)
{
total += temps[i];

}

avg = (float)total/(float)no_of_temps;

printf("\n\nThe average temperature is  %.2f" , avg);
printf("\n\n");
}

 void calculate_min_max(int temps[], int no_of_temps)
{
  int max = temps[0];
  int min = temps[0];
  int i;

  for (i = 0; i < no_of_temps; i++)
     {
      if (temps[i] > max)
        {
          max = temps[i];
        }
      else if (temps[i] < min)
        {
          min = temps[i];
        }
    }
  printf ("\n\nThe maximum Temperature is : %d\n", max);
  printf ("The minimum temperature is : %d\n", min);
  printf("\n\n");

}
void convert_to_fahrenheit(int temps[], int no_of_temps)
{
    int i;
    double f;

    for (i = 0; i < no_of_temps; i++)

{
         f=temps[i]*9.0/5.0+32.0;

printf("\n\nThe temperature %d in Celsius is %lf in Fahrenheit\n\n" , temps[i], f);

}
}

void sort_temps(int temps[], int no_of_temps)
{
   int i, temperature;
   char sorted;
        printf("Unsorted Temperatures:");
        for (i = 0; i < no_of_temps; i++)
        printf("%d ", temps[i]);

    do
    {
        sorted=1;
        for (i = 0; i < no_of_temps; i++)
        {
            if (temps[i+1] < temps[i])
            {
                temperature=temps[i];
                temps[i] = temps [i+1];
                temps [i+1]= temperature;
                sorted=0;
            }
        }
    }
    while (!sorted);

    printf("\n\nSorted Temperatures:");
    for (i = 0; i < no_of_temps; i++)
        printf ("%d ", temps[i]);
    printf ("\n\n");
}
void temps_graph(int temps[], int no_of_temps)
{
    int i;
    char symbol;

    for (i = 0; i < no_of_temps; i++)
{


    printf("\n%d ",temps[i]);

    for (symbol = 0; symbol < temps[i]; symbol++)
    {
        {
            if (symbol<0)
            {
            putchar ('-');
            }
            else if (symbol>0)
            {
                putchar ('+');
            }
        }

    }
}
}

Open in new window

Avatar of Let_Me_Be
Let_Me_Be
Flag of United States of America image

Variable symbol is a char, you are comparing it to an int. Chars can or cannot contain negative values depending on the platform you are using (most likely, on your platform they can't). In both cases, they can only contain 256 values.
Use an int instead.
Actually its not guaranteed that the char will only store 256 values, but this is true for every platform I have every seen.
Avatar of evilrix
<slightly off topic but useful to know>

An interesting link about char types.
http://www.trilithium.com/johan/2005/01/char-types/

</slightly off topic but useful to know>
Avatar of NatashaMac
NatashaMac

ASKER

Hi

I have changed it to an int.  but how do i place the char's - and + in?
> but how do i place the char's - and + in?

You do that already. Or is it still not working?
ASKER CERTIFIED SOLUTION
Avatar of milindsm
milindsm
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> for (symbol = 0; symbol < temps[i]; symbol++)

This is comparing signed and unsigned values, which can lead to unexpected results.
Oh sorry I just realized, that you not working with the value correctly.

int value = temps[i];
char symbol = '+';

if (value < 0)
{ symbol = '-'; value*=-1; }

for (int i = 0; i < value; i++)
  putchar(symbol);
putchar('\n');

Open in new window

Did the code I posted work for you????
Thank you that has dont the job perfectly
If you use the code, don't forget to change symbol to int.
thanks.... :)  anyway, the code needs lots of tuning.... but u need to post another question for that with some 100000 points or something .... :P