Merge Arrays

Merge Arrays

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 6376 Views - Last Post: 19 February 2010 - 09:52 AM Rate Topic: -----

#1 hern84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-February 07

Merge Arrays

Posted 20 February 2007 - 09:30 PM

Can somebody help me merging two arrays??? PLease!!! :(

I have this two arrays

FIRST ARRAY a[ 5]={3.2, 4.3, 6.0, 7.2, 9.2 };
SECOND ARRAY b[4 ]={2.1, 3.2 , 5.3, 8.6};




THIRD ARRAY results[ 8 ]={2.1, 3.2, 4.3, 5.3, 6.0, 7.2, 8.6, 9.2};

I NEED TWO ADD THE FIRST TWO ARRAYS INTO THE THIRD ONE AND CHECK THAT THEY DONT REPEAT.

IM LOST!!!

Is This A Good Question/Topic? 0
  • +

Replies To: Merge Arrays

#2 eXceed69  Icon User is offline

  • "Super Sentai Knight Of DawN"
  • member icon

Reputation: 7
  • View blog
  • Posts: 688
  • Joined: 12-November 06

Re: Merge Arrays

Posted 20 February 2007 - 09:33 PM

let:
b=first array
c=second array
a=third array

for(float i=0; i>0; i++)
{ 
 a[i]=b[i]+c[i];
}



Quote

I NEED TWO ADD THE FIRST TWO ARRAYS INTO THE THIRD ONE AND CHECK THAT THEY DONT REPEAT.

This post has been edited by eXceed69: 20 February 2007 - 09:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 sidharth  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 10-November 06

Re: Merge Arrays

Posted 20 February 2007 - 09:40 PM

i don't think so if exceed is right
& clealy it is a homework question , firstly post ur try then we shall help
Was This Post Helpful? 0
  • +
  • -

#4 hern84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-February 07

Re: Merge Arrays

Posted 20 February 2007 - 09:47 PM

View Postsidharth, on 20 Feb, 2007 - 09:40 PM, said:

i don't think so if exceed is right
& clealy it is a homework question , firstly post ur try then we shall help



ITS A PART ON MY LAB FOR SCHOOL, IM HAVE THE TWO ARRAYS BUT NO IDEA HOW TO ADD THEM AND CHECK IF THEY DONT REPEAT, IM BEGINNER, I DONT KNOW TWO MUCH OF C++
Was This Post Helpful? 0
  • +
  • -

#5 sidharth  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 10-November 06

Re: Merge Arrays

Posted 20 February 2007 - 09:50 PM

Quote

ITS A PART ON MY LAB FOR SCHOOL, IM HAVE THE TWO ARRAYS BUT NO IDEA HOW TO ADD THEM AND CHECK IF THEY DONT REPEAT, IM BEGINNER, I DONT KNOW TWO MUCH OF C++

THAT IS WHY i am telling u to learn it rather then ask
Was This Post Helpful? 0
  • +
  • -

#6 eXceed69  Icon User is offline

  • "Super Sentai Knight Of DawN"
  • member icon

Reputation: 7
  • View blog
  • Posts: 688
  • Joined: 12-November 06

Re: Merge Arrays

Posted 20 February 2007 - 09:51 PM

I already given you a template to used, sidhart wanted to say is just post your code :D

The forum commitment to the community is just to give you guidance or consultation or simply helping mechanism to member/other. :D
Was This Post Helpful? 0
  • +
  • -

#7 hern84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-February 07

Re: Merge Arrays

Post icon  Posted 20 February 2007 - 10:00 PM

View PosteXceed69, on 20 Feb, 2007 - 09:51 PM, said:

I already given you a template to used, sidhart wanted to say is just post your code :D

The forum commitment to the community is just to give you guidance or consultation or simply helping mechanism to member/other. :D



OK LET ME TRY TO DO IT AND I POST MY CODE; GIVE ME A SEC
Was This Post Helpful? 0
  • +
  • -

#8 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1877
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Merge Arrays

Posted 20 February 2007 - 10:04 PM

Can we lose the caps lock?

And hern, exceed and sidharth want to help you, but need to know where to start. Posting your code is not only a rule here at dream.in.code, it will help others help you.
Was This Post Helpful? 0
  • +
  • -

#9 hern84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-February 07

Re: Merge Arrays

Posted 20 February 2007 - 10:15 PM

THIS IS WHAT IM TRYING TO DO, IM HAVING ERRORS, I DONT KNOW WHY, IM TRYNG TO LEARN BUT INMY BOOKS THEY DONT EXPLAIN ARRAYS A LOT





#include

<iostream> 
using

namespace std; 
 

 

int

main () 
{


int i; 

float a[5]={3.2,4.3,6.0,7.2,9.2}; 

float b[4]={2.1,3.2,5.3,8.6}; 

float c[20]; 

for (float i=0; i>0; i++) 
{

c[i]=a[i]+b[i];

}

 

 

 

 

}

 



------ Rebuild All started: Project: Lab6, Configuration: Debug Win32 ------

Deleting intermediate and output files for project 'Lab6', configuration 'Debug|Win32'

Compiling...

1.cpp

Lab6.cpp

c:\users\enrique\documents\visual studio 2005\projects\lab6\lab6\lab6.cpp(12) : warning C4305: 'initializing' : truncation from 'double' to 'float'

c:\users\enrique\documents\visual studio 2005\projects\lab6\lab6\lab6.cpp(12) : warning C4305: 'initializing' : truncation from 'double' to 'float'

c:\users\enrique\documents\visual studio 2005\projects\lab6\lab6\lab6.cpp(12) : warning C4305: 'initializing' : truncation from 'double' to 'float'

c:\users\enrique\documents\visual studio 2005\projects\lab6\lab6\lab6.cpp(12) : warning C4305: 'initializing' : truncation from 'double' to 'float'

c:\users\enrique\documents\visual studio 2005\projects\lab6\lab6\lab6.cpp(13) : warning C4305: 'initializing' : truncation from 'double' to 'float'

c:\users\enrique\documents\visual studio 2005\projects\lab6\lab6\lab6.cpp(13) : warning C4305: 'initializing' : truncation from 'double' to 'float'

c:\users\enrique\documents\visual studio 2005\projects\lab6\lab6\lab6.cpp(13) : warning C4305: 'initializing' : truncation from 'double' to 'float'

c:\users\enrique\documents\visual studio 2005\projects\lab6\lab6\lab6.cpp(13) : warning C4305: 'initializing' : truncation from 'double' to 'float'

c:\users\enrique\documents\visual studio 2005\projects\lab6\lab6\lab6.cpp(18) : error C2108: subscript is not of integral type

c:\users\enrique\documents\visual studio 2005\projects\lab6\lab6\lab6.cpp(18) : error C2108: subscript is not of integral type

c:\users\enrique\documents\visual studio 2005\projects\lab6\lab6\lab6.cpp(18) : error C2108: subscript is not of integral type

Generating Code...

Build log was saved at "file://c:\Users\Enrique\Documents\Visual Studio 2005\Projects\Lab6\Lab6\Debug\BuildLog.htm"

Lab6 - 3 error(s), 8 warning(s)

========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
[/cod]
Was This Post Helpful? 0
  • +
  • -

#10 eXceed69  Icon User is offline

  • "Super Sentai Knight Of DawN"
  • member icon

Reputation: 7
  • View blog
  • Posts: 688
  • Joined: 12-November 06

Re: Merge Arrays

Posted 20 February 2007 - 10:47 PM

The identifier is converted to a smaller type, resulting in loss of information.
int main () 
{


int i; 

float a[5]={3.2,4.3,6.0,7.2,9.2}; 

float b[4]={2.1,3.2,5.3,8.6}; 

float c[20]; 

for (i=0; i>0; i++) 
{

c[i]=a[i]+b[i];

}
}




One thing, array1 and array2 are not the same with its indexes. try to browse the DIC tutorials on c++ on right side.
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Merge Arrays

Posted 21 February 2007 - 12:21 AM

From what you posted in your original code you seem to be on the wrong path. You said you needed to merge the two array to get the resulting array {2.1, 3.2, 4.3, 5.3, 6.0, 7.2, 8.6, 9.2}. This is NOT the same as adding array elements.

here is a ruff and tuff way of doing it.

use a for loop to add all of a[] to c[]
use another for loop to add all of b[] to c[]
however this time we want to check to see if the element is already in c
before we add it to the end.
so we need a var to keep track of the the ending (so we know where to add the new value).

Try to code it.
Was This Post Helpful? 0
  • +
  • -

#12 sidharth  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 10-November 06

Re: Merge Arrays

Posted 21 February 2007 - 09:18 PM

hern , i had tol;d u earleir also that exceed has not given the correct template . what u have done is only used that template without using intelligence
so firstly learn basics which u can learn fron tutorials & then write the snippet
Was This Post Helpful? 0
  • +
  • -

#13 hern84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-February 07

Re: Merge Arrays

Post icon  Posted 21 February 2007 - 10:36 PM

Hi! Im still with the array problem, If somebody can give me an idea how to eliminate a number that is being duplicated in the result array???
I am trying to add two arrays into a third one, but numbers cant duplicate.




#include <iostream>
#include <string>
using namespace std;
void mergeArray(float a[],int sizeA, float b[],int sizeB,float result[],int *size);
void display (float c[],int num);

void main ()

{
	float a[]={3.2, 4.3, 6.0, 7.2, 9.2 };
	float b[]={2.1, 3.2 , 5.3, 8.6};
	float result [100];
	int size;
	mergeArray(a,5,b,4,result,&size);
	display(result,size);
}



void mergeArray(float a[],int sizeA, float b[],int sizeB,float result[],int *size)
{
	for (int i=0; i<sizeA;i++)
		result [i]=a[i];

			for (int i=0;i<sizeB;i++)
				result[sizeA+i]=b[i];
			*size=sizeA+sizeB;


}



void display (float c[],int num)
{
	for (int i=0; i<num;i++)
		cout<<c[i]<<"  ";
}


Was This Post Helpful? 0
  • +
  • -

#14 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Merge Arrays

Posted 21 February 2007 - 11:11 PM

Topics merged.

Please do not create additional threads on the same subject. Keep them together in one thread.

Thank you.
Was This Post Helpful? 0
  • +
  • -

#15 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Merge Arrays

Posted 21 February 2007 - 11:46 PM

void mergeArray(float a[],int sizeA, float b[],int sizeB,float result[],int *size)
{
	for (int i=0; i<sizeA;i++)
		result [i]=a[i];

			for (int i=0;i<sizeB;i++)
				result[sizeA+i]=b[i];
			*size=sizeA+sizeB;
}

Now we have something to work with. To ensure that you don't have duplicates you can check all the previously added elements.

this requres another loop and in If statement or two. Loop from result[0] to result[max] and see if there is a match, if you reach max without finding on then you add the element and add 1 to max... then get the next element of b and try again. etc.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2