Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have
int[] array1 = {1,2,3,4,2,4,5}
int[] array2 = {7,8,9,10,4.4,2,3}

i want output like below

int[] array1 = {1,2,3,4,5}
int[] array2 = {7,12.4,12,10,4.4,3}

addition of duplicates - like 2 is repeated and 2 has 8,4.4 values in array2
same as 4 is repeated in array1 and it has values 10 and 2 in array2
now i want show value for 2 is 12.4 and value for 4 is 12.

What I have tried:

tried below solution
class Program
	{
		public static void Main(string[] args)
		{
			int [] sArray = {10,20,30,40,40,50,60};
			var sList = new ArrayList();
			
			for (int i = 0; i < sArray.Length; i++) {
			    if (sList.Contains(sArray[i]) == false) {
			        sList.Add(sArray[i]);
			    }
			}
			
			var sNew = sList.ToArray();
			
			for (int i = 0; i < sNew.Length; i++) {
			    Console.Write(sNew[i]);
			}
			Console.ReadLine();
		}
	}
Posted
Updated 13-Jan-17 8:17am
Comments
Jon McKee 13-Jan-17 3:04am    
Your output for array #1 and #2 are vastly different. Array #1 to modified array #1 makes sense, but from array #2 to modified array #2 makes no discernible sense. Could you elaborate on the conversion between array #2 and modified array #2?
Kumud Dumbre 13-Jan-17 3:56am    
yeah its difficult but there should be solution for this which i am unable to find , my question is array1 has values {1,2,3,4,2,4,5} and array2 has values {7,8,9,10,4.4,2,3} now i want to do addition in array2 on the basis of duplicates of array1 ; like 2 repeated 2 times in array1 so i want to do addition of 8 and 4.4 from array2 also 4 repeated 2 times in array1 so i want to do addition of 10 and 2 from array2 , so output shuld be like array1 {1,2,3,4,5} and array2{7,12.4,9,12,3}
Jon McKee 13-Jan-17 4:03am    
I'd try to help, honestly, but I'm still not sure what you're after. Addition of 8 and 4,4 would yield 16. Plus I'm not sure on the rules regarding what should be added to array #1 in what position and what should be ignored. For example, why is 7 not modified by array #2? From my standpoint, there seems to be no relationship between modified values and the index in array #2.
Kumud Dumbre 13-Jan-17 4:07am    
i have repeated numbers in array1 are 2 and 4 and their values in array2 are 8,4.4 for number 2 and 10,2 for number 4 of array1, now i just want to do addition of numbers in array2 on the basis of index of array1 so it will look like array1 {1,2,3,4,5} and array2{7,12.4,9,12,3}
Kumud Dumbre 13-Jan-17 4:08am    
i am really thankful to you Jon McKee for helping me.

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!
I'll give you a hint: Your sList contains all the numbers that are duplicated in sArray - so you need to use that information to find all the numbers in the second array with matching indexes in the original array - you aren't doing that, you are printing the numbers that are duplicated, rather than anything to do with the "second array".
Personally, I'd so it very differently - I'd create a class (or struct) which contained the value, the number of times it was encountered, and the total value from the second array, then store that in an array of unique values, so that I only need to pass through the first array once. I'd then print from that array the ones with a count greater than one. But that may be a little advanced for you, so try it with a second loop to add up the values for each repeat.

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
Jon McKee 13-Jan-17 3:12am    
Gave him the benefit of the doubt, but I can't agree more with this. Homework is designed to make you think. Without the ability to think you are no more than a copy-paste bot for which google could do your job.
OriginalGriff 13-Jan-17 3:23am    
Worse: "Developement homework" is there to teach you *how* to think! :laugh:
Jon McKee 13-Jan-17 3:26am    
Why think when you can just ask CP or SO? :laugh:
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

as I understand the problem:
Each time you find a duplicate in array1 at position x and y,
    you add array2[y] to array2[x]
    remove array2[y] and array1[y]

You just have to translate this to code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900