0

function hot_days(), that has two parameters: the number of temperatures for the current month and an array in which the temperatures are stored. Search through the temp array and count all the days on which the noon temp exceeds 32. Return this count.

check my hot_days function in the end of the code. the icounter is not working and i think the problem is in the x < 32.

#include <stdio.h>
    #include <conio.h>
    #define HIGH 32

    //function prototypes
    void read_temps(int []);

    void hot_days(int [], int []);

    int main()
    {


        //Array
        int TempsAry[31];
        int hotAry[31];

        //sending array 
        read_temps(TempsAry);

        //sending hot array
        hot_days(TempsAry, hotAry);






    getch();
    return 0;
        }

void read_temps(int TempsAry [])
{

      // variables
        int tempnum ;
        int x = 0 ;
        int icount = 0;


        while (x < 31)

    {
        // entering temperature

        printf("Please enter today's temperature:\n ");
        scanf("%d",&tempnum);

        if (tempnum <= -500)
        {
           break;         
        } 

         TempsAry[x] = tempnum;                   
         ++x;
         icount = icount +1;
      }

        //outputting array      
        for (x = 0 ; x<icount; ++x) 
        {
        printf("%d\n",TempsAry[x]);
        }
}

 void hot_days(int TempsAry [], int hotAry [])
 {
    int x = 0 ;
    int icount = 0;

    while (x<31)
    {
          if (TempsAry[x] > HIGH)
          {
            hotAry[x] = TempsAry[x];
            ++icount;              
          }

          ++x;   
    }

    printf("%d\n", icount);  









 }
user3483718
  • 15
  • 1
  • 1
  • 7
  • can you give the input and output you are getting and expecting. – LearningC Apr 03 '14 at 05:35
  • Clear the buffer after you read with scanf. – this Apr 03 '14 at 05:39
  • am just getting a random number in the icounter variable and am expecting it to count how many temperatures are over 32. – user3483718 Apr 03 '14 at 05:40
  • In temps_array() function you read the temps and print that array. there temps array is having right values? – LearningC Apr 03 '14 at 05:42
  • @user3483718, Please provide sample input and desired output, as well as output that you are getting. – merlin2011 Apr 03 '14 at 05:52
  • yeah my temps array is all good. i can put as many temperatures i want as long as it is below 31 days. in my second function my she asking us to use the numbers we got in the first array and to check if any temperature is higher then 32 and to count how many. – user3483718 Apr 03 '14 at 06:01
  • I tried your code, and it works quite fine. What the exact problem?? http://ideone.com/e.js/R68XwR – Suryavanshi Apr 03 '14 at 06:05

4 Answers4

0

You have uninitialized values in TempsAry. You are getting undefined behavior.

Change the line

int TempsAry[31];

to

int TempsAry[31] = {0};

which is equivalent to initializing with TempsAry with 31 comma separated zeros. More on that can be found at How to initialize an array in C

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

There is nothing wrong in this code, its working properly. checkout the snippet at ideone.

Suryavanshi
  • 595
  • 1
  • 7
  • 24
0
while (x < 31)

    {
        // entering temperature

        printf("Please enter today's temperature:\n ");
        scanf("%d",&tempnum);

        if (tempnum <= -500)
        {
           break;         
        } 

here the loop breaks if you enter temprature <=-500. So if you entered value satisfying this condition as i'th temperature then the array temp_array[] will have garbage values after temp_array[i] since they will not be assigned value. This can result in your program to give random icount.
So make those remaining temp_array[] values 0.

LearningC
  • 3,182
  • 1
  • 12
  • 19
0

You have a Sight issue with your code, If you're entering numbers less than 31, because the memory allocated for the array TempsAry[] contain raw values .So in function

void hot_days(int TempsAry [], int hotAry [])

the while loop

while (x<31)
{
      if (TempsAry[x] > HIGH)
      {
        hotAry[x] = TempsAry[x];
        ++icount;              
      }

      ++x;   
}

compares up to 31 elements even if the number of elements is less than 31, So this also includes the garbage values in the uninitialized memory, so the result obtained will be wrong

This can be eliminated by initializing the memory allocated for TempsAry to 0 in the main() function

int TempsAry[31];
int hotAry[31];
memset(TempsAry,0x00,sizeof(TempsAry));
Sorcrer
  • 1,634
  • 1
  • 14
  • 27