Aug 3, 2014 at 8:29pm UTC
Hi, I'm a beginner and I am trying to order arrays. This is what I have so far
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include <iostream>
using namespace std;
int main()
{
int arr[4];
int t;
int ar[4]{35,11,36,47};
/*if(ar[0]<ar[1]){ar[0]=arr[1];ar[1]=arr[0];};
for(int i=1; i<4; i++)
{
t=i+1;
if (ar[i]>arr[t]){arr[i]=ar[t];arr[t]=ar[i];}
else {arr[i]=ar[i];arr[t]=ar[t];};
}
for(int d=0;d<4; d++)
{
cout<<arr[d]<<",";
}*/
if (ar[0]<ar[1])
{
ar[0]=arr[1];
ar[1]=arr[0];
}
else
{
arr[1]=ar[0];
arr[1]==ar[0];
}
if (arr[1]<ar[2])
{
t=ar[2];
arr[1]=ar[2];
arr[2]=t;
}
else
{
arr[1]=ar[0];
arr[1]==ar[0];
}
if (arr[2]<ar[3])
{
t=ar[3];
arr[2]=ar[3];
arr[3]=t;
}
else
{
arr[1]=ar[0];arr[1]==ar[0];
}
if (arr[4]<ar[3])
{
t=ar[4];
arr[3]=ar[4];
arr[4]=t;
}
else
{
arr[1]=ar[0];arr[1]==ar[0];
}
for (int d=0;d<4; d++)
{
cout<<arr[d]<<"," ;
}
return 0;
}
the commented part is the part that I tried to use a for loop instead of hard coding it, but it didn't work. can you help me by giving me an example and its even better if you can help me on the for loop.
thanks
Last edited on Aug 3, 2014 at 8:30pm UTC
Aug 3, 2014 at 8:55pm UTC
I would suggest doing a search for Sorting algorithms so that you get a better understanding of what is involved as there are several ways to do it. There are a few examples here:
https://www.cs.cmu.edu/~adamchik/15-121/lectures/Sorting%20Algorithms/sorting.html
The biggest thing you are doing wrong with what you have is that you are not swapping the values like you think you are.
1 2 3
//ar[0] = 35, ar[1] = 11
ar[0]=ar[1]; // ar[0] = 11
ar[1]=ar[0]; // ar[1] = 11
You need to have a temp variable to swap
1 2 3
temp = ar[0]; // temp = 35
ar[0] = ar[1]; // ar[0] = 11
ar[1] = temp; //ar[1] = 35
Last edited on Aug 3, 2014 at 8:57pm UTC
Aug 5, 2014 at 2:23am UTC
can you do it using a for loop and not hard code it
Aug 5, 2014 at 2:57am UTC
this still doesnt work
#include <iostream>
using namespace std;
int main()
{
int arr[4];
int t;
int ar[4]{35,11,36,47};
/*if(ar[0]<ar[1]){ar[0]=arr[1];ar[1]=arr[0];};
for(int i=1; i<4; i++)
{
t=i+1;
if (ar[i]>arr[t]){arr[i]=ar[t];arr[t]=ar[i];}
else {arr[i]=ar[i];arr[t]=ar[t];};
}
for(int d=0;d<4; d++)
{
cout<<arr[d]<<",";
}*/
if(ar[0]<ar[1])
{
t = ar[0];
ar[0] = ar[1];
ar[1] = t;
}
else
{
arr[1]=ar[0];
arr[1]==ar[0];
}
if(arr[1]<ar[2])
{
t=ar[2];
arr[1]=ar[2];
arr[2]=t;
}
else
{
arr[1]=ar[0];
arr[1]==ar[0];
}
if(arr[2]<ar[3])
{
t=ar[3];
arr[2]=ar[3];
arr[3]=t;
}
else
{
arr[1]=ar[0];arr[1]==ar[0];
}
if(arr[4]<ar[3])
{
t=ar[4];
arr[3]=ar[4];
arr[4]=t;
}
else
{
arr[1]=ar[0];arr[1]==ar[0];
}
for(int d=0;d<4; d++)
{
cout<<arr[d]<<",";
}
return 0;
}
Last edited on Aug 5, 2014 at 2:58am UTC
Aug 5, 2014 at 3:00am UTC
can you give me a whole sample program???
Aug 6, 2014 at 9:44pm UTC
thank!!!
Last edited on Aug 6, 2014 at 10:58pm UTC
Aug 6, 2014 at 10:57pm UTC
can you make a hardcode for this sorting program with ifs and else ifs
Last edited on Aug 6, 2014 at 10:59pm UTC
Aug 6, 2014 at 11:33pm UTC
You would need to know the size of the array before hand but why in the world would you want to? At least use a loop(s) and if(s).
Aug 10, 2014 at 2:05am UTC
can you sort an array using while loops???
Aug 10, 2014 at 2:07am UTC
Anything that can be done by a for
loop can be done by while
loop.
Aug 10, 2014 at 2:07am UTC
A while loop is just a less-restrictive for loop, so yes.
Aug 10, 2014 at 2:10am UTC
Try not using the arbitrary hard coding
Aug 10, 2014 at 2:11am UTC
can you give me an example
Aug 10, 2014 at 2:11am UTC
Hard coding is a bad habit
Aug 10, 2014 at 2:12am UTC
Search google for the definition of arbitrary and you'll know what I'm saying
Aug 10, 2014 at 2:20am UTC
THEN Y DID BOB THE ZEALOT REPORT ME ON SAYING THANKS???