7 Replies - 1038 Views - Last Post: 16 December 2009 - 05:15 PM Rate Topic: -----

#1 obe1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-December 09

Min and max return values for an array are incorrect. Beginner code.

Post icon  Posted 16 December 2009 - 03:39 PM

I've just began to use C and while doing the following program I get incorrect values returned for Min and Max. I'm not sure why? I'm aware that the average part of the code needs to be a float but i'll change it when I write the rest of the code.
This is my first post so I hope I've satisfied the rules.
Thanks,
Eoin, (Dublin, Ireland)

#include <stdio.h>
#include <stdlib.h>
main()
{
  int menu();
  void func_input_temps(int temp_array[],int num);
  void func_display_temps(int temps[],int num);
  int func_display_average(int temps[], int num );

  int func_min_max(int temps[], int num);




  int* temps;
  int i;
  int no_of_temps, no_of_bytes;
  int option;
  int average_temp;
  int min;
  int max;


  do
  {
	option=menu();

	switch (option)
	{
	case 0:
	  break;

	case 1:
	  printf("Enter the Number of Temperature Readings\n");
	  scanf("%d",&no_of_temps);
	  no_of_bytes=no_of_temps*sizeof(int);
	  temps=(int *)malloc(no_of_bytes);

	  func_input_temps(temps,no_of_temps);
	  break;

	case 2:
	  func_display_temps(temps,no_of_temps);
	  break;

	case 3:
	  average_temp=func_display_average(temps,no_of_temps);
	   printf("The average temperature is %d\n\n", average_temp);
	  break;

	case 4:
	 func_min_max(temps, no_of_temps);
	  printf("The maximum temperature is %d\n", max);
	  printf("The minimum temperature is %d\n", min);
	  break;

 }
  }
  while (option!=0);
}

int menu()
{
  int opt;
  printf ("1. Input temperatures\n");
  printf ("2. Display temperatures\n");
  printf ("3. Display the average temperature\n");
  printf ("4. Display the minimum and maximum temperatures\n");
  printf ("5. Display the temperatures in Farenheit and Celsius\n");
  printf ("6. Sort and display the temperature array\n");
  printf ("7. Display the Celsius temperature array graphically\n\n");
  printf ("Please select one of the above options 1-7 or 0 to exit.\n");

  scanf("%d",&opt);
  return opt;
}

void func_input_temps(int temp_array[],int num)
{
  int i;
  printf("Enter %d temperatures\n",num);
  for (i=0;i<num;i++)
	scanf("%d",&temp_array[i]);
}

void func_display_temps(int temps[],int num)
{
  int i;
  printf("Temperatures are ");
  for (i=0;i<num;i++)
	printf("%d ",temps[i]);

  printf("\n\n\n");
}

func_display_average(int temps[], int num )
{
  int i;
  int ave;
  int sum;

  for (i=0;i<num;i++)
  {
   sum += temps[i];
  }
   ave = (sum/num);

return(ave);

}


func_min_max(int temps[], int num)
{

	int i;
	int min;
	int max;

	min = temps[0];
	max = temps[0];

	for (i=0; i<num; i++)
	{
		if (temps[i] > max)
		{
			max = temps[i];
		}
		if (temps[i] < min)
		{
			min = temps[i];
		}
	}
	 return (min, max);

	}



Is This A Good Question/Topic? 0
  • +

Replies To: Min and max return values for an array are incorrect. Beginner code.

#2 openinfinity  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 215
  • Joined: 25-March 09

Re: Min and max return values for an array are incorrect. Beginner code.

Posted 16 December 2009 - 03:45 PM

It looks like you forgot to add int in front of the last two functions
Was This Post Helpful? 0
  • +
  • -

#3 obe1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-December 09

Re: Min and max return values for an array are incorrect. Beginner code.

Posted 16 December 2009 - 04:02 PM

Thanks, I did leave the int's out but even with them in I get the same issue.
Was This Post Helpful? 0
  • +
  • -

#4 openinfinity  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 215
  • Joined: 25-March 09

Re: Min and max return values for an array are incorrect. Beginner code.

Posted 16 December 2009 - 04:06 PM

I see what you mean now I just ran it and got this

Enter 5 temperatures
45 55 33 66 77
1. Input temperatures
2. Display temperatures
3. Display the average temperature
4. Display the minimum and maximum temperatures
5. Display the temperatures in Farenheit and Celsius
6. Sort and display the temperature array
7. Display the Celsius temperature array graphically

Please select one of the above options 1-7 or 0 to exit.
4
The maximum temperature is 11985668
The minimum temperature is 11984884



let me take a look and see whats going on.
Was This Post Helpful? 0
  • +
  • -

#5 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 769
  • Joined: 27-June 09

Re: Min and max return values for an array are incorrect. Beginner code.

Posted 16 December 2009 - 04:18 PM

View Postobe1, on 16 Dec, 2009 - 02:39 PM, said:

int func_min_max(int temps[], int num);
.
..
...
....
func_min_max(int temps[], int num) //type needs to match declaration *** int func_min_max(int temps[], int num)***
{

	int i;
	int min;
	int max;

	min = temps[0];
	max = temps[0];

	for (i=0; i<num; i++)
	{
		if (temps[i] > max)
		{
			max = temps[i];
		}
		if (temps[i] < min)
		{
			min = temps[i];
		}
	}
	 return (min, max); //You are supposed to return type int.  (int, int) is not a proper return type.
				  //Either return an array/custom data structure or make two seperate functions for min and max.

	}



See comments
Was This Post Helpful? 0
  • +
  • -

#6 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: Min and max return values for an array are incorrect. Beginner code.

Posted 16 December 2009 - 04:33 PM

The problem is that the variables min and max that you are printing out are local variables in the main() function, while the ones where you store the results are variables local to the function func_min_max(). Two different pairs of variables, which do not share their values. I would suggest creating separate functions for min and max, and then assigning the return values to min and max variables in main(). Like this:
...
main() {
...
  int min;
  int max;
...
	case 4:
	 min = func_min_(temps, no_of_temps);
	 max = func_max_(temps, no_of_temps);
	  printf("The maximum temperature is %d\n", max);
	  printf("The minimum temperature is %d\n", min);
	  break;
...
int func_min_(int temps[], int num)
{
...
	 return min;
	}
}
int func_max_(int temps[], int num)
{
...
	 return max;
	}
}


This post has been edited by Paul-: 16 December 2009 - 04:34 PM

Was This Post Helpful? 0
  • +
  • -

#7 obe1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-December 09

Re: Min and max return values for an array are incorrect. Beginner code.

Posted 16 December 2009 - 05:00 PM

Thanks Paul. Is the only way to avoid using 2 functions to use pointers?
Was This Post Helpful? 0
  • +
  • -

#8 Paul-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: Min and max return values for an array are incorrect. Beginner code.

Posted 16 December 2009 - 05:15 PM

Pointers will work. An alternative is to use structures.
struct min_max {
  int min;
  int max;
};

struct min_max func_min_max(int temps[], int num) {
  struct min_max mm;
  //...
  mm.min = min;
  mm.max = max;
  return mm;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1