Passing Variables by Value to a Function

Hello all, I was wondering if I could get some help with my code. For some reason, my variables pressure_max1, pressure_min1, temp_max1, and temp_min1 don't seem to be changing or working as I hoped they would. I'm trying to make a program where you enter 20 temperatures and 20 pressures, and it's supposed to find the largest and the smallest of the values. Usually I can work these issues out, but I really need help from the community on this one. My poor, flu-riddled brain is pulling blanks on what's going on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdio.h>

/******************************************************************************/
/*                                                                            */
/*                                main                                        */
/*                                                                            */
/******************************************************************************/
/*                                                                            */
/*   Input Variables(arrays):                                                 */
/*      float temp[20]         - Temperature for the scientific experiment    */
/*      float pressure[20]     - Pressure for scientific experiment           */
/*      ch                     - Used for exiting program                     */
/*                                                                            */
/*   Output Variables (Calculated):                                           */
/*      float temp_max         - Final max temperature                        */
/*      float pressure_max     - Final max pressure                           */
/*      float temp_min         - Final min temperature                        */
/*      float pressure_min     - Final min pressure                           */
/*                                                                            */
/*   Functions Called                                                         */
/*      float temp_extreme()     - Finds temp-max and temp_min                */
/*      float pressure_extreme() - Finds pressure_max and pressure_min        */
/*                                                                            */
/*   Variables local to temp_extreme() and pressure_extreme()                 */
/*                                                                            */
/*      float temp_max1        - Largest temperature in the temp array        */
/*      float temp_min1        - Smallest number in the temp array            */
/*      float pressure_max1     - Largest pressure in the pressure array      */
/*      float pressure_min1     - Smallest pressure in the pressure array     */
/*                                                                            */
/******************************************************************************/
float temp[20];
float pressure[20];
float temp_extreme(float, float, int);
float pressure_extreme(float, float, int);

int main()
{
    float temp_max = 0;
    float temp_min = 0;
    float pressure_max = 0;
    float pressure_min = 0;
    int ch;
    int count = 0;
    
    printf("Please input the 20 temperature and pressure readings\n");
    
    for(; count < 20; count++)
    {
              printf("Count = %d  \n", count);
              printf("Temperature: ");
              scanf("%f", &temp[count]);
              printf("Pressure: ");
              scanf("%f", &pressure[count]);
              temp_extreme(temp_max, temp_min, count);
              pressure_extreme(pressure_max, pressure_min, count);
    }
    
    printf("Lowest Temperature: %f degrees\nHighest Temperature: %f degrees\n Lowest Pressure: %f \n Highest Pressure: %f \n",temp_min, temp_max, pressure_min, pressure_max);
    while ((ch = getchar()) != '\n' && ch != EOF);
                    {
                         printf ("\n\nPress enter to close the program\n\n");
                         getchar ();
                         return 0;
                    }
    return 0;
    
    
}


/******************************************************************************/
/*                                                                            */
/*                     float temp_extreme(float, float, int)                  */
/*                                                                            */
/******************************************************************************/
/*                                                                            */
/*   Variables passed:                                                        */
/*      temp_max -> temp_max1                                                 */
/*      temp_min -> temp_min1                                                 */
/*                                                                            */
/******************************************************************************/
float temp_extreme(float temp_max1, float temp_min1, int count1)
{
      
/* If-Else Initializes max1 and min1 to the value of temp[count1]             */
/* This enables a comparison for after the initial count from main(), setting */
/* the scene for the else statment, which compares temp_max1 and temp_min1    */
/* to the value of the current array element (based on the count of the for   */
/* loop in main()                                                             */
      printf("%d \n", count1);
      if(count1 = 0)
      {
               temp_max1 = temp[count1];
               temp_min1 = temp[count1];
               printf("inital max: %f\n min: %f \n", temp_max1, temp_min1);
      }
      
      else
      {
          if(temp_max1 <= temp[count1])
                      temp_max1 = temp[count1];
          
          if(temp_min1 >= temp[count1])
                      temp_min1 = temp[count1];
          
          printf("max: %f\n min: %f \n", temp_max1, temp_min1);
      return temp_max1, temp_min1;
      }
      
}

/******************************************************************************/
/*                                                                            */
/*                     float pressure_extreme(float, float, int)              */
/*                                                                            */
/******************************************************************************/
/*                                                                            */
/*   Variables passed:                                                        */
/*      pressure_max -> pressure_max1                                         */
/*      pressure_min -> pressure_min1                                         */
/*                                                                            */
/******************************************************************************/

float pressure_extreme(float pressure_max1, float pressure_min1, int count1)
{
    printf("%d \n", count1);
/* If-Else Initializes max1 and min1 to the value of temp[count1]             */
/* This enables a comparison for after the initial count from main(), setting */
/* the scene for the else statment, which compares pressure_max1 and          */
/* pressure_min1 to the value of the current array element (based on the      */
/* count of the for loop in main()                                            */

      if(count1 = 0)
      {
               pressure_max1 = pressure[count1];
               pressure_min1 = pressure[count1];
               printf("Initial max: %f\n min: %f \n", pressure_max1, pressure_min1);
      }
      
      else
      {
          if(pressure_max1 <= pressure[count1])
                      pressure_max1 = pressure[count1];
          
          if(pressure_min1 >= pressure[count1])
                      pressure_min1 = pressure[count1];
          
          printf("max: %f\n min: %f \n", pressure_max1, pressure_min1);
      return pressure_max1, pressure_min1;
      }
      
}
return temp_max1, temp_min1;

is the same as:

return temp_min1 ; due to the way the comma operator works and the fact that you can only return one object from a function.

Your choices, if you want to (effectively) return more than one value from a function are to return a struct or class, return a container or pass variables in by reference and modify them in the function body.

Also, it may be useful to do something with the value returned rather than discard it as you're doing now.
Hmmm...Since I've not yet learned about containers or structs, I might have to pass the variables by reference. That might be easiest. I would set those variables as static, but I honestly don't have need of those values to be retained. I'm only really concerned with the largest and the smallest values.
Topic archived. No new replies allowed.