[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Sorting program

Posted on 2006-11-07
9
Medium Priority
?
257 Views
Last Modified: 2010-04-15
hi,

I have a sort program like below. there is no compiler error but it has some error. could you please help me correct it. thanks




---------------code --------------------------------------
#include <stdio.h>
#define maximum 4


void bubblesort(int a[], int n);
main()
{

 int a[maximum];
 int j;
 int x;
 int CNT=0;

 printf("(a)please enter number:");
 scanf("%d", &x);
 while (x !=-1 && CNT < maximum){
      a[CNT++]=x;
      printf("(b) enter number: ");
      scanf("%d", &x);

    if (CNT==maximum)
          printf("(c) overflow\n");
    else{
          printf("(d) extra: ");
          scanf("%d", &x);
     }
  }


  printf("(e) unslo list: " );
    for (j=0; j < CNT; j++)
        printf("%d, %d\n", j, a[j]);
       
    if (CNT >0)
     
      printf("(f) sorted: \n");
      bubblesort (a, CNT);
      for (j=0; j< CNT; j++)
         printf("%d, %d", j, a[j]);
         
         system("pause");
}


void bubblesort(int a[], int n)
{
    int i, j, tmp;

    for (i=0; i<n-1; i++)
    {
        for (j=0; j<n-1-i; j++)
            if (a[j+1] < a[j])     /* compare the two neighbors */
            {                      
                tmp = a[j];        /* swap a[j] and a[j+1]      */
                a[j] = a[j+1];
                a[j+1] = tmp;                        
            }

    }
   
}
0
Comment
Question by:rmtogether
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 17893025
Two problems here:

main()
{

 int a[maximum];
 int j;
 int x;
 int CNT=0;

 printf("(a)please enter number:");
 scanf("%d", &x);
 while (x !=-1 && CNT < maximum){
     a[CNT++]=x;
     printf("(b) enter number: ");
     scanf("%d", &x);

    if (CNT>=maximum) //  <--------- also for all values > maximum
         printf("(c) overflow\n");
    else{
         printf("(d) extra: ");
         scanf("%d", &x);
     }
  }


  printf("(e) unslo list: " );
    for (j=0; j < CNT; j++)
        printf("%d, %d\n", j, a[j]);
       
    if (CNT >0) { // <---------- need brackets here, or the 'if' will only be conditional for 'printf()'
     
      printf("(f) sorted: \n");
      bubblesort (a, CNT);
      for (j=0; j< CNT; j++)
         printf("%d, %d", j, a[j]);
         
         system("pause");
     } // <--- closing bracket also
}
0
 

Author Comment

by:rmtogether
ID: 17893142
hi, thanks

I following your instruction and change the code like below. but the the output seems not correct. it not really sorts the input numbers




#include <stdio.h>
#define maximum 4


void bubblesort(int a[], int n);
main()
{

 int a[maximum];
 int j;
 int x;
 int CNT=0;

 printf("(a)please enter number:");
 scanf("%d", &x);
 while (x !=-1 && CNT < maximum){
     a[CNT++]=x;
     printf("(b) enter number: ");
     scanf("%d", &x);

    if (CNT>=maximum) //  <--------- also for all values > maximum
         printf("(c) overflow\n");
    else{
         printf("(d) extra: ");
         scanf("%d", &x);
     }
  }


  printf("(e) unslo list: " );
    for (j=0; j < CNT; j++)
        printf("%d, %d\n", j, a[j]);
       
    if (CNT >0) { // <---------- need brackets here, or the 'if' will only be conditional for 'printf()'
     
      printf("(f) sorted: \n");
      bubblesort (a, CNT);
      for (j=0; j< CNT; j++)
         printf("%d, %d", j, a[j]);
         
         system("pause");
     } // <--- closing bracket also
}

void bubblesort(int a[], int n)
{
    int i, j, tmp;

    for (i=0; i<n-1; i++)
    {
        for (j=0; j<n-1-i; j++)
            if (a[j+1] < a[j])     /* compare the two neighbors */
            {                      
                tmp = a[j];        /* swap a[j] and a[j+1]      */
                a[j] = a[j+1];
                a[j+1] = tmp;                        
            }

    }
   
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 17893282
The more I look at your code the stranger the input sequence gets. Did you mean to

#include <stdio.h>
#define maximum 4


void bubblesort(int a[], int n);
main()
{

 int a[maximum];
 int j;
 int x;
 int CNT=0;

 while (x !=-1 && CNT < maximum){
     printf("(b) enter number: ");
     scanf("%d", &x);
     a[CNT++]=x;

  }


  printf("(e) unslo list: \n" );
    for (j=0; j < CNT; j++)
        printf("%d, %d\n", j, a[j]);
       
    if (CNT >0) { // <---------- need brackets here, or the 'if' will only be conditional for 'printf()'
     
      printf("(f) sorted: \n");
      bubblesort (a, CNT);
      for (j=0; j< CNT; j++)
         printf("%d, %d\n", j, a[j]);
         
         system("pause");
     } // <--- closing bracket also
}

void bubblesort(int a[], int n)
{
    int i, j, tmp;

    for (i=0; i<n-1; i++)
    {
        for (j=0; j<n-1-i; j++)
            if (a[j+1] < a[j])     /* compare the two neighbors */
            {                      
                tmp = a[j];        /* swap a[j] and a[j+1]      */
                a[j] = a[j+1];
                a[j+1] = tmp;                        
            }

    }
   
}

?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:rmtogether
ID: 17893470
thanks, I know it looks strange....I am kind confused....


but, I guess my teacher need some thing can return "overflow" if user's input excess array size , because  the original code was roughly copied from the class (there are some error, but the idea is like that)
if (CNT>=maximum) //  <--------- also for all values > maximum
         printf("(c) overflow\n");

I actually did the same thing like you give to me, but looks like my teach did not like it...><
If I recall correct this is call "variable link list"... do you have any idea about this?

0
 
LVL 86

Expert Comment

by:jkr
ID: 17893493
>>but, I guess my teacher need some thing can return "overflow" if user's input excess array size

You aren't taking any input regarding the array size in your code.

>>If I recall correct this is call "variable link list"

Err, no, that's something completely different.
0
 
LVL 3

Expert Comment

by:tang_tzuchi
ID: 17895088
Hi,

Try to change as following:

do{
printf("(a)please enter number:");
 scanf("%d", &x);

if (CNT>=maximum) //  <--------- also for all values > maximum
         printf("(c) overflow\n");

}while(x>=maximum || x<0);

 while (x !=-1 && CNT < maximum){
     a[CNT++]=x;
     printf("(b) enter number: ");
     scanf("%d", &x);
.... // the rest of ur code will be the same
}

hope it can help you.
0
 

Author Comment

by:rmtogether
ID: 17895106

hi, tang_tzuchi

could you give me the complete code for the main() function.  thanks.
0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 17895299
It is not really effective to check that while already exevuting teh loop, rather make sure that teh range is valid when inputting the value:

main()
{

 int a[maximum];
 int j;
 int x;
 int CNT=0;
 int max = maximum + 1;

 while (max > maximum) { // repeats until input is below maximum
   printf("(a)please enter number (max. is %d):", maximum);
    scanf("%d", &max);
 }

 while (x !=-1 && CNT < max){
     printf("(b) enter number: ");
     scanf("%d", &x);
     a[CNT++]=x;

  }


  printf("(e) unslo list: \n" );
    for (j=0; j < CNT; j++)
        printf("%d, %d\n", j, a[j]);
       
    if (CNT >0) { // <---------- need brackets here, or the 'if' will only be conditional for 'printf()'
     
      printf("(f) sorted: \n");
      bubblesort (a, CNT);
      for (j=0; j< CNT; j++)
         printf("%d, %d\n", j, a[j]);
         
         system("pause");
     } // <--- closing bracket also
}
0
 
LVL 3

Assisted Solution

by:tang_tzuchi
tang_tzuchi earned 1000 total points
ID: 17903044
#include <stdio.h>
#define maximum 4


void bubblesort(int a[], int n);
main()
{

 int a[maximum];
 int j;
 int x;
 int CNT=0;

 do{
printf("(a)please enter number:");
 scanf("%d", &x);

if (CNT>=maximum) //  <--------- also for all values > maximum
         printf("(c) overflow\n");

}while(x>=maximum || x<0);

 while (x !=-1 && CNT < maximum){
     a[CNT++]=x;
     printf("(b) enter number: ");
     scanf("%d", &x);
}

  printf("(e) unslo list: \n" );
    for (j=0; j < CNT; j++)
        printf("%d, %d\n", j, a[j]);
       
    if (CNT >0) { // <---------- need brackets here, or the 'if' will only be conditional for 'printf()'
     
      printf("(f) sorted: \n");
      bubblesort (a, CNT);
      for (j=0; j< CNT; j++)
         printf("%d, %d\n", j, a[j]);
         
         system("pause");
     } // <--- closing bracket also
}

void bubblesort(int a[], int n)
{
    int i, j, tmp;

    for (i=0; i<n-1; i++)
    {
        for (j=0; j<n-1-i; j++)
            if (a[j+1] < a[j])     /* compare the two neighbors */
            {                      
                tmp = a[j];        /* swap a[j] and a[j+1]      */
                a[j] = a[j+1];
                a[j+1] = tmp;                        
            }

    }
   
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question