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

#include<iostream.h>
#include<conio.h>
void main()

 {
 clrscr();
 int a[10],n,min,max,i;
 cout<<"Enter the number of elements in the matrix \t";
 cin>>n;
 cout<<"enter the elements of the matrix \n";
 for(i=0;i<n;i++)
 cin>>a[i];
 max=min=a[0];

 for(i=0;i<n;i++)
 if(a[i]>max)
  max=a[i];
 if(a[i]<min)
  min=a[i];
 cout<<"Largest is "<<max<<" and Smallest is "<<min;
 getch();

You need more { }

Hey this is the new code.. Is there anything wrong with this one? I missed the ELSE and a few {}

#include<iostream.h>
#include<conio.h>
void main()

 {
 clrscr();
 int a[10],n,min,max,i;
 cout<<"Enter the number of elements in the matrix \t";
 cin>>n;
 cout<<"enter the elements of the matrix \n";
 for(i=0;i<n;i++)
 cin>>a[i];
 max=min=a[0];

 for(i=0;i<n;i++)
 if(a[i]>max)
     {
 max=a[i];
      }
 else if(a[i]<min)
     {
  min=a[i];
     }
 cout<<"Largest is "<<max<<" and Smallest is "<<min;
 getch();
 }

And what about the for loop?

hey excuse me but the above code is absolutely fine,i dont find an error it is running fine

Yeah..There are no problems with the code.. It is working fine.. What is wrong with the for loop? :idea:

It's missing { }

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

anyways the problem is now solved so make the thread as solved

okay buddy..

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:

#include <iostream.h>
#include <conio.h>

using namespace std;

int main(int argc, char **argv)
{
	clrscr();
	int a[10],n,min,max,i;
	cout<<"Enter the number of elements in the matrix \t";
	cin>>n;
	cout<<"enter the elements of the matrix \n";

	for(i=0;i<n;i++) {
		cin>>a[i];
		max=min=a[0];

		 for(i=0;i<n;i++) {
			 if(a[i]>max) {
				max=a[i];

			 } else if(a[i]<min) {
			 	min=a[i];
			 }
		 }
	}

	cout<<"Largest is "<<max<<" and Smallest is "<<min;
	getch();

	return 0;
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.