Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want to check whether this code for arranging nos. in ascending order written in c++ is correct? If wrong pls correct it.

What I have tried:

C++
#include<iostream.h>
#include<conio.h>
void main()
{
	int i,j=1,n,temp,a[10];
	clrscr();
	cout<<"enter element lm"<<endl;

	cin>>n;
	cout<<"enter elements";
	for(i=0;i<n;++i)
		cin>>a[i];

	for(i=0;i<n,j<=n;i++,j++)
	{
		if(a[i]<=a[i+j])
				a[i]=a[i];
			else
				{
					temp=a[i];
					a[i]=a[i+1];
					a[i+1]=temp;
				}
			}
			cout<<"new array"<<endl;
			for(i=0;i<n;++i)
				cout<<a[i]<<"\n";
			getch();
		}
Posted
Updated 13-May-16 2:28am
v7
Comments
Patrice T 11-May-16 10:52am    
At least indicate the sort method it is supposed to be !
This code is not sorting at all.

Well...no.
This is your homework, and that means you are supposed to test it yourself.
It's important: it's a big part of the developement cycle:
0) Design
1) Code
2) Test
3) Debug
At any stage, you can go back to an earlier one as a result.
The last two are interrelated to a fair degree, and are often the most time consuming part of the exercise.

So, come up with some sets of test data:
Random values
All the same value
1 value
2 different values
All sorted correctly
All in reverse order
All correct except the last one, which is the lowest.
All correct except the first one, which is the highest

And try it with each set. Does it produce the right output? If so, it passes (unless you can think of more test sets).
If not - time to start looking at the code with the debugger and examine exactly what is happening!

Yes, we could do that for you - but this is a skill. And like all skills you only develop it by using it - and it's a lot easier to develop it on trivial exercises like this than on a 100,000 line application! :laugh:
Give it a try: see what you can find out.
 
Share this answer
 
Comments
Patrice T 11-May-16 22:20pm    
5ed
Quote:
I want to check whether this code for arranging nos. in ascending order wriiten in c++ is correct?
This is your duty to check if your code is correct ! Use the debugger.
By the way your code is so far away from 'correct' that is it bot even possible to guess what you tried to do.

You should learn to use the debugger as soon as possible. It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Quote:
PLZZ MAHN IF NOT CORRECT WRITE A DIFFERENT CODE FOR ME....
Being able to check own code and detect fails is the most basic skill requested from a programmer. Better start now.
 
Share this answer
 
v3
Comments
[no name] 12-May-16 10:41am    
PLZZ MAHN IF NOT CORRECT WRITE A DIFFERENT CODE FOR ME....
Patrice T 12-May-16 12:07pm    
Hi, looks like you don't like my answer.

If you look under your question, you will see I ask for information and you didn't even respond.

The fist thing, when you ask a question, is to give proper information.
-----------------
Simply giving a try to your code would have told you that your code is not correct. Your question let us think you didn't even try. This is rather rude to helpers.
-----------------
You have been given advices, did you try ?
-----------------
Did you understood that we don't write code on request just because you need it.
We help you correct your code, but not if a complete rewrite is needed.
Sergey Alexandrovich Kryukov 12-May-16 11:26am    
Please see new question by this inquirer and the comments, rude from the very beginning, even in the text of the question, apparently referring to your answer.

I think this person is asking for cancellation of the account.

http://www.codeproject.com/Questions/1099673/Here-is-a-trial-of-mine-for-arranging-array-in-asc.

[EDIT]

The past have been removed. The inquirer got 5 abuse reports on the account and asked the question again, more nicely, but that question also was removed...

—SA
Patrice T 12-May-16 12:11pm    
thank you for report.
[no name] 14-May-16 0:49am    
[Bad word, profanity, foul language, aggression — removed, the inquirer's account disabled — SA]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900