sorting problem with swap

hi
l am trying to sort an array that will be read .
l use swap function but it only swaps it once for the first value of i.


#include<stdio.h>
#include<iostream.h>
#include <string>
using namespace std;
void main(){

int j=1,i ,nota[5],vari;
//char nombre[20][20];
for(i=0;i<=5;i++){
cin >> nota[i] ;
swap(nota[i],nota[i+1]);}
for(i=0;i<=5;i++){
cout << nota[i];}}
This is going to access the array out of bounds:
 
for(i=0;i<=5;i++)

Arrays are subscripted from 0 to n-1. So your code is going to try to access non existing array elements.
l am sorry but l really dont get what u trying to say , can you please explain more .
this code sorts the numbers in a strange way , inputs for exapmple are (1,2,3,4,5,6)
out put after sorted (6,1,2,3,4,5) knowing that they are supposed to be (6,5,4,3,2,1).


#include<stdio.h>
#include<iostream.h>

#include <string>
using namespace std;
void main(){
int s;
int j=1,i ,nota[20];
char nombre[20][20];
for(i=0;i<=5;i++){
//cout << "entre el nombre";
//cin >> nombre[i];
cout << "entre la nota";
cin >> nota[i];}
for(i=0;i<5;i++)
{
for(j=i+j;j<6;j++)
{
if(nota[i]<nota[j])
swap (nota[i],nota[j]);
}
}
for(s=0; s <=5;s++){
cout <<"\n"<< nota;}}
[s]
Last edited on
here is another code when l run it , it works fine with numbers of two values like
10 , 11 , ...
but when it is a single value l get strange results


#include<stdio.h>
#include<iostream.h>

#include <string>
using namespace std;

void main(){
const int x=5;
int s;
int j=1,i ,nota[x];
char nombre[x][20];
for(i=0;i<x;i++){
//cout << "entre el nombre";
//cin >> nombre[i];
cout << "entre la nota";
cin >> nota[i];}
for(i=0;i<x;i++)
{
for(j=i+1;j<x+1;j++)
{
if(nota[i]<nota[j])
swap (nota[i],nota[j]);
}
}
for(s=0; s <x;s++){
cout <<"\n"<< nota[s];}
//cout <<"\n"<< nombre[s];}
}




results l get with single numbers would be ( 654444) knowing that the input was (13625)

but it works find other double numbers.
for(j=i+1;j<x+1;j++)

is wrong for the reason Galik posted above.

Your sort is also all wrong. This is not what you want:
1
2
if(nota[i]<nota[j])
swap (nota[i],nota[j]);


You don't want to compare element #i to element #j; you want to
compare element #j to element #j+1.
the code works just fine , l dont think it is wrong .

l have defined already that j=i+1 which is the next element in the array .
the first round (i) stays fixed but j increases each time by 1 wihich is correct .

so it goes like this 1-2 , 1-3, 1-,4 , 1-5 , 1-6 ........etc
the problem is when l enter single numbers as l mentioned l get that strange results l described earelier. please help
@jsmith: the algorithm is a version of selection sort. Put the maxim value of the array at the beginning, advance and repeat.
@Issam: Please use code tags to put your code.
Make a desk test:
1
2
3
4
for(i=0;i<x;i++)
	for(j=i+1;j<x+1;j++) //last value of j is x=5
		if(nota[i]<nota[j]) //nota[0]<nota[5] Error: Out of Bounds
			swap (nota[i],nota[j]);


Also, in c++ main() must return int.
l will next time .
thanks everyone for your responds .
ne55 : l dont understand why out of bounds ! nota[5] is the last element why there would be any error ?
l honestly dont get it

and what about the strange results l get with single values . can any one explain that to me .
thanks alot for your support

l am new in this forum and l dont know how to use code tag any demonstration would be great.
Galik wrote:

Arrays are subscripted from 0 to n-1. So your code is going to try to access non existing array elements.

nota[0], nota[1], nota[2], nota[3], nota[4]: That's your array of 5 elements.
nota[4] it's the last element.
nota[5] out of bounds

Code tags:
http://www.cplusplus.com/forum/articles/9044/#msg41810
thanks so much for making it clear , now l understand .
but the problem of single number still apeear !!

inputs (5,3,2,1,9)
output (5,4,3,4,5)
l really dont get it , why would that happend !! where that 4 came from?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<stdio.h>
#include<iostream>

#include <string>
using namespace std;

void main(){
	const int x=5;
	//int s;
	int j=1,i ,nota[x];
	char nombre[x][20];
	char a[20];
	for(i=1;i<=x;i++){
		//cout << "entre el nombre";
		//cin >> nombre[i];
		cout << "entre la nota";
		cin >> nota[i];}
	for(i=1;i<x;i++)
	{
		for(j=i+1;j<=x;j++)
		{
			if(nota[i]<nota[j])
			{
				swap (nota[i],nota[j]);
				 //strcpy (a, nombre[ i ]);
                // strcpy (nombre[i], nombre[j]);
                 //strcpy (nombre[i],a);
			}
		}
	}
		for(i=1; i <=x;i++){
			cout <<"\n"<<  nota[i];}
			//cout <<"\n"<< nombre[i];}
	

	

}
Line 13 will make i out of bounds.

 
for(i=1;i<=x;i++) // I will get too big !! 
"Line 13 will make i out of bounds."

why would that happen ?
l have already defined x =5 .
my problem now is not this , as l mentioned l get that strange sequence of numbers as output.
thanks for trying helping me out , l appreciate that.
x = 5.

Therefore i will become equal to 5.

5 is out of bounds.

1
2
3
4
5
nota[0]; 
nota[1]; 
nota[2]; 
nota[3]; 
nota[4]; // last index is 4,NOT 5  


nota[5] is out of bounds
lf u take a good look on the code you would see that l have changed that already , and l set

i = 1 .
which means

nota[1 ;
nota[2];
nota[3];
nota[4];
nota[5;];

nota[5] is included because in the loop it is "x<=" .less than or equal.

just until x = 5.

lf l am wrong please correct me , and prove me l am wrong .
thanks alot for your help
Last edited on
lssam wrote:
nota[5] is included because in the loop it is "x<=" .less than or equal.


Which is your mistake. You should make i < x.

Arrays don't work from 1-5. It works from 0-4.

This is wrong:
1
2
3
4
5
nota[1];
nota[2];
nota[3];
nota[4];
nota[5];


This is correct:
1
2
3
4
5
nota[0];
nota[1];
nota[2];
nota[3];
nota[4];

thanks alot , l think that is what l needed to confirm. l want to point out that
l dont think it is correct saying arrays are defined from (0 to n-1 ) otherwise lf l have an array and l only want to get a range of elements then according to that definition l cant !!
so l believe that it would be more correct saying that we cant use an "=" equal saying when defining the range of the elements of an array, or in other words it can not be a closed range.

in my code l still get that strange results :

input (2,9,5,3,9)
l get ordered like this (9,8,5,4,4)
why is that !!!

first where is 3?
second where that 4 came from?
third why that 4 twice?

it is pretty challening l have been trying to understand all these days with your help guys
but l really dont get , why?
here is the new code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<stdio.h>
#include<iostream>

#include <string>
using namespace std;

void main(){
	const  int x=5;
	//int s;
	int j=1,i ,nota[x];
	char nombre[x][20];
	char a[20];
	for(i=0;i<x;i++){
		//cout << "entre el nombre";
		//cin >> nombre[i];
		cout << "entre la nota";
		cin >> nota[i];}
	for(i=0;i<x;i++)
	{
		for(j=i+1;j<x+1;j++)
		{
			if(nota[i]<nota[j])
			{
				swap (nota[i],nota[j]);
				 //strcpy (a, nombre[ i ]);
                // strcpy (nombre[i], nombre[j]);
                 //strcpy (nombre[i],a);
			}
		}
	}
		for(i=0; i <x;i++){
			cout <<"\n"<<  nota[i];}
			//cout <<"\n"<< nombre[i];}
	
}


Last edited on
Why one earth are you repeating the same mistake again and again:
There is no prob in your code except array out of bound.
In line number 20 of ur above code u have

for(j=i+1;j<x+1;j++)

instead it should be j<x because j < x+1 will mean j < 6 and therefore j can be equal to 5.sicne nota[4] is the last element max value of j should be 4.

Have made that change and removed commented line from your code.And it works perfectly fine.See below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<cstdio>
#include<iostream>
#include <algorithm>
#include <string>
using namespace std;

int main(){
        const  int x=5;
        //int s;
        int j=1,i ,nota[x];
        char a[20];
        for(i=0;i<x;i++){
                cout << "entre la nota";
                cin >> nota[i];}
        for(i=0;i<x;i++)
        {
                for(j=i+1;j<x;j++)
                {
                        if(nota[i]<nota[j])
                        {
                                swap(nota[i],nota[j]);
                        }
                }
        }
                for(i=0; i <x;i++){
                        cout <<"\n"<<  nota[i];}
return 0;
}
Last edited on
lssam wrote:
l dont think it is correct saying arrays are defined from (0 to n-1 ) otherwise lf l have an array and l only want to get a range of elements then according to that definition l cant !!

You can think that all you want. But your the one with code that doesn't work.

Now if you want a range of (say) 4 elements from an array, then your range is defined from 0 to n-1 where n is the number of elements in the range.

Its the only way that it does work.

instead it should be j<x because j < x+1 will mean j < 6 and therefore j can be equal to 5.sicne nota[4] is the last element max value of j should be 4.

yes , l forgot that one , works just fine now.

thanks alot guys l really appreciate it alot.
it should be a good day for me now that little code works fine .

Last edited on
Topic archived. No new replies allowed.