What is wrong?..!

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: What is wrong?..!

 
0
  #11
Feb 22nd, 2009
Just because something is syntactically accurate doesn't mean "there is nothing wrong with the code." An important thing to mention about code, is that you are supposed to make it easy to read. Just because "it compiles fine" doesn't mean another programmer can look at it, and decipher your code without major effort. Some of these things include proper indentation. Indentation allows someone to see which blocks of code fall under which conditions and loops. It is meant to simplify the debugging process, and make life easier for you later on, and any other programmers who might work on your code. I'll wager that Salem's comment, wasn't merely a quick fix to a problem, it was meant to address a much deeper issue as well, such as the entirety of the style. I'll guess it should look something more like:
  1. #include <iostream.h>
  2. #include <conio.h>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char **argv)
  7. {
  8. clrscr();
  9. int a[10],n,min,max,i;
  10. cout<<"Enter the number of elements in the matrix \t";
  11. cin>>n;
  12. cout<<"enter the elements of the matrix \n";
  13.  
  14. for(i=0;i<n;i++) {
  15. cin>>a[i];
  16. max=min=a[0];
  17.  
  18. for(i=0;i<n;i++) {
  19. if(a[i]>max) {
  20. max=a[i];
  21.  
  22. } else if(a[i]<min) {
  23. min=a[i];
  24. }
  25. }
  26. }
  27.  
  28. cout<<"Largest is "<<max<<" and Smallest is "<<min;
  29. getch();
  30.  
  31. return 0;
  32. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 40
Reputation: seanhunt is an unknown quantity at this point 
Solved Threads: 6
seanhunt seanhunt is offline Offline
Light Poster

Re: What is wrong?..!

 
0
  #12
Feb 24th, 2009
What you've got works, since the if/else loop code is actually only a single statement. I would recommend, however, that you include brackets for clarity and ease of maintenance; it doesn't cost anything and it can lead to errors that can be quite difficult to identify (like the error that you originally had).

Thanx,
Sean
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC