Rosg R.
asked 02/04/15can you tell me the wrong
int main() {
int x,n,d,c,swap,array[d],swap
scanf("%d", &x);
switch (x) {
case 1: mergeSort(...);
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
break;
case 2: bubbleSort(...);
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
break;
default: printf("Invalid choice\n");
}
return 0;
}
int x,n,d,c,swap,array[d],swap
scanf("%d", &x);
switch (x) {
case 1: mergeSort(...);
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
break;
case 2: bubbleSort(...);
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
break;
default: printf("Invalid choice\n");
}
return 0;
}
More
1 Expert Answer
Inactive Tutor answered 03/04/15
Tutor
New to Wyzant
Hi,
This program is trying to sort the array of numbers. The array cannot be defined before one decides how many member you need, unless it is declared dynamically.
I have added the include statements to be able to execute scanf and other utilities. Added print statements to see the results. I have added a random number generator to create data, which then can be sorted. I don't know how merge sort supposed to work, and I have used a book to rewrite bubble sort. (There has been many years since I have written sort code) See code below. This code executes using my compiler. It is limited to 50 number array. I also added a forever while to keep the window open, otherwise one cannot see the output on the PC.
I hope this helps -- If you have additional questions. please do not hesitate to contact me.
George
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int x,n,d,dd,c,swap,array[50], flag;
srand(time(NULL));
printf("enter member count : ");
scanf("%d",&d);
printf("entered: %d\n",d);
printf("enter option: ");
scanf("%d", &x);
printf("entered: %d\n",x);
n=d;
if(d > 49) {
printf(" too large member count: %d\n",d);
while(true);
exit(1);
}
for(dd=0; dd< d; dd++) {
array[dd] = rand();
printf("random number: %d\n",array[dd]);
}
switch (x) {
case 1: // mergeSort(...);
dd=0;
flag=0;
while(1)
if( array[dd] > array[dd+1] ) {
flag=1;
for (c = 0 ; c < ( n - 1 ); c++)
{
if (array[c] > array[c+1]) /* For decreasing order use < */
{
swap = array[c];
array[c] = array[c+1];
array[c+1] = swap;
}
}
if( ++dd > (n-1))
{
if(flag)
break;
dd-0;
}
dd=0;
flag=0;
} else {
printf("continue\n");
printf(" dd: %d\n",dd);
if(++dd > (n-1)) {
dd=0;
if(flag--)
continue;
for (int k=0; k< d;k++)
printf("member: %d value: %d\n",k,array[k]);
break;
} else {
continue;
}
}
case 2: // bubbleSort(...);
printf("n: %d",n);
for (c = n-1 ; c > 0; c--)
{
for (d = 0 ; d < c; ++d)
{
printf(" d: %d d+1: %d\n",array[d],array[d+1]);
if (array[d] > array[d+1])/* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
break;
default: printf("Invalid choice\n");
break;
for (int k=0; k< d;k++)
printf("member: %d value: %d\n",k,array[k]);
}
while(true);
return 0;
}
#include <stdlib.h>
#include <time.h>
int main() {
int x,n,d,dd,c,swap,array[50], flag;
srand(time(NULL));
printf("enter member count : ");
scanf("%d",&d);
printf("entered: %d\n",d);
printf("enter option: ");
scanf("%d", &x);
printf("entered: %d\n",x);
n=d;
if(d > 49) {
printf(" too large member count: %d\n",d);
while(true);
exit(1);
}
for(dd=0; dd< d; dd++) {
array[dd] = rand();
printf("random number: %d\n",array[dd]);
}
switch (x) {
case 1: // mergeSort(...);
dd=0;
flag=0;
while(1)
if( array[dd] > array[dd+1] ) {
flag=1;
for (c = 0 ; c < ( n - 1 ); c++)
{
if (array[c] > array[c+1]) /* For decreasing order use < */
{
swap = array[c];
array[c] = array[c+1];
array[c+1] = swap;
}
}
if( ++dd > (n-1))
{
if(flag)
break;
dd-0;
}
dd=0;
flag=0;
} else {
printf("continue\n");
printf(" dd: %d\n",dd);
if(++dd > (n-1)) {
dd=0;
if(flag--)
continue;
for (int k=0; k< d;k++)
printf("member: %d value: %d\n",k,array[k]);
break;
} else {
continue;
}
}
case 2: // bubbleSort(...);
printf("n: %d",n);
for (c = n-1 ; c > 0; c--)
{
for (d = 0 ; d < c; ++d)
{
printf(" d: %d d+1: %d\n",array[d],array[d+1]);
if (array[d] > array[d+1])/* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
break;
default: printf("Invalid choice\n");
break;
for (int k=0; k< d;k++)
printf("member: %d value: %d\n",k,array[k]);
}
while(true);
return 0;
}
Still looking for help? Get the right answer, fast.
Ask a question for free
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Find an Online Tutor Now
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Jon P.
02/05/15