5 Replies - 154947 Views - Last Post: 02 February 2011 - 02:06 AM Rate Topic: -----

#25 syinamdar  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 1
  • Joined: 01-February 07

Re: Find maximum and minimum element in a array of 10 elements.

Posted 01 February 2007 - 11:46 PM

// Find maximum and minimum element in an arary.
#include<stdio.h>
int
main ()
{
  int i;
  int a[10] = { 10, 55, 9, 4, 234, 20, 30, 40, 22, 34 };
  int max = a[0];
  int min = a[0];

  for (i = 0; i < 10; i++)
	{
	  if (a[i] > max)
		{
		  max = a[i];
		}
	  else if (a[i] < min)
		{
		  min = a[i];
		}
	}
  printf ("Maximum element in an array : %d\n", max);
  printf ("Minimum element in an array : %d\n", min);

  return 0;
}

EDIT : Code Tags Added -b2c-
Was This Post Helpful? 2

#26 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Find maximum and minimum element in a array of 10 elements.

Posted 02 February 2007 - 12:09 AM

Your program works perfectly. Whats your question?
Was This Post Helpful? 0
  • +
  • -

#27 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Find maximum and minimum element in a array of 10 elements.

Posted 02 February 2007 - 03:57 AM

He might have intended to post the code as a code snippet.
Was This Post Helpful? 0
  • +
  • -

#28 momike205  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-November 09

Re: Find maximum and minimum element in a array of 10 elements.

Posted 23 November 2009 - 10:50 AM

But I can't seem to understand why you would:
int max = a[0];
int min = a[0];

i dont understand the theory behind, please help me understand...
Was This Post Helpful? 0
  • +
  • -

#29 static_void  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 22-November 09

Re: Find maximum and minimum element in a array of 10 elements.

Posted 23 November 2009 - 11:14 AM

View Postmomike205, on 23 Nov, 2009 - 09:50 AM, said:

But I can't seem to understand why you would:
int max = a[0];
int min = a[0];

i dont understand the theory behind, please help me understand...


Try breaking the code into individual parts figuring out what it does at each step. This is what I come up with...

// Find maximum and minimum element in an arary.
#include<stdio.h>/*printf*/
int main ()
{
  int i;/*loop counter*/
  int a[10] = { 10, 55, 9, 4, 234, 20, 30, 40, 22, 34 };/*array of ints*/
/*set values for comparison starting from the beginning of the array*/
  int max = a[0];
  int min = a[0];

/*step through the array*/
  for (i = 0; i < 10; i++)
	{
/*if anything is greater than the value of max, which  started as the int pointed to with a[0]*/
	  if (a[i] > max)
		{
/*set the value found as the new max*/
		  max = a[i];
		}
/*if anything is less than the value of min, which started as the int pointed to by a[0]*/
	  else if (a[i] < min)
		{
/*set the value found as the new min*/
		  min = a[i];
		}
	}
/*after that loop is executed, we should have the minimum and maximum values*/
  printf ("Maximum element in an array : %d\n", max);
  printf ("Minimum element in an array : %d\n", min);

  return 0;
}


If you comment code, even when you didn't write it yourself, you learn a lot about programming logic.
Was This Post Helpful? 0
  • +
  • -

#30 Guest_Cleonjoys*


Reputation:

Re: Find maximum and minimum element in a array of 10 elements.

Posted 02 February 2011 - 02:06 AM

Quote

if you don't assign max and min as a[0] or the first element of the array then think of the case where array has only 1 element or array containing all the elements as same element (?).

Anyway i came up with the following code which makes us O(1.5n) comparisons compared to O(2n) comparisons. However the space complexity is O(n+2), where i need an array of size n/2 to store max values and another array of size n/2 to store min values and 2 storages for storing min and max value. In traditional O(2n) complexity code the space complexity is O(2). In case of the 10000 elements my code works with 15000 comparisons however traditional O(2n) works for 20000 comparisions



#include <stdio.h>

int max, min;

void find(int [], int);

int main(){

	int a[] = { 5, 2, 6, 0, 1};
	find(a, 6);
	printf("The Max is %d amd Min is %d\n", max, min);

	return 0;
}

void find(int a[], int n){

	int b[n/2+1], c[n/2+1];
	int i;

	for(i=0; i<n/2+1; i++)
		if(a[i]>a[n-1-i]){
			b[i] = a[i];
			c[i] = a[n-1-i];
		}
		else{
			b[i] = a[n-1-i];
			c[i] = a[i];	
		}

	max = b[0];
	for(i=1; i<n/2+1; i++)
		if(max < b[i])
			max = b[i]; 

	min = c[0];
	for(i=1; i<n/2+1; i++)
		if(min > c[i])
			min = c[i];
}



Sorry i forgot the main thing, traditional algorithm works best in case input is in incremental order example:-
1 2 3 4 5, since only the if statement will be executed each time leading to O(n) comparisons, however the worst case scenario is when the list is reversely sorted or in decremental order i.e., 5 4 3 2 1, then 2 comparisons are done causing the complexity to be O(2n). My Algo is independent of the nature of input, its always O(1.5n).
Was This Post Helpful? 0

Page 1 of 1