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]
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]);
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.
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.
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 :
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:
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.
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 .