Click here to Skip to main content
11,430,039 members (76,411 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
My 3rd qestion..Big Grin | :-D
The request is: Input 4 numbers and print on screen:
1. Max and Min.
2. The remaning numbers.

I successfully coding the frist (Max and Min). The code's here:

#include<stdio.h>

int main()
{
    int a,b,c,d;
    printf("Input a: ");
    scanf("%i", &a);
     printf("Input b: ");
    scanf("%i", &b);
     printf("Input c: ");
    scanf("%i", &c);
     printf("Input d: ");
    scanf("%i", &d);
    if(a>b&&a>c&&a>d) printf("Max is: %i\n", a);
    if(b>a&&b>c&&b>d) printf("Max is: %i\n", b);
    if(c>a&&c>b&&c>d) printf("Max is: %i\n", c);
    if(d>a&&d>b&&d>c) printf("Max is: %i\n", d);
    if(a<b&&a<c&&a<d) printf("Min is: %i\n", a);
    if(b<a&&b<c&&b<d) printf("Min is: %i\n", b);
    if(c<a&&c<b&&c<d) printf("Min is: %i\n", c);
    if(d<a&&d<b&&d<c) printf("Min is: %i\n", d);
    system("pause");
    return 0;
}
    

Now i get stuck at the algorithm of the 2nd request...Can you give me a solution? (And of course, the code if you can). Thank you!!^^
Posted 12-Sep-12 18:23pm
Comments
prashant patil 4987 at 13-Sep-12 0:30am
   
hey Minh, are youwant to do this multiple times.?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

/*
Create an integer Array. and store input data into that array. I assume that you know how to store input data into array.
*/
void main(void)
{
   int arrMaxMin[4] = {23,43,45,12}; /* you have to store input values into array. I have hard coded value for an example. */
   int arrsize = sizeof(arrMaxMin) / sizeof(int);
   int max = max_array(arrMaxMin,arrsize );
   int min = min_array(arrMaxMin,arrsize );
 
   printf("The max is %d\n", max);
   printf("The min is %d\n", min);
 
   printf("\nArray:\n");
   print_remainingarray(arrMaxMin, arrsize, max, min);
 
   
}
 
int max_array(int arrMaxMin[], int arrsize)
{
   int i, max=arrMaxMin[0];
   for (i=0; i<arrsize;>     if (arrMaxMin[i]>max)
	max=arrMaxMin[i];
   return(max);
}
int min_array(int arrMaxMin[], int arrsize)
{
   int i, min=arrMaxMin[0]; 
   for (i=0; i<arrsize;>	 if (arrMaxMin[i]<min)>
	    min=arrMaxMin[i];
   return(min);
}
 
void print_remainingarray(int arrMaxMin[], int arrsize, int max, int min)
{
   int i;
   for(i=0; i<arrsize;>       if(arrMaxMin[i]!= max & arrMaxMin[i]!= min)
	 printf("%d ", arrMaxMin[i]);
   }
   printf("\n");
}
 
/* Mark it as an answer if it helps you. */
  Permalink  
Comments
Minh Hikari at 13-Sep-12 7:55am
   
is there any more simple way (only using if-else)???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.150428.2 | Last Updated 13 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100