-2

I have this code but this is not showing me the required result. that is to merge 2 arrays and print it in descending order. I wan to merge 2 sorted arrays taking input from user. user will tell the size of array the elements of array too and then my program should merge and sort descendingly and print

int main()
{

int num1,num2,i,tem;

printf("Number of elements in first array:");
scanf("%d",&num1);

printf("Number of elements in second array:");
scanf("%d",&num2);

int array1[num1],array2[num2],merge[num1+num2];

printf("Elements for array 1 \n");

for ( i = 0; i < num1; i++)
{
    printf("Element:"); 
    scanf("%d",&array1[i]);
}

printf("Elements for second array\n");

for ( i = 0; i < num2; i++)
{
    printf("Element:");
    scanf("%d",&array2[i]);
}
for ( i = 0; i < num1; i++)
{
    merge[i] = array1[i];
}
for ( i = 0; i < num2; i++)
{
    merge[i+num1] = array2[i];
}
for ( i = 0; i < num1 + num2; i++ ) 
{
    if ( merge[i] < merge[i+1] )
    {
        tem = merge[i];
        merge[i] = merge[i+1];
        merge[i+1] = tem;
    }
}

printf("Merge:");

for ( i = 0; i < num1 + num2; i++ )
{
   printf("%d  ",&merge[i]);
}

return 0;

}
Uchiha Itachi
  • 1,251
  • 1
  • 16
  • 42
  • You should also state what language you're coding in, for future reference... That being said, you're not sorting your array. You're just adding to it and modifying values. I'll put an answer below. – Uchiha Itachi Dec 07 '16 at 17:20
  • Possible duplicate of [Create sorted array from multiple pre-sorted arrays](http://stackoverflow.com/questions/29010420/create-sorted-array-from-multiple-pre-sorted-arrays) – Uchiha Itachi Dec 07 '16 at 20:26

2 Answers2

0
int count1 = 10;
int count2 = 15;

int arNums1[];
int arNums2[];
int arMergeNums[];

arNums1 = new int[count1];
arNums2 = new int[count2];
arMergeNums = new int[count1 + count2];

//POPULATE YOUR FIRST TWO ARRAYS HERE...

//FILL YOUR MERGED ARRAY LIKE THIS:
for (int i = 0; i < (count1 + count2); i++)
{
    if ( i < count1 )
        arMergeNums[i] = arNums1[i];
    else arMergeNums[i] = arNums2[i];
}

//THEN SORT IT LIKE THIS:
for (int i = 0; i < (count1 + count2); i++)
{
    for (int j = i + 1; j < (count1 + count2); j++)
    {
        if (arMergeNums[i] < arMergeNums[j])
        {
            int temp = arMergeNums[i];
            arMergeNums[i] = arMergeNums[j];
            arMergeNums[j] = temp;
            temp = null;
        }
    }
}

That's it...

Uchiha Itachi
  • 1,251
  • 1
  • 16
  • 42
0
$(document).ready(function(){
  var cars = [4,3,9,6];
 for(i=0;i<cars.length;i++){
 //var carsrev=cars[cars.length-i-1];
  //alert(carsrev);
  $('<li>'+cars[cars.length-1-i]+'</li>').appendTo("ul.demo");
 //alert($("ul.demo li").length);
  }
 });

//Out Put is 6 9 3 4 not 9 6 4 3 its just reverse

D V Yogesh
  • 3,337
  • 1
  • 28
  • 39