Sum of minimals coding problem
Have you worked before with two dimensional arrays? This will be small and simple programming exercise. Write a function, that takes an two dimensional array of integers and finds the minimum value in each row. And then returns the sum of all of the minimum values.
Example: [1,2,3] //the lowest value is 1 [6,2,8] //2 [9,9,9] //and 9 So the function should return 12 //the sum of 1+2+9
Scroll down for the solution of this programming exercise for beginners
public static int SumOfMinimums(int[,] num) { //First lets get some variables (space in memory) //GetLength(0) will give us the size of the first dimension //GetLength(1) will give us the size of the second dimension //GetLength(2) will give us the size of the … //well we only have two dimensions right now //Do you remember how to get the minimum number in an array ? //If not try to make a program that does only that first. int a = num.GetLength(0); int b = num.GetLength(1); int sum = 0; int min = num[0,0]; //we will go through every number in each row //and find the minimum value //then we will add it to "sum" //we will use nested for loops. If you don't remember //how to use them, make a simple program that //use two nested for loops and examine the results for yourself for (int i = 0; i < a; i++) { min = num[i, 0]; for (int j = 0; j < b; j++) { if (num[i, j] < min) min = num[i, j]; } sum += min; } //and return the sum when finished return sum; }