Question

im doing a calculator, this is the code:

If i go to the Multiplication function and i enter for example i want to multiple 2*2*2 this code it's outputting 4 8 4 I dont understand why, well i know that 2*2 = 4 * 2 = 8 but why the last 4? and how can i only get the result without getting the complete series?, when i try to do the Console WriteLine outside the for loops it throws an error. it doesn't let me.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {

        static void LetsSum(int [] myArray)
        {
            int sum = myArray.Sum();
            Console.WriteLine(sum);     
        }

        static void  letsMult(int [] myArray)
        {  
            for (int a = 0; a < myArray.Length; a++ )
            {
                for (int b = a+1; b < myArray.Length; b++ )
                {
                int multip =  myArray[a] *= myArray[b];
                Console.WriteLine(multip);

                }

            }

        }

        static void Main(string[] args)
        {

            //First Variable total of numbers from users input
            int TotalNumb = 0;           
            //String to receive users input
            string TotalN = string.Empty;
            //Specifying size of array
            Console.WriteLine("Please specify how many numbers do you want to do the math");
            TotalN = Console.ReadLine();
            //Converte because readline it's a string
            TotalNumb = Convert.ToInt32(TotalN);
            //Name of ARRAY and passing the value selected by the user
            int[] myArray = new int[TotalNumb];

            //counter for the loop
            int i = 0;
                    for (i = 0; i < TotalNumb; i++ )
                    {
                        Console.WriteLine("Enter your number");

                        myArray[i] = Convert.ToInt32(Console.ReadLine());
                    }

            Console.WriteLine("Please enter SUM, LESS, MULT, DIV");
            string ToDo = Console.ReadLine();


                    if (ToDo == "SUM")
                    {
                        Program.LetsSum(myArray);
                    }

                    if (ToDo == "MULT")
                    {
                        Program.letsMult(myArray);
                    }

                    Console.Read();
        }
    }
}
Was it helpful?

Solution

Very simple... alter your code like as bellow...

 static void  letsMult(int [] myArray)
        {  
            int output=1;

            for (int a = 0; a < myArray.Length; a++ )
            {
                  output=output*myArray[a];
                  Console.WriteLine(output);
            }    
        }

OTHER TIPS

The error message is because you're declaring int multip in the innermost scope of the for loop. The int goes out of scope after that, and is not usable. If you want multip to be accessible outside of the for loop, declare it outside of the for loop:

    static void  letsMult(int [] myArray)
    {  
        int multip = 1;
        for (int a = 0; a < myArray.Length; a++ )
        {
            for (int b = a+1; b < myArray.Length; b++ )
            {
              multip =  myArray[a] *= myArray[b];
            }

        }
        Console.WriteLine(multip);
    }

Note, it's declared above the for loop, but used in the loop, and outputted outside the loop, but still in its scope (which in my case is "the entire function").

You still have a logical issue. Are you sure you need a nested loop? How would you describe to a person how to multiply a list of numbers to get the total product?

You have 3 Elements in your Array

[2, 2, 2]

Then you multiply 0 and 1 and output the result:

[4, 2, 2] OUTPUT: 4

Then you multiply 0 and 2 and output the result:

[8, 2, 2] OUTPUT: 8

The n you multiply 1 and 2 and output the result:

[8, 4, 2] OUTPUT: 8

Perhaps you want to remove the outer loop, in this case your Output would be

4, 8

You can do this with LINQ's Aggregate():

for sum :

  int[] arr1 = new int[] { 2, 2, 2 };
  int result = arr1.Aggregate((a, b) => b + a);
 //or
  int result = arr1.Sum();

for multiplication :

  int[] arr1 = new int[] { 2, 2, 2 };
  int result = arr1.Aggregate((a, b) => b * a);

I guess you want to display each multiplacation result.If so you can do the following:

int multip = myArray[0];
for (int a = 1; a < myArray.Length; a++ )
{
    Console.Write("{0} * {1} = ",multip, myArray[a]);
    multip *= myArray[a];
    Console.WriteLine(multip);
}

use

int multip=1;

for (int a = 0; a < myArray.Length; a++ )
            {

                 multip  *= myArray[a];
                Console.WriteLine(multip);

                }

            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top