943,767 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 774
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 22nd, 2009
0

What is wrong?..!

Expand Post »
Hello friends.. I am an amateur programmer in TURBO C++ .. It is a very old version v3.0 ... I am beginner.. I wrote a very simple programme to find the largest ans smallest element in an array . .
The largest element finding process is working finely .. I added a watch on MAX and MIN... but out put MIN is wrong..! please help me Pros


Jeevan
C++ Syntax (Toggle Plain Text)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4.  
  5. {
  6. clrscr();
  7. int a[10],n,min,max,i;
  8. cout<<"Enter the number of elements in the matrix \t";
  9. cin>>n;
  10. cout<<"enter the elements of the matrix \n";
  11. for(i=0;i<n;i++)
  12. cin>>a[i];
  13. max=min=a[0];
  14.  
  15. for(i=0;i<n;i++)
  16. if(a[i]>max)
  17. max=a[i];
  18. if(a[i]<min)
  19. min=a[i];
  20. cout<<"Largest is "<<max<<" and Smallest is "<<min;
  21. getch();
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster
jeevsmyd is offline Offline
136 posts
since Oct 2008
Feb 22nd, 2009
0

Re: What is wrong?..!

You need more { }
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Feb 22nd, 2009
0

Re: What is wrong?..!

Hey this is the new code.. Is there anything wrong with this one? I missed the ELSE and a few {}
C++ Syntax (Toggle Plain Text)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4.  
  5. {
  6. clrscr();
  7. int a[10],n,min,max,i;
  8. cout<<"Enter the number of elements in the matrix \t";
  9. cin>>n;
  10. cout<<"enter the elements of the matrix \n";
  11. for(i=0;i<n;i++)
  12. cin>>a[i];
  13. max=min=a[0];
  14.  
  15. for(i=0;i<n;i++)
  16. if(a[i]>max)
  17. {
  18. max=a[i];
  19. }
  20. else if(a[i]<min)
  21. {
  22. min=a[i];
  23. }
  24. cout<<"Largest is "<<max<<" and Smallest is "<<min;
  25. getch();
  26. }
Reputation Points: 8
Solved Threads: 0
Junior Poster
jeevsmyd is offline Offline
136 posts
since Oct 2008
Feb 22nd, 2009
0

Re: What is wrong?..!

And what about the for loop?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Feb 22nd, 2009
0

Re: What is wrong?..!

hey excuse me but the above code is absolutely fine,i dont find an error it is running fine
Reputation Points: 10
Solved Threads: 5
Light Poster
arghasen is offline Offline
35 posts
since Nov 2008
Feb 22nd, 2009
0

Re: What is wrong?..!

Yeah..There are no problems with the code.. It is working fine.. What is wrong with the for loop?
Reputation Points: 8
Solved Threads: 0
Junior Poster
jeevsmyd is offline Offline
136 posts
since Oct 2008
Feb 22nd, 2009
0

Re: What is wrong?..!

It's missing { }
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Feb 22nd, 2009
0

Re: What is wrong?..!

no problem with the for loop as well

maybe sallem gave the coment as u missed the barces of the for loop in your first code
Reputation Points: 10
Solved Threads: 5
Light Poster
arghasen is offline Offline
35 posts
since Nov 2008
Feb 22nd, 2009
0

Re: What is wrong?..!

anyways the problem is now solved so make the thread as solved
Reputation Points: 10
Solved Threads: 5
Light Poster
arghasen is offline Offline
35 posts
since Nov 2008
Feb 22nd, 2009
0

Re: What is wrong?..!

okay buddy..
Reputation Points: 8
Solved Threads: 0
Junior Poster
jeevsmyd is offline Offline
136 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help on fscanf
Next Thread in C++ Forum Timeline: How to use an array element in if-statement?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC