Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,416 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,225 people online right now. Registration is fast and FREE... Join Now!




Display the array without the 3 maximum interger-Please help

 
Reply to this topicStart new topic

Display the array without the 3 maximum interger-Please help, I want to find the the 3 maximm number of the aibatary array and to di

prachi
27 Feb, 2007 - 09:42 PM
Post #1

New D.I.C Head
*

Joined: 27 Feb, 2007
Posts: 2


My Contributions
<include stdio.h>
int main()
{
int a[100],i,n=0;
int max=a[0];
int med=a[0];
int min=a[0];
printf("enter the no of element\n",n);
scanf("%d",&n);

for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
}
else if(a[i]<min)
{
min=a[i]
}
this code give me max but min is not coming ..also can any one help me to find med no .
I also want to display the array excluding the largest 3 no..like if the array is (3,6,1,4,6,7,5,9) then the output should print (3,1,4,5)
I would be really thank ful if some one can help me

This post has been edited by prachi: 27 Feb, 2007 - 09:44 PM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Display The Array Without The 3 Maximum Interger-Please Help
27 Feb, 2007 - 11:33 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,864



Thanked: 53 times
Dream Kudos: 550
My Contributions
You use some very unconventional syntax there... I think you bigest mistake is initalizing min to a[0] before it actually has any data in it, on some compilers (old ones I think) a[0] would be 0, on most compilers a[0] would be undeterminable (whatever value those bytes were lucky enough to be left with when they were last used).

here is some updated code. I am sorry about the reformating but I needed to look at it in a syntax familier to me:
CODE

#include <stdio.h>
int main()
{
    int a[100],i,n=0;
    //a is uninitalized so the next 3 lines
    //  assign unknown values to max, med and min.
    int max=a[0];
    int med=a[0];
    int min=a[0];
    printf("Enter the nomber of element\n",n);
    scanf("%d",&n);
    
    for(i=0;i<n;i++)
    {
        scanf("%d",a[i]);
    }
    
    for(i=0;i<n;i++)
    {
        printf("%d",a[i]);
    }

    //Now that a[] has some data in it we can
    //  use a[0] to initialize the variables.
    max=a[0];
    med=a[0];
    min=a[0];
    //we can start from 1 since we already assigned
    //a[0]
    for(i=1;i<n;i++)
    {
        if(a[i]>max)
        {
            max=a[i];
        }
        else if(a[i]<min)
        {
            min=a[i]
        }
    }
    return 0;
}

User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Display The Array Without The 3 Maximum Interger-Please Help
28 Feb, 2007 - 07:07 AM
Post #3

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,153



Thanked: 44 times
Dream Kudos: 125
My Contributions
For max and min NickDMax has given the perfect thing... icon_up.gif

Now it's turn to find (n)th largest number [3rd largest or middle or whatever...]

I gave a thought to it and came up with this logic.

CODE

#include <stdio.h>
#define length 7
main()
{
    int arr[length],i,j,max = -10000,oldmax = 0;    
    printf("Enter %d Numbers : ",length);
    for(i = 0; i < length; i++)
    {    
        printf("\nNumber %d :",i + 1);
        scanf("%d",&arr[i]);        
    }            
    for(i = 0; i < length; i++)
    {
        if(arr[i] >= max) max = arr[i];
    }
    oldmax = max;    
    for(i = 0; i < 2; i++) //will give 3rd largest, set (length / 2) for middle number
    {
        //setting to arr[0] creates problem for desceding arrays
        max = -10000;
        for(j = 0;j < length;j++)
        {
            if(arr[j] < oldmax && arr[j] >= max)
                max = arr[j];
        }
        oldmax = max;
    }
    printf("\n3rd Largest Number = %d",max);
}


now changing value in for loop from 2 to any other value gives say n
(n+1)th largest number. so (length/2) gives middle number.

Hope this will help you. smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 11:01AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month