how can i merge the values of the arrays?

if i get the values of each array in a file.txt and file2.txt

void ReadList()
{

	ifstream fin;
	fin.open ("1.txt");
	for(A=0; A<10; A++)
		fin >> Array[A];
	fin.close();

	ifstream fin2;
	fin2.open("2.txt");
	for(B=0; B<20; B++)
		fin2 >> Array[B];
	fin2.close();

	N=A+B;

	cout << Array[N];


}

in N=A+B.. im not sure.. how?

when I run the program, only the 10 numbers in A appears.. B is not included..

don't know of a clever way but using 2 arrays would work. read into array1 then into array2 then have a loop which adds the value in each part of array2 to each part of array1 then output array1.

>>how can i merge the values of the arrays?

Merge how? Do you want to take 10 values from the first file and twenty values from the second text file and end up with an array of 30 values where the 20 values of 2.txt are appended to the ten from 1.txt(i.e. concatenation)?

If so, change line 12 from this:

for(B=0; B<20; B++)

to this:

for(B=10; B<30; B++)

so the values from 1.txt do not get overwritten.

I have no idea what you are trying to do in line 18.

the numbers became upto 40.. then only 20 numbers from 2.txt appear.. 20-40 numbers are with the value 0

There must be relevant code (i.e. declaration of the array and display of the array) that isn't posted. Line 18 isn't going to display the array. If you make the change I suggested, you should end up with an array with thirty values from the two files.

What is the type of the array (int, char, double, ...)?

what will i do?? it's type of array is int

>> what will i do??

Make the change I suggested and, provided the array declaration is correct and the files exist and have numbers in them and you have the proper permissions, you'll end up with an array where the first 30 elements are numbers from those files. I can't comment on any other than what you've posted. In particular, I have no idea where you're displaying the array. I reiterate that I have no idea what you're trying to do in line 18. You might want to add some checks to make sure those files are actually opening.

I presumed addition of the two without noticing they were of different size:$
Disregard my first post.

This is what I ended up coming up with. First you want to make sure you can open your file. If you cannot open your file, your arrays will automatically be filled with junk. That may be half of your problem.

After that, I just used cout << as a basic step by step debugger to make sure my input into the array was truly valid.

I also changed this from a void() to a main() as I didn't have anything else to go by. Good luck to you and hope this answers your question.

#include <iostream>
#include<fstream>
using namespace std;

int main() {

	int ArrayA[10], ArrayB[20], ArrayN[20];

	ifstream fin;
	fin.open ("1.txt");
	if(fin.fail() ){
		cout << "Input file failed to open.\n";
		exit(1);
	}

	for(int index = 0; index < 10; index++)
		fin >> ArrayA[index];

	for(int index = 0; index < 10; index++)
		cout << "The Array A["<< index << "] " <<ArrayA[index] << endl;


	fin.close();

	ifstream fin2;
	fin2.open("2.txt");
	if(fin2.fail() ){
		cout << "Input file failed to open.\n";
		exit(1);
	}

	for(int index=0; index<20; index++)
		fin2 >> ArrayB[index];

	for(int index = 0; index < 20; index++)
		cout << "The Array B["<< index << "] " <<ArrayB[index] << endl;

	fin2.close();

	//N=A+B to the first 10 in the array	
	for(int index=0; index < 10; index++)
		ArrayN[index] = ArrayA[index] + ArrayB[index]	;	

	// For N[10 - 19] = B[10-19] in the array as ArrayA only goes up to 9, not 19.
	for(int index = 10; index < 20; index++)
		ArrayN[index]  = ArrayB[index];

	// cout << N
	for(int index = 0; index < 20; index++)
		cout << "The Array N["<< index << "] " <<ArrayN[index] << endl;

	return 0;


}

This snippet of code is assuming you have 2 arrays, you want ArrayA to add to ArrayB, even though they are different sizes, to be placed in ArrayN. I read in the previous posts of Arrays 20 to 40. I have no clue what you are talking about there as ArrayA only reads the first 10 and ArrayB reads the first 20 in each respective input files. Unless of course you were looking for ArrayN[0 - 9] = ArrayA[0-9] and also ArrayN[10-29] = ArrayB[0-19]. If that were the case, then you can easily switch around my N=A+B that I created above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.