Merging Array

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 930 Views - Last Post: 19 February 2013 - 06:04 PM Rate Topic: -----

#1 Sandals456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 02-February 13

Merging Array

Posted 10 February 2013 - 04:11 PM

I need help with an assignment, I have to tell the user to input 13 integers in array 1 which will only take 13 integers, and 5 in array 2 which will only take 5 integers. After that, the two arrays will combine into array 1 to make 18 integers. I have the user put in both into array 1 and array 2, I just don't know how to combine them into array 1 because I can only use those two arrays and no third array to put into...

This is what I have so far.

#include<stdio.h>

int main()

{
        int array_a[18], array_b[5];
        int input1, input2;

        input1 = 0;

        printf("Please enter 13 integers: ");

                while(input1<13) /*This while loop will only take 13 integers for the first array*/
                {
                scanf("%d", &array_a[1]);

                input1++;
                }
        printf("Now enter 5 integers: ");

        input2 = 0;

                while(input2<5) /* And this one will only take 5 */
                {
                scanf("%d", &array_b[1]);

                input2++;
                }



return 0;
}





Is This A Good Question/Topic? 0
  • +

Replies To: Merging Array

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Merging Array

Posted 10 February 2013 - 04:27 PM

Quote

I have the user put in both into array 1 and array 2, I just don't know how to combine them into array 1 because I can only use those two arrays and no third array to put into...

If array 1 can hold 18 elements and you only put in 13 elements, then there are 5 empty spaces left in the array which can be filled by array2. After you input data into array2, iterate over array2 and add the elements to array1 from indices 13-17
Was This Post Helpful? 0
  • +
  • -

#3 Sandals456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 02-February 13

Re: Merging Array

Posted 10 February 2013 - 04:32 PM

Quote

If array 1 can hold 18 elements and you only put in 13 elements, then there are 5 empty spaces left in the array which can be filled by array2. After you input data into array2, iterate over array2 and add the elements to array1 from indices 13-17


How would I do it though? I have to have the combined array in ascending order.
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Merging Array

Posted 10 February 2013 - 05:09 PM

Quote

How would I do it though? I have to have the combined array in ascending order.

After you combine the arrays together, you can applying any of the sorting algorithms to sort array1
Was This Post Helpful? 0
  • +
  • -

#5 Sandals456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 02-February 13

Re: Merging Array

Posted 10 February 2013 - 05:26 PM

Quote

After you combine the arrays together, you can applying any of the sorting algorithms to sort array1


So besides that, everything is good?
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Merging Array

Posted 10 February 2013 - 05:30 PM

No, you are not loading the arrays correctly. For example, you are only loading element 1 of array_a1 from input.

while(input1<13) /*This while loop will only take 13 integers for the first array*/
{
   scanf("%d", &array_a[1]);

   input1++;
}



Rather than passing 1 as the array index argument, you should be passing input1. This will fill the array sequentially with input.


You have the same issue with array2 as well

This post has been edited by jjl: 10 February 2013 - 05:30 PM

Was This Post Helpful? 0
  • +
  • -

#7 Sandals456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 02-February 13

Re: Merging Array

Posted 10 February 2013 - 05:34 PM

View Postjjl, on 10 February 2013 - 05:30 PM, said:

No, you are not loading the arrays correctly. For example, you are only loading element 1 of array_a1 from input.

while(input1<13) /*This while loop will only take 13 integers for the first array*/
{
   scanf("%d", &array_a[1]);

   input1++;
}



Rather than passing 1 as the array index argument, you should be passing input1. This will fill the array sequentially with input.


You have the same issue with array2 as well


So then it will be;

while(input1<13) /*This while loop will only take 13 integers for the first array*/
{
   scanf("%d", &input1);

   input1++;
}


Was This Post Helpful? 0
  • +
  • -

#8 Sandals456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 02-February 13

Re: Merging Array

Posted 10 February 2013 - 05:45 PM

Nevermind, if I left it like the last post, it will always continue accepting integers so I did it like this:

while(input1<13)/*This will only accept 13 integers in array a*/
                {
                scanf("%d",array_a[input1]);/*It will input the integers into the array until the 13th integer*/

                input1++;

                }


Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Merging Array

Posted 10 February 2013 - 06:23 PM

while(input1<13)/*This will only accept 13 integers in array a*/
                {
                scanf("%d",array_a[input1]);/*It will input the integers into the array until the 13th integer*/

                input1++;

                }




This is incorrect, scanf accepts a pointer as it's parameter, however array_a[input1] is a integer. You need to pass the address of the array at input1 index.

while(input1<13) {
   scanf("%d", &array_a[input1]);
   input1++;
}




I suggest you brush up on arrays, because this method of guessing and checking is not an optimal way of debugging.

This post has been edited by jjl: 10 February 2013 - 06:24 PM

Was This Post Helpful? 0
  • +
  • -

#10 Sandals456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 02-February 13

Re: Merging Array

Posted 10 February 2013 - 09:20 PM

Quote

This is incorrect, scanf accepts a pointer as it's parameter, however array_a[input1] is a integer. You need to pass the address of the array at input1 index.

while(input1<13) {
   scanf("%d", &array_a[input1]);
   input1++;
}




I suggest you brush up on arrays, because this method of guessing and checking is not an optimal way of debugging.


Oh yeah sorry I didn't notice the mistake.
Was This Post Helpful? 0
  • +
  • -

#11 Sandals456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 02-February 13

Re: Merging Array

Posted 10 February 2013 - 10:14 PM

I don't know how to combine the two arrays...
Was This Post Helpful? 0
  • +
  • -

#12 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Merging Array

Posted 10 February 2013 - 10:25 PM

Quote

I don't know how to combine the two arrays...

Show us your attempt
Was This Post Helpful? 0
  • +
  • -

#13 Sandals456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 02-February 13

Re: Merging Array

Posted 10 February 2013 - 10:29 PM

I know it is completely wrong.

int array_a = array_a + array_b;



I looked up many examples but they include a third array.
Was This Post Helpful? 0
  • +
  • -

#14 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Merging Array

Posted 11 February 2013 - 12:14 AM

Quote

I know it is completely wrong.


Have a read : http://www.cplusplus...utorial/arrays/
Was This Post Helpful? 0
  • +
  • -

#15 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Merging Array

Posted 11 February 2013 - 10:34 AM

Sandals, do you want to:

*merge AND sort the two arrays into one array, at the same time, using a merge sort algorithm

or

*merge the array values first, THEN sort the array values, after they all get into one array.

In the first case, you would normally use a third array:
array 1\
        \
         -> array 3
        /
array 2/



But it CAN be done, it's just a bit more difficult to do it the first way, with only two arrays.

What do you need to do here?

This post has been edited by Adak: 11 February 2013 - 10:35 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2