2

I have written a program that asks the user the input the high and low temperature over the course of four days. Following this, the program calculates the mean temperature using the inputs from all four days. Everything is working fine however, I need to have the program determine and output the greatest high temperature and the day it occurred on as well as the smallest low temperature and the day it occurred on. Here's my code so far

#include <stdio.h> #define NUMS 4 int main (void) { int high[NUMS]; int low[NUMS]; const int MAX = 40; const int MIN = -40; int totalhigh; int totallow; int sum; float avg; printf ("---===IPC Temperature Analyzer ===---\n"); printf ("Enter the high value for day 1: "); scanf ("%d", &high[0]); printf ("Enter the low value for day 1: "); scanf ("%d", &low[0]); while (high[0] > MAX || low[0] < MIN || high[0] < low[0]) { printf ("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.\n"); printf ("Enter the high value for day 1: "); scanf ("%d", &high[0]); printf ("Enter the low value for day 1: "); scanf ("%d", &low[0]); } printf ("Enter the high value for day 2: "); scanf ("%d", &high[1]); printf ("Enter the low value for day 2: "); scanf ("%d", &low[1]); while (high[1] > MAX || low[1] < MIN || high[1] < low[1]) { printf ("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.\n"); printf ("Enter the high value for day 2: "); scanf ("%d", &high[1]); printf ("Enter the low value for day 2: "); scanf ("%d", &low[1]); } printf ("Enter the high value for day 3: "); scanf ("%d", &high[2]); printf ("Enter the low value for day 3: "); scanf ("%d", &low[2]); } printf ("Enter the high value for day 4: "); scanf ("%d", &high[3]); printf ("Enter the low value for day 4: "); scanf ("%d", &low[3]); while (high[3] > MAX || low[3] < MIN || high[3] < low[3]) { printf ("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.\n"); printf ("Enter the high value for day 4: "); scanf ("%d", &high[3]); printf ("Enter the low value for day 4: "); scanf ("%d", &low[3]); } totalhigh = high[0] + high[1] + high[2] + high[3]; totallow = low[0] + low[1] + low[2] + low[3]; sum = totalhigh + totallow; avg = sum/8.0; printf ("The average (mean) temperature was: %.2f\n", avg); if (high[0] > high[1] || high[0] > high[2] || high[0] > high[3]) { printf ("The highest temperature was %d, on day 1\n", high[0]); } else if (high[1] > high[0] || high[1] > high[2] || high[1] > high[3]) { printf ("The highest temperature was %d, on day 2\n", high[1]); } else if (high[2] > high[0] || high[2] > high[1] || high[2] > high[3]){ printf ("The highest temperature was %d, on day 3\n", high[2]); } else { printf ("The highest temperature was %d, on day 4\n", high[3]); } return 0; } 
7
  • 6
    You should get your head around the concept of a loop. Commented Feb 9, 2017 at 2:04
  • 1
    "temperatures must be in the range -40 to 40" -- so you don't want Inuits to use your program in the winter or people living in the Sahara to use it much at all? Commented Feb 9, 2017 at 2:09
  • You need not to use scanf("%d", &high[0]); simply scanf("%d", high[0])' is enough as an array is like a pointer (I said like). For example, a = &a[0]; Commented Feb 9, 2017 at 2:37
  • 1
    Good to qualify user input, yet by @JohnColeman, perhaps the range should be -90 to 57 per Extremes on Earth? Commented Feb 9, 2017 at 2:46
  • 1
    You don't have any code that tries to find the highest and lowest temperatures. Commented Feb 9, 2017 at 2:49

2 Answers 2

1

Your current code can use a loop and a helper function, which would shorten your code by reducing all those scanf() calls. You could also abstract a lot more, by using more functions, but it will show the general idea.

It is also good to check the result of scanf(), just in case the user enters a non-integer.

Your current code could look like this:

#include <stdio.h> #include <stdlib.h> #define NUMS 4 /* takes a pointer to a number */ void get_input(int *temp) { if (scanf("%d", temp) != 1) { printf("Invalid temp entered\n"); exit(EXIT_FAILURE); } } int main(void) { int high[NUMS]; int low[NUMS]; const int MAX = 40; const int MIN = -40; int day = 1, totalhigh = 0, totallow = 0, sum; float avg; for (size_t i = 0; i < NUMS; i++) { printf ("Enter the high value for day %d: ", day); /* takes the address of the pointer given by get_input() */ get_input(&high[i]); printf ("Enter the low value for day %d: ", day); get_input(&low[i]); while (high[i] > MAX || low[i] < MIN || high[i] < low[i]) { printf ("Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low.\n"); printf ("Enter the high value for day %d: ", day); get_input(&high[i]); printf ("Enter the low value for day %d: ", day); get_input(&low[i]); } day++; } for (size_t i = 0; i < NUMS; i++) { totalhigh += high[i]; totallow += low[i]; } sum = totalhigh + totallow; avg = sum/8.0; printf ("The average (mean) temperature was: %.2f\n", avg); return 0; } 

In terms of finding the largest and smallest temperatures, here is a method you can use:

  • Set max and min to the first element of your array, array[0].
  • loop from i=1 to i=n.
  • If and element if bigger than max, set max to array[i]. If an element is smaller than min, set min to array[i].
  • The day for the highest and lowest temperatures will be i+1.

Since doing something like this will help you understand loops better, I decided to just describe the steps. The above code was just an improvement on your current code, and showing you a easier way to do it will show you a different perspective on how to do problems like these.

Sign up to request clarification or add additional context in comments.

2 Comments

This does look a lot simpler than what I did. Thanks for this, I will use it as a reference while I study arrays. Would you mind further explaining the part that occurs prior to int main void? I've never come across it yet so I'm a little confused as to what's going on in it.
@Jinto Its just a helper function that does scanf() for you. It helps to stop writing scanf() everywhere. Instead this function passes a pointer, so scanf() doesnt require a &. You will understand it more once you understand arrays and pointers. You dont have to have this function, you can simply use scanf() in the loop. The main thing that will help your code is using a loop instead.
0

I updated my code to have the if statement mentioned in my above code to function correctly. Here it is:

 if (high[0] > high[1] && high[0] > high[2] && high[0] > high[3]) { // Check to see if day 1 has the highest temperature against days 2,3 and 4. printf ("The highest temperature was %d, on day 1\n", high[0]); // Output day 1 as the highest temperature and indicate the temperature value. } else if (high[1] > high[0] && high[1] > high[2] && high[1] > high[3]) { // Same function as the above function for day 1 except this is used for day 2. printf ("The highest temperature was %d, on day 2\n", high[1]); // Refer to day 1 printf } else if (high[2] > high[0] && high[2] > high[1] && high[2] > high[3]){ printf ("The highest temperature was %d, on day 3\n", high[2]); } else { printf ("The highest temperature was %d, on day 4\n", high[3]); } // Switch out high values with low values in order to determine lowest temperature and its corresponding day. 

1 Comment

you really must use a loop

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.