• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

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

0
NatashaMac
Asked:
NatashaMac
  • 6
  • 3
  • 2
  • +1
1 Solution
 
Let_Me_BeCommented:
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.
0
 
Let_Me_BeCommented:
Actually its not guaranteed that the char will only store 256 values, but this is true for every platform I have every seen.
0
 
evilrixSenior Software Engineer (Avast)Commented:
<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>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
NatashaMacAuthor Commented:
Hi

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

You do that already. Or is it still not working?
0
 
milindsmCommented:
Try this,

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

int 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);

	return 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;
	char sign = '+';

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


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

	if (temps[i] < 0)
	{
		temps[i] = -1*temps[i];
		sign = '-';
	}
    for (symbol = 0; symbol < temps[i]; symbol++)
    {
		putchar(sign);
    }
}
}

Open in new window

0
 
Let_Me_BeCommented:
> for (symbol = 0; symbol < temps[i]; symbol++)

This is comparing signed and unsigned values, which can lead to unexpected results.
0
 
Let_Me_BeCommented:
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

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now