Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi there,

Please provide a solution to calculating the first and last number in an array. like below.

So I have an array:
133286

I want to calculate the sum until I have only two numbers like so:

133286
7115
122
32



Thanks in Advance.

What I have tried:

I have tried using a for loop but it only runs once, how do I continuously run it till i have only two digits.
C#
int[] per = new int[1000];
            int sum = 0;
            Console.WriteLine("The % score:");
            for (int i = 0; i < count; i++)
            {
                per[i] = numbers[i] + numbers[count-1];
                count--;
                sum++;
                
                
            }
Posted
Updated 4-Oct-21 6:30am
v2
Comments
Richard MacCutchan 4-Oct-21 7:24am    
You need to adjust the end point of the array each time. So as you add the last number to the first you reduce the end point of the array by 1. So at the end of each loop the number of elements in the array will be something around half of the original. But you need to decide how many digits should remain as the final value, so you know when to stop the loop.

Rephrasing your question so it's more obvious:
133286
First = 1, last = 6: Sum = 7
Remaining
 3328
First = 3, last = 8: Sum = 11
Remaining
  32
First = 3, last = 2: Sum = 5
And it's clear that this is the same task repeated - so make your code reflect that.

The first thing to do is to put the code to work out the sums into a method, so you can call it repeatedly. Pass it the array and the start index, have it return the sum, and it's pretty simple to do.

When you have that working, add your loop to call it repeatedly.
Start by making that call the array the right number of times - sort out when the loop should exit first, and do something useful with the result your method return. What exactly is "useful" will depend on you, the environment yoru code is to run in, and what exactly you want to do with the result.
For example, a Console app may want to print each result, but a more general app may want to add them to a collection (a List or an Array maybe) for later processing. We don't know what your tutor wants you to do!

Then make it pass an incrementing start index to method, and you're done.

Think about the task, try it on paper, and you'll get it done in very little time.
 
Share this answer
 
Comments
CPallini 4-Oct-21 7:41am    
5.
CoderL18 4-Oct-21 8:18am    
I have my code to calculate the sum of the first and last in the array. My only problem is that I don't know how to make my loop to keep running until I only have two numbers like this. I am trying to do it without putting my code in another method.

• 22211111112
o 4
• 22211111112
o 43
• 22211111112
o 433
• 22211111112
o 4332
• 22211111112
o 43322
• 22211111112
o 433221
• 433221
o 5
• 433221
o 55
• 433221
o 555
• 555
o 10
• 555
o 105
• 105
o 6
• 105
o 60
OriginalGriff 4-Oct-21 8:40am    
If you have code that works, move it into a separate method and test it carefully - that way you can "isolate" known working code so you don't / can't accidentally change it and spend loads of time trying to work out what is wrong with the code around it. Putting it in a method means it's just there, a "black box" that you know does what you wanted it to, and no longer care how it does it.
This is an important part of software development, and even has a important sounding name: the "Single Responsibility Principle". We do it all the time, because it makes our code easier to read, easier to modify, and more reliable - so do it here as well and you can focus on the area that doesn't work yet!

Give it a try: it's really easy to do, and it makes your job a whole load easier!
Something like this, but probably needs refining:
C#
int start = 0;
int end = numbers.Length - 1;
while (end > 1)
{
    for (start = 0; start < end ; start++, end--)
    {
        Console.Write($"start: {start}, end: {end}");
        numbers[start] += numbers[end];
        Console.WriteLine($" numbers[start] += numbers[end]: {numbers[start]}");
        numbers[end] = 0;
    }
    Console.WriteLine($"start: {start}, end: {end}");

    for (int i = 0; i < 9 ; i++)
    {
        Console.Write($"{numbers[i]}, ");
    }
    Console.WriteLine("");
}
 
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