i've managed to find out the greatest/smallest character according to the ASCII values but i cant think of a way to arrange all the characters in alphabetical order. i would like not to use fucntions and pointers because i've not started doing those yet.
thanks a lot but i've been told not to use sorting algorithms.
its not a problem if the code gets a bit lengthy but i want to stick to just the basic ideas so as to get my basics clear and only then do i want to go on to the advanced ideas.
i'm pretty new to c++ yet.
if something using just the 'c' ideas can be worked out i would be really thankful.
I think you're missing Duoas's point. The algorithms described in the wiki are not code someone has already written. They are an explanation of the steps needed to sort out data. The selection sort would just involve simple coping of data around. Have a look and have a go, it's the best way to learn. If you get stuck come back and ask for guidance.
I've written the following code but all i get as output are a bunch of 0's. I cant seem to find the problem with the code, but i think there is some problem while assigning the values to the array. I would really appreciate some help.
why don't you seperate the data-entry, sorting and data-output. That would make your code much more readable.
You forgot to initialize your array with arr[NUM] = {0};
Your array will have undefined values which are 50% of the time, greater then 0.
Your logic is wrong.
Line 17 will always be true. No matter what you do.
For the sorting:
If you enter a number larger then 0 (5 for example) as the first number, the if statements will find out that 5 is smaller then 0. So, your 5 will switch places from arr[0] to arr[1] .
But in the second pass of the loop, you overwrite arr[1] with the next value because for data-antry, your index is num (which is 1 in the second loop), for the switching, it is x (which is always 0).
But it gets worse:
After the first pass of the loop, arr[0] == 0. So no matter what you enter later on, the statement in line 20 can never become true again.
If you sort while entering the date, you must not use fixed indices for the position of the entered data but instead, you have to search your array for the right place to insert the new value.
That is why:
make a loop for entering the data
make a loop for sorting the data
make a loop to put out the data
My suggestion:
Start anew or use a pencil and paper and play debugger. You will see what happens in your code and you will understand.
int main
P.S.: A sorting algorithm is a defined sequence of statements(that's what algorithm means) that sorts something. Since you want to sort something and you are programming it, you will create a defined sequence of statements to sort something. So, if you succeed in sorting something, you will have created a sorting algorithm. There is no way past this. Sorry for your professor. But feel free to blame me :)
A slight correction to int mains post. Line 13 should be cin >> arr[i]. Every loop you are writing one step off the end of the array so you'll never get anything in the array itself.
#include<iostream>
#define NUM 4
usingnamespace std;
int main()
{
int arr[NUM];
int i;
int x;
int temp;
// User enters the number and store it in the array
for(i=0;i<NUM;i++){
cout << "Enter Four Whole Numbers\n";
cin>>x; // stores the number in x
cout<< "Thank You for Number " << i+1 <<"\n";
arr[i]=x;
}
// now we have the numbers in the array
// iterate through the array for sorting
for(i=0; i<NUM; i++){
int current=arr[i]; //get current number
int tempHold=current; // to hold it temporarily
// then check it with all other numbers
//if it is < store it in current
int smallestIndex=i;// to hold the smallest index in the rest of the array
for(int j=i+1;j<NUM;j++){
int rest=arr[j];
if(rest<current){// rest is less than current
current=rest;
smallestIndex=j;
}
}
// now we got the smallest in the array replace the ith one
arr[i]=current;
arr[smallestIndex]=tempHold;
}
// print it out
cout << "After Sorting\n";
for(int i=0; i<NUM; i++){
cout<<arr[i]<<"\n";
}
return 0;
}