-1

So I'm making a ghetto weather report by creating a random number generator anywhere from 60-100 and storing 25 of these in an array. Then I have a function that calculates max, min, and average along with printing all of this out.

I got it to run without error, but all I'm getting are a bunch of zeros in my display, which means I'm messing up big time somewhere in the calculation, any suggestions?

Also I'm trying to get down calling user-defined functions which is why I have several.

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


int sum = 0;
int min = 0;
int max = 0;
int temp[25];
int i = 0;
float avg = 0;


int main () {


srand( (unsigned) time(NULL) );

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

get_value(i);
sum += temp[i];

}

calc_results(temp[25]);

return 0;
};



int get_value(void) {
    return((rand() % (100 - 60 + 1)) + 60);

};



int calc_results(int temp_number[], int number) {

avg = ((sum)/(25));
max = temp[0];
  for(i=1;i<25;i++){
      if(max<temp[i])
           max=temp[i];
        };
min =temp[0];
  for(i=1;i<25;i++){
      if(min>temp[i])
           min=temp[i];
  };

printf("Temperature Conditions on October 9, 2015 : \n");
printf("Time of day     Temperature in degrees F \n");
printf("     0                        %d\n",temp[0]);
printf("     1                        %d\n",temp[1]);
printf("     2                        %d\n",temp[2]);
printf("     3                        %d\n",temp[3]);
printf("     4                        %d\n",temp[4]);
printf("     5                        %d\n",temp[5]);
printf("     6                        %d\n",temp[6]);
printf("     7                        %d\n",temp[7]);
printf("     8                        %d\n",temp[8]);
printf("     9                        %d\n",temp[9]);
printf("     10                       %d\n",temp[10]);
printf("     11                       %d\n",temp[11]);
printf("     12                       %d\n",temp[12]);
printf("     13                       %d\n",temp[13]);
printf("     14                       %d\n",temp[14]);
printf("     15                       %d\n",temp[15]);
printf("     16                       %d\n",temp[16]);
printf("     17                       %d\n",temp[17]);
printf("     18                       %d\n",temp[18]);
printf("     19                       %d\n",temp[19]);
printf("     20                       %d\n",temp[20]);
printf("     21                       %d\n",temp[21]);
printf("     22                       %d\n",temp[22]);
printf("     23                       %d\n",temp[23]);
printf("     24                       %d\n",temp[24]);
printf("     25                       %d\n",temp[25]);
printf("Maximum Temperature for the day: %d Degrees F\n", max);
printf("Minimum Temperature for the day: %d Degrees F\n", min);
printf("Average Temperature for the day: %.1f Degrees F\n", avg);

};
Garf365
  • 3,619
  • 5
  • 29
  • 41
Jclee
  • 39
  • 9
  • Also sorry if it's a big mess! The syntax takes me some getting used to – Jclee Mar 12 '16 at 05:37
  • `calc_results(temp[25]);` How can that be right? `temp[25]` is an out of bounds array access. And `calc_results` is defined to take two paramters anyway: `int calc_results(int temp_number[], int number)`. Guess it should be `calc_results(temp, 25)` – kaylum Mar 12 '16 at 05:46
  • 2
    Note: `printf(" 0 %d\n",temp[0]); printf(" 1 %d\n",temp[1]); printf(" 2 %d\n",temp[2]); ....` cries for a loop. – chux - Reinstate Monica Mar 12 '16 at 06:04

3 Answers3

0

Just chance this line

get_value(i);

To

temp[i]=get_value(i);

Because you have to store the random values in temp[i] then you can calculate the other values.

And while you passing the array. You should do this--

calc_results(temp);

You can print all array value by a for loop. Like this:

int cnt;
for(cnt = 0; cnt<=25; cnt++)
{
   printf("     %d                        %d\n",cnt,temp[cnt]);
}
Mr. Perfectionist
  • 2,605
  • 2
  • 24
  • 35
0

firstly dude use loop to print 25 printf statements... ur code would be a bit smaller... do the following changes and it should work pretty okay...

time_t t;
srand((unsigned) time(&t)); // this is a more standard way of using srand

also you have passed and int in get_value() whose parameter is void... do this in the for loop in main

temp[i]=get_value();

also declare your functions on above of your code...

you dont need calc_results() like this...

do it void calc_results(void) and no neeed of passing temp since its already global...no need of passing number type integer since you are not using any such thing...no need of using return type as int since you dont need to return any integer...

final suggestion... Get a good book on functions

so your final code would look something like this:-

    #include <stdio.h>
#include <stdlib.h>
#include <time.h>
int get_value(void);
void calc_results(void);
int sum = 0;
int min = 0;
int max = 0;
int temp[25];
int i = 0;
float avg = 0;


int main () {
time_t t;
srand((unsigned) time(&t));
for (i=0; i < 25; i++) {

temp[i]=get_value();
sum += temp[i];

}

calc_results();

return 0;
};



int get_value(void) {
    return((rand() % (100 - 60 + 1)) + 60);

};



void calc_results(void) {

avg = ((sum)/(25));
max = temp[0];
  for(i=1;i<25;i++){
      if(max<temp[i])
           max=temp[i];
        };
min =temp[0];
  for(i=1;i<25;i++){
      if(min>temp[i])
           min=temp[i];
  };

printf("Temperature Conditions on October 9, 2015 : \n");
printf("Time of day     Temperature in degrees F \n");
for(int j=0;j<25;j++){
printf("  %d      %d\n",i,temp[i]);
}
printf("Maximum Temperature for the day: %d Degrees F\n", max);
printf("Minimum Temperature for the day: %d Degrees F\n", min);
printf("Average Temperature for the day: %.1f Degrees F\n", avg);

};

also Dont use global variables unless local variables fail...this would be at great help when you do some big codes and file handling

Shreyan Mehta
  • 550
  • 5
  • 17
  • Wow, I see how bad i am lol, the bit about the headers of the functions really helps a lot, I'm barely getting the hang of how to pass and call user-defined functions > – Jclee Mar 12 '16 at 08:00
0

You aren't assigning temp[i] any value?

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

temp[i] = get_value(i);
sum += temp[i];

}
Ryan G
  • 380
  • 1
  • 11