943,712 Members | Top Members by Rank

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

Re: What is wrong?..!

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:
C++ Syntax (Toggle Plain Text)
  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. }
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Feb 24th, 2009
0

Re: What is wrong?..!

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
Reputation Points: 13
Solved Threads: 6
Light Poster
seanhunt is offline Offline
40 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