User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 363,388 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,786 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 389 | Replies: 8 | Solved
Reply
Join Date: Aug 2007
Posts: 20
Reputation: web_master is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
web_master web_master is offline Offline
Newbie Poster

Problem in finding the greatest no. from array

  #1  
May 9th, 2008
#include<stdio.h>
int main()
{
    int i ,j ,a[5];
    for(i=0;i<=4;i++)
    {
        printf("\n\tEnter the %dst value of the array: ", i+1);
        scanf("%d", &a[i]);
    }
    j = a[0];
    printf("\n\tOriginal array entered by the user: ");
    for(i=0;i<=4;i++)
    {
        printf(" %d ", a[i]);
        if(a[i]<a[i+1])
        {
             j = a[i+1];
        }
    }
    printf("\n\n\tGreatest no. from the array: %d", j);
getch();
return 0;   
}

according to me there is no problem in this code.......

but its printing some wierd values again n again.....

plz...help me out here....

My Wierd Output:

Enter the 1st value of the array: 56

Enter the 2st value of the array: 35

Enter the 3st value of the array: 26

Enter the 4st value of the array: 15

Enter the 5st value of the array: 24

Original array entered by the user: 56 35 26 15 24

Greatest no. from the array: 4007048
Last edited by web_master : May 9th, 2008 at 2:17 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 2,919
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 18
Solved Threads: 321
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: Problem in finding the greatest no. from array

  #2  
May 9th, 2008
Your i+1 means you're reading off the end of the array.

Also, finding the maximum is a lot simpler than this.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Join Date: May 2008
Posts: 3
Reputation: adarshcu is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
adarshcu adarshcu is offline Offline
Newbie Poster

Re: Problem in finding the greatest no. from array

  #3  
May 9th, 2008
your value of j= [i+1] reads the value of the a[5] in the last cycle which does not exist at all...
Reply With Quote  
Join Date: Aug 2007
Posts: 20
Reputation: web_master is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
web_master web_master is offline Offline
Newbie Poster

Re: Problem in finding the greatest no. from array

  #4  
May 9th, 2008
I solved it myself......

the code is:
#include<stdio.h>
int main()
{
    int i ,j ,a[5];
    for(i=0;i<=4;i++)
    {
        printf("\n\tEnter the %dst value of the array: ", i+1);
        scanf("%d", &a[i]);
    }
    j = a[0];
    printf("\n\tOriginal array entered by the user: ");
    for(i=0;i<=3;i++)
    {
        printf(" %d ", a[i]);
        if(a[i]<a[i+1])
        {
             j = a[i+1];
        }
    }
    printf("\n\n\tGreatest no. from the array: %d", j);
getch();
return 0;
}

the loop that was comparing the numbers was looping one extra time so i limited it to 4 times (i<=3) instead of 5(i.e. the array size).....

now it works fine......

and i dont see a better way of doing it......maybe there is a easier way.......
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 666
Reputation: jephthah has a spectacular aura about jephthah has a spectacular aura about jephthah has a spectacular aura about 
Rep Power: 4
Solved Threads: 42
jephthah's Avatar
jephthah jephthah is offline Offline
Practically a Master Poster

Re: Problem in finding the greatest no. from array

  #5  
May 10th, 2008
good thing about shotguns is that if you're close enough you can still hit the target even if your aim is lousy.
I drink your milkshake.
Reply With Quote  
Join Date: May 2008
Posts: 2
Reputation: rajaa13 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rajaa13 rajaa13 is offline Offline
Newbie Poster

Re: Problem in finding the greatest no. from array

  #6  
May 12th, 2008
Change the prog as follows
#include<stdio.h>
#include<conio.h>
int main()
{
    int i ,j ,a[5];
    for(i=0;i<=4;i++)
    {
        printf("\n\tEnter the %dst value of the array: ", i+1);
        scanf("%d", &a[i]);
    }
    j = a[0];
    printf("\n\tOriginal array entered by the user: ");
    for(i=0;i<=4;i++)
    {
        printf(" %d ", a[i]);
	if(j<a[i])
        {
	     j = a[i];
        }
    }
    printf("\n\n\tGreatest no. from the array: %d", j);
getch();
return 0;
}
Last edited by ~s.o.s~ : May 12th, 2008 at 1:27 pm. Reason: Added code tags, learn to use them.
Reply With Quote  
Join Date: May 2008
Posts: 2
Reputation: rajaa13 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rajaa13 rajaa13 is offline Offline
Newbie Poster

Re: Problem in finding the greatest no. from array

  #7  
May 13th, 2008
Your program still contain serious logical error.give data like 120,12,13,45,90 and check with your prog.




Originally Posted by web_master View Post
I solved it myself......

the code is:
#include<stdio.h>
int main()
{
    int i ,j ,a[5];
    for(i=0;i<=4;i++)
    {
        printf("\n\tEnter the %dst value of the array: ", i+1);
        scanf("%d", &a[i]);
    }
    j = a[0];
    printf("\n\tOriginal array entered by the user: ");
    for(i=0;i<=3;i++)
    {
        printf(" %d ", a[i]);
        if(a[i]<a[i+1])
        {
             j = a[i+1];
        }
    }
    printf("\n\n\tGreatest no. from the array: %d", j);
getch();
return 0;
}

the loop that was comparing the numbers was looping one extra time so i limited it to 4 times (i<=3) instead of 5(i.e. the array size).....

now it works fine......

and i dont see a better way of doing it......maybe there is a easier way.......
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 666
Reputation: jephthah has a spectacular aura about jephthah has a spectacular aura about jephthah has a spectacular aura about 
Rep Power: 4
Solved Threads: 42
jephthah's Avatar
jephthah jephthah is offline Offline
Practically a Master Poster

Re: Problem in finding the greatest no. from array

  #8  
May 13th, 2008
since he's happy with his own "solution", i think it's safe to say he's not looking to improve/fix it -- hence, my earlier shotgun metaphor.

theres another metaphor that applies here, it starts with "you can lead a horse to water..."


.
Last edited by jephthah : May 13th, 2008 at 3:23 am.
I drink your milkshake.
Reply With Quote  
Join Date: Nov 2007
Posts: 18
Reputation: varsha0702 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
varsha0702 varsha0702 is offline Offline
Newbie Poster

Re: Problem in finding the greatest no. from array

  #9  
May 16th, 2008
Originally Posted by web_master View Post
#include<stdio.h>
int main()
{
    int i ,j ,a[5];
    for(i=0;i<=4;i++)
    {
        printf("\n\tEnter the %dst value of the array: ", i+1);
        scanf("%d", &a[i]);
    }
    j = a[0];
    printf("\n\tOriginal array entered by the user: ");
    for(i=0;i<=4;i++)
    {
        printf(" %d ", a[i]);
        if(a[i]<a[i+1])
        {
             j = a[i+1];
        }
    }
    printf("\n\n\tGreatest no. from the array: %d", j);
getch();
return 0;   
}

according to me there is no problem in this code.......

but its printing some wierd values again n again.....

plz...help me out here....

My Wierd Output:

Enter the 1st value of the array: 56

Enter the 2st value of the array: 35

Enter the 3st value of the array: 26

Enter the 4st value of the array: 15

Enter the 5st value of the array: 24

Original array entered by the user: 56 35 26 15 24

Greatest no. from the array: 4007048




Hi...
U were not comparing all the elements. code started with comparing a single element with others. Following code will help u out.
  1. #include<stdio.h>
  2. int main()
  3. {
  4. int i ,j ,a[5],temp;
  5. for(i=0;i<=4;i++)
  6. {
  7. printf("\n\tEnter the %dst value of the array: ", i+1);
  8. scanf("%d", &a[i]);
  9. }
  10. i=0;
  11. // printf("\n\tOriginal array entered by the user: ");
  12.  
  13. //for(i=0;i<=3;i++)
  14. //{
  15. // printf(" %d ", a[i]);
  16. i=0;
  17. for(j=1;j<=4;j++)
  18. {
  19. if(a[i]>a[j])
  20. {
  21. temp =a[i];
  22. a[i]=a[j];
  23. a[j]=temp;
  24.  
  25. }
  26.  
  27. i++;
  28. }
  29.  
  30. printf("\n\n\tGreatest no. from the array: %d\n", a[4]);
  31.  
  32. return 0;
  33. }
Last edited by Narue : May 16th, 2008 at 12:41 pm. Reason: Added code tags
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 12:31 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC