Fabricio - 6 days ago 5
C# Question

# Sum the values of each row in an 2d array and storage them in a 1d array

I´m trying to create a simple program using methods in C# that:

1. Ask the names of five workers using a 1D array

``````// Arrays used in the program
private string[] obrero = new string[5];
private int[,] produccion = new int[5,5];
private int[] totalprodu = new int[5];

public void Capture()
{
for (int i = 0; i < 5; i++)
{
int j = i + 1;
Console.WriteLine("Enter name for worker {0} ", j);
}
}
``````

2. Ask a salary for each day of the week (m-f) for each worker using a method and a 2D array

``````    public void CalculateSalaries()
{
string tmp;

for (int i = 0; i < 5; i++)
{
Console.WriteLine("For worker: " + obrero[i]);
Console.Write("Enter salary for monday: ");
produccion[i, 0] = Convert.ToInt32(tmp);

Console.Write("Enter salary for tuesday: ");
produccion[i, 1] = Convert.ToInt32(tmp);

Console.Write("Enter salary for wednesday: ");
produccion[i, 2] = Convert.ToInt32(tmp);

Console.Write("Enter salary for thursday: ");
produccion[i, 3] = Convert.ToInt32(tmp);

Console.Write("Enter salary for friday: ");
produccion[i, 4] = Convert.ToInt32(tmp);

// here goes the sum of each row using the totalprodu array
}
}
``````

3. Within the method CalculateSalaries() sum up the values of each row to get the weekly payment for each one of the five workers using a totalprodu array

This is my problem so far, I tried different methods to get the sum of values in each row, and then storage those results in each index of the totalprodu array with no success, this is one example

``````  // this code is wrong
int k;
for (int j = 0; j < 5; j++)
{
for (k = 0; k < 5; k++)
{
totalprodu[k] += produccion[j,k];
}
}
``````

Except for point 3, the program works ok when I call the methods from the main class (code not shown). I would really appreciate any help to solve this issue, thanks in advance!

Class Matriz complete:

``````using System;

namespace matrices
{
class Matriz
{
private string[] obrero = new string[5];
private int[,] produccion = new int[5,5];
private int[] totalprodu = new int[5];

public void Capture()
{
for (int i = 0; i < 5; i++)
{
int j = i + 1;
Console.WriteLine("Enter name for worker {0} ", j);
}
}

public void CalculateSalaries()
{
string tmp;

for (int i = 0; i < 5; i++)
{
Console.WriteLine("For worker: " + obrero[i]);
Console.Write("Enter salary for monday: ");
produccion[i, 0] = Convert.ToInt32(tmp);

Console.Write("Enter salary for tuesday: ");
produccion[i, 1] = Convert.ToInt32(tmp);

Console.Write("Enter salary for wednesday: ");
produccion[i, 2] = Convert.ToInt32(tmp);

Console.Write("Enter salary for tuesday: ");
produccion[i, 3] = Convert.ToInt32(tmp);

Console.Write("Enter salary for friday: ");
produccion[i, 4] = Convert.ToInt32(tmp);

// here goes the sum of each row using the totalprodu array
}
}

public void PrintTotals()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("\n" + totalprodu[i]);
}
}
}
}
``````

Apologies for my English, I'm not a native speaker.

You don't have to use so much loops and array's actually. `Obrero` is an entity, for an entity you mostly make a class that represents it.

## Example Obrero Class

``````public class Obrero
{
public string Name { get; set; }
public Dictionary<Day, decimal> Produccion { get; set; }
}
``````

## Improvement

• Make code readable, maintainable and reusable as good as you can. (You will surely learn on your path).
• Don't use arrays for the sake of using them
• Use the `decimal` type for money values

## Full example

I made an example that demonstrates how you could get a better approach of doing stuff. Hope it's useful for you, and you are able to pick up some small basic things.

(It's basically a copy paste example for you, so feel free to make a new console project, paste the code and try it out)

``````class Program
{
private static List<Obrero> _obreroList;

static void Main(string[] args)
{
PopulateObreros();

foreach (var obrero in _obreroList)

Console.WriteLine("Totals for each obrero:");
foreach (var obrero in _obreroList)
CalculateSalary(obrero);

}

private class Obrero
{
public string Name { get; set; }
public Dictionary<Day, decimal> Produccion { get; set; }
}

private enum Day
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}

private static void PopulateObreros()
{
_obreroList = new List<Obrero>
{
new Obrero {Name = "John"},
new Obrero {Name = "Marcy"},
new Obrero {Name = "Jimmy"}
};
}

{
obrero.Produccion = new Dictionary<Day, decimal>();
decimal salary;

Console.WriteLine("For worker: " + obrero.Name);

Console.Write("Enter salary for Monday: ");

Console.Write("Enter salary for Tuesday: ");

Console.Write("Enter salary for Wednesday: ");