3 Replies - 1105 Views - Last Post: 19 February 2012 - 02:17 PM Rate Topic: -----

#1 Renegade_Queermo  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 29
  • Joined: 13-February 12

sorting an unsorted array

Posted 17 February 2012 - 06:16 PM

SO first off I basically have this thing working. they are 3 main things I need guidance with.
1. I have to enter in a blank character and hit enter before it actually initiates the program (basically I have to hit 1 then enter and then I can enter values into the array)
2. I can't enter the number 1 into the array or the program will not accept anymore values and excute.
3. not really a problem but I can only get 5 entries max. I want to know more why it does this not necassiarly fix it if that makes sense.

If that doesn't make sense sorry I am a little tired and ran out of coffe.

Anyway

#include<stdio.h>
#include<conio.h>
main()
{
int arr[50],temp,i,j,n;

printf("\nEnter any Value less Than 50. \n");
scanf("%d",&n);
printf("\n\tEnter The Values into ARRAY ");
for(i=0;i<n;i++)
{
printf("\n\n Enter Element no %d: ",i+1);
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(arr[j] >arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\n\n-- Sorted Series --");
for(i=0;i<n;i++)
{
printf("\n %d",arr[i]);
}
getch();







}


Is This A Good Question/Topic? 0
  • +

Replies To: sorting an unsorted array

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: sorting an unsorted array

Posted 17 February 2012 - 06:50 PM

View PostRenegade_Queermo, on 18 February 2012 - 02:16 AM, said:

SO first off I basically have this thing working. they are 3 main things I need guidance with.
1. I have to enter in a blank character and hit enter before it actually initiates the program (basically I have to hit 1 then enter and then I can enter values into the array)
What compiler/IDE are you using? I pasted your code into MS Visual C++ 2010 and can't reproduce that issue. Are you certain that the code you posted is the code you've used? Also, are you sure that you've recompiled since the last time you made a change to your code?

View PostRenegade_Queermo, on 18 February 2012 - 02:16 AM, said:

2. I can't enter the number 1 into the array or the program will not accept anymore values and excute.

Likewise, I can't reproduce this problem - see above.

View PostRenegade_Queermo, on 18 February 2012 - 02:16 AM, said:

3. not really a problem but I can only get 5 entries max. I want to know more why it does this not necassiarly fix it if that makes sense.
Your code has undefined behaviour in it, so anything could happen. Look at the invariant for your inner-loop, and the way that you're using j.
for(j=0;j<n-i;j++)
{
    if(arr[j] >arr[j+1])
Ask yourself the following question:
"If n == 5, what happens when j == 4 and i == 0" - Bearing in mind that your elements in your array containing meaningful data are those indexed from 0 to (n-1).


Also, be aware that declaring main() on its own without an int return type is no longer considered 'valid' C code (It's bad even if it compiles). It used to be legal in C89, but as of C90 your options are
int main(void)
int main(int argc, char* argv[])





On an unrelated note, I strongly suggest you follow some consistent indentation (using spaces instead of tabs - there should be a way in your IDE to configure this). It's far easier to read your code when everything is indented to show the start and end of different code blocks.

I churned the code through the auto-indenter which is part of Visual Studio, and the result is below (much clearer to the eye)
#include<stdio.h>
#include<conio.h>
int main(void)
{
    int arr[50],temp,i,j,n;

    printf("\nEnter any Value less Than 50. \n");
    scanf("%d",&n);
    printf("\n\tEnter The Values into ARRAY ");
    for(i=0;i<n;i++)
    {
        printf("\n\n Enter Element no %d: ",i+1);
        scanf("%d",&arr[i]);
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-i;j++)
        {
            if(arr[j] >arr[j+1])
            {
                temp=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=temp;
            }
        }
    }
    printf("\n\n-- Sorted Series --");
    for(i=0;i<n;i++)
    {
        printf("\n %d",arr[i]);
    }
    getch();

}

This post has been edited by Bench: 17 February 2012 - 06:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 Renegade_Queermo  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 29
  • Joined: 13-February 12

Re: sorting an unsorted array

Posted 18 February 2012 - 01:07 PM

I am using code block 10.05 as my complier. Still getting errors that I can't find the solution to my code is still the same as before as I have yet to find fixes for it.
Was This Post Helpful? 0
  • +
  • -

#4 Renegade_Queermo  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 29
  • Joined: 13-February 12

Re: sorting an unsorted array

Posted 19 February 2012 - 02:17 PM

K, I completely redid my code and it is almost perfect. One thing though I want to be able to print the first string after the user enters in all the elements. For example the user enters in "3,2,6," it print "326" before print out the "236" or the sorted array. The problem area is convienlty marked in blue comments for anyone willing to guide me in my journey of knowledge and epic coding.






#include<stdio.h>
#include<conio.h>
int main()
{

    int n,i,j,temp,a[20];//declaration of variables
    printf("\nEnter the number of elements being entered into the array.\n");//print statements
    printf("The number of elements must not exceed 19 characters in length.\n");

    scanf("%d",&n);//user inputs the lenght of the array up to 19 characters
    printf("Enter %d elements.\n",n);// user inputs the elements of the array
    for(i=0;i<n;i++)//looping the cycle of entering in the elements in the array
    {
        scanf("%d",&a[i]);
        }
        printf("Unsorted array:\n");/********this is what I neeed help on******/
        printf("%d.\n",a[i]);//it print this out after every number is entertered


        for(i=0;i<n-1;i++)//creating a loop to compare the size of each element in the array
        {
            for(j=i+1;j<n;j++)//setting the variable 'j' to increment
            {
                if(a[i]>a[j])// comparison if statement to compare the values of the array and sort them
                {
                    temp=a[j];
                    a[j]=a[i];
                    a[i]=temp;
                }
            }
        }
    printf("\nSorted Array:\n");//print statemnt
    for(i=0;i<n;i++)//final loop to print out all the elemnts in the array
    {
        printf("%d",a[i]);//print statement for the final sorted array
    }
    getchar();//return statement
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1