Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My 3rd qestion..: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
Comments
prashant patil 4987 13-Sep-12 0:30am    
hey Minh, are youwant to do this multiple times.?

1 solution

SQL
/*
Create an integer Array. and store input data into that array. I assume that you know how to store input data into array.
*/

C++
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");
}

SQL
/* Mark it as an answer if it helps you. */
 
Share this answer
 
Comments
Minh Hikari 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900