Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Convert to C# #include <iostream> using namespace std; //function prototypes voi

ID: 3722062 • Letter: C

Question

Convert to C#

#include <iostream>

using namespace std;

//function prototypes

void displayMonthly(double []);

void displayTotal(double []);

int main()

{                  

       //declare variable and array

                    int choice = 0;

                    double rainfall[12] = {0.0};

    

    for (int x = 0; x < 12; x += 1)

    {     //get rainfall amounts

                    cout << "Enter rainfall for month " << x + 1 << ": ";

                    cin >> rainfall[x];

     } //end for

   do

    {     //display menu and get menu choice

                    cout << endl;

                    cout << "1 Display monthly amounts" << endl;

                    cout << "2 Display total amount" << endl;

                    cout << "3 End program" << endl;

                    cout << "Enter your choice: ";

                    cin >> choice;

                              

        //call appropriate function or end program

                    if (choice == 1)

                                         displayMonthly(rainfall);

                    else if (choice == 2)

                                         displayTotal(rainfall);

                    //end ifs

    } while (choice == 1 || choice == 2);

    return 0;

}   //end of main function

//*****function definitions*****

void displayMonthly(double rain[])

{   

                    cout << "Monthly rainfall amounts:" << endl;

                    for (int x = 0; x < 12; x += 1)

                                         cout << rain[x] << endl;

                    //end for

} //end of displayMonthly function

void displayTotal(double rain[])

{

                    double total = 0.0;                           

                    for (int x = 0; x < 12; x += 1)

                                         total = total + rain[x];

                    //end for

                    cout << "Total rainfall: " << total << endl;

} //end of displayTotal function

Explanation / Answer

Please find below the source code rewritten in c# language

using System;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args)
{

int choice;
double[] rainfall = new double[12];

for (int i = 0; i < 12; i++)
{
//Console.write function will allow the user to provide input in the same line
//Console.writeline will request for user input in the next line
Console.Write("Enter rainfall for month "+i+": ");
rainfall[i] = double.Parse(Console.ReadLine());
}

do
{
//Equivalent for endl in c++
Console.WriteLine();
Console.WriteLine("1 Display monthly amounts");
Console.WriteLine("2 Display total amount");
Console.WriteLine("3 End program");
Console.Write("Enter your choice: ");
choice = int.Parse(Console.ReadLine());

if (choice == 1)
{
displayMonthly(rainfall);
}
else if (choice == 2)
{
displayTotal(rainfall);
}


} while (choice == 1 || choice == 2);
}
/// <summary>
/// monthly display function
/// </summary>
/// <param name="rain"></param>
static void displayMonthly(double[] rain)
{
Console.Write("Monthly rainfall amounts:");
foreach (double entry in rain)
Console.Write(" "+entry);
Console.WriteLine();
}
/// <summary>
/// Display total function
/// </summary>
/// <param name="rain"></param>
static void displayTotal(double[] rain) {

double total = 0.0;

foreach(double entry in rain)
total += entry;

Console.WriteLine("Total rainfall: "+total);

} //end of displayTotal function
}
}