hi.. just asking of what code am i going to use to merge
arrays?
for example.
the output should be..Code:intA[5]={1,2,3,4,5}; intB[5]={2,4,6,8,10};
1,2,3,4,5,6,8,10
please help
advance thanks ^_^
hi.. just asking of what code am i going to use to merge
arrays?
for example.
the output should be..Code:intA[5]={1,2,3,4,5}; intB[5]={2,4,6,8,10};
1,2,3,4,5,6,8,10
please help
advance thanks ^_^
Make a new array, or reallocate an array. But since these arrays are static, fixed-size, you can't reallocate them, so a new might be necessary. If you have fixed-width arrays, you can define a new fixed-size array which is the length of the two combined. Otherwise you need to allocate a new array dynamically via malloc/free.
But if you only want to print out the data, you don't really need to merge the arrays at all.
nono..thats just for example what i mean is like this
Enter values for the first array
[code]scanf("%d",&a[5]);[\code]
Enter values for the second array
[code]scanf("%d",&b[5]);[\code]
for example the user supplied numbers of 5, 9 , 11, 13 , 15
and 1, 5, 9 11, 14
the output should be like this..
1, 5, 9, 11, 13, 14, 15
So basically, you don't have to store the merged arrays?
And you want to ignore repeats... why not add the element from A providing if it's not in B to C, and the same for B (providing it's not in A) add it to C. Then order C... And output it.
The max size of C will be, sizeof(A) + sizeof(B).
1. Input A (not with your broken code - but with a loop)
2. Input B
3. Allocate C big enough to store members of 2 arays
4. Copy A to C
5. for each member of B that is not found in A - copy to C
6. sort C
for example
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
- Input both arrays, A and B,
- Allocate a new one that's size sizeof(A)+sizeof(B)
- Put numbers in new array - use a loop.
- Delete the repeats.
- Sort it - bubblesort is easy to implement.
Now code.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
dude how can i copy and allocate the members of the array to C?
heres my code.. i dont know what to do next..
Code:#include<stdio.h> void main() { int a[5]; int b[5]; int c[10][10]; int d; clrscr(); printf("\n\nEnter values of first array:\n"); for(d=0;d<5;d++) {scanf("%d",&a[5]);} printf("\n\nEnter values of second array:\n"); for(d=0;d<5;d++) {scanf("%d",&b[5]);}
Heheh, acohrockz21, have you heard of merge sort?better sort first - then output ignoring duplicates (only works if A and B do not have duplicates from the beginning)
By the way, void main() should be int() main(), clrscr() is non-standard and not needed here, and you should indent your code a little better.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Indent properly and you'll see you're missing an ending }.
And don't use void main. Use int main. And avoid clrscr. It's non-standard.
It also seems to me that you need to go back to the books because this is code that will get you nowhere. It's not even functional right now.
If the output doesn't have to be sorted I would just make a union of the two arrays, a and b.
But that's why I told you to consult the books a little more. To try to grasp the more basics concepts before moving on.
Don't worry, everyone starts off as a beginner. You just need practice.
I can highlight the problems in the code if it makes you feel better.
int c[10][10];
The array is a 2D array of 10 x 10 elements, which is too much. You don't need all that space.
This code loops 5 time and writes the input to the 5th (or rather 6th) element of the array, which btw, does not exist. When you create an array of 5 elements (such as int a[5]), then you get elements 0 to 4 (number of elements - 1, since indexing starts from 0, not 1).Code:for(d=0;d<5;d++) { scanf("%d",&a[5]); }
Writing over the same data over and over is not very productive and writing beyond the end of the array is a security risk.
In itself, a, b, c and d are not very good variable names. They tell nothing of what they're used for, so it's better to use productive names that tells what they are used for.
And finally, you need to indent better so you can match all the { and } correctly. If you need help indenting, use a code beautifier or an IDE that indents for you, such as Visual Studio (Express).
Welcome to C.
Good luck.