Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
arr[]={1,3,2,4,6,7,8,9,10, 11,12,16,5,8,7,8,4,6, 8,9,10, 11,12,16,2,4, 5};
split=2; // the arr[] will be divide in 2(no of splits) parts each with equal elements split=1,2,3...n
skip=2; // skip is no of element to be skipped. skip=1,2,3...n size of res[] should be accordingly.
group=3 // vary between 1,2,3..n
then
res[0]=15 //1+4+8
res[1]=18//3+6+9
res[2]=19 //2+7+10
res[3]=24 //11+5+8
res[4]= 24 //12+8+4
res[5]= 29//16+7+6
res[6]=21 //8+11+2
res[7]=25 //9+12+4
res[8]=31 //10+16+5

and like wise split can be 1 to n.
arr[]={1,2,3...n} elements

What I have tried:

C#
int[] arr = { 1,3,2,4,6,7,8,9,10, 11,12,16,5,8,7,8,4,6, 8,9,10, 11,12,16,2,4, 5};
            int split = 2;
            int counter = arr.Length / (split);
            int skip = 2;
            int gor = 3;
            int len = arr.Length / gor;
            //int g1 = gor - 1;
            int[] tempArr = new int[counter];
            for (int idx = 0; idx < counter; idx++)
            {
                int sum = 0;
                int cn = 0;
                for (int i = idx; i < arr.Length; i = i + skip + 1)
                {
                    if (cn == gor)
                    {
                        break;

                    }
                    else
                    {
                        sum += arr[i];
                        cn++;
                    }
                }
                tempArr[idx] = sum;
            }


            for (int i = 0; i < tempArr.Length; i++)
            {
                MessageBox.Show(tempArr[i].ToString());
            }
        }
It gives first three correct others are giving wrong.
Posted
Updated 21-Dec-16 14:50pm
v2
Comments
Maciej Los 21-Dec-16 1:42am    
You should use debugger to find out where you made mistake in your code.
Kornfeld Eliyahu Peter 21-Dec-16 2:57am    
Your description of the problem unclear, the sample input and the explanation does not fit to the sample output by any logic...
1. split... so what if the number of elements is odd? What should we do with the parts we got?
2. skip... running linearly and skipping some elements... OK... and then what?
3. group... do you mean sum?
Simon_Whale 21-Dec-16 3:49am    
you have already asked this question yesterday. as partial solution was given to you https://www.codeproject.com/Questions/1161820/Please-help-me-to-do-this-sum-and-split-array-like

If you aren't sure what is going on, then decomplicate your software: write a method which splits your input array:
C#
private static int[] Split(int[] inp, int blocks = 2, int blockno = 0)
    {
    int len = inp.Length / blocks;
    int[] result = new int[len];
    int blockstart = blockno * len;
    for (int i = 0; i < len; i++)
        {
        result[i] = inp[blockstart + i];
        }
    return result;
    }

Then write a method which skips elements - it'll be similar to Split, but it'll skip elements when it creates the new array:
C#
private static int[] Skip(int[] inp, int count = 0)
    {
    ...
    return result;
    }
Then one to group them:
C#
private static int[] Group(int[] inp, int count = 2)
    {
    ...
    return result;
    }
That way, you aren't trying to make a complicated operation in one go - it's not as efficient as a single method to do it all, but it's a lot, lot easier to understand - and that means it's also probably a lot more reliable! It's also much, much easier to test because at each stage you are using the results of previous, tested operations.

But it's your homework, so I'll not write them for you!
 
Share this answer
 
Comments
Maciej Los 21-Dec-16 8:30am    
+5!
As I understand, this is how the split should be done:
C++
{1,3,2,4,6,7,8,9,10,11,12,16,5,8,7,8,4,6,8,9,10,11,12,16,2,4,5}
 0 1 2 0 1 2 0 1  2  3  4  5 3 4 5 3 4 5 6 7  8  6  7  8 6 7 8

Use the debugger to see where your program don't follow the pattern.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v3
Steps to do:

  1. Split an array into destination number of arrays (later called: subarrays). In this case you should get three 9-elements subarrays
  2. Loop through the data of each subarray and group data by using modulo of counter and the number of consecutive numbers to skip
  3. Calculate the sum for each group


Linq solution:
C#
int[] arr = new int[]{1,3,2,4,6,7,8,9,10,11,12,16,5,8,7,8,4,6,8,9,10,11,12,16,2,4,5};
int ConsecutiveNoToSkip = 2;
int NoOfGroups = 3;
int ItemsInGroup = arr.Length/NoOfGroups;

for(int i=0; i<NoOfGroups; i++)
{
	var grp = arr.Skip(ItemsInGroup*i)
			.Take(ItemsInGroup)
			.Select((x, counter) => new
			{
				Number=x,
				Counter=counter,
				GrpNo=counter%(ConsecutiveNoToSkip+1)
			})
			.GroupBy(x=>x.GrpNo)
			.Select(g=>new
			{
				Numbers = String.Join("+", g.Select(x=>x.Number)),
				Result = g.Sum(y=>y.Number)
			});
	//here you can display the data
}


Result:
subarray #1
Numbers Result
1+4+8   13 //not 15!
3+6+9   18 
2+7+10  19 


subarray #2
Numbers Result
11+5+8  24 
12+8+4  24 
16+7+6  29 


subarray #3
Numbers Result
8+11+2  21 
9+12+4  25 
10+16+5 31 
 
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