Jump to content

Passing Parameters

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Alex_j

Alex_j

    Newbie

  • Members
  • PipPip
  • 29 posts
Hi, How can I pass a value from one method to another? Heres my code:

private static void GradePercentages(int[] GradesEntered)

        {

            double GradeAPercentage = (GradesEntered[0] / TotalAmountofGrades) * 100;

            Console.WriteLine("Grade A Percentage: {0}", GradeAPercentage);

        }







        private static void TotalAmountofGrades_Sum(int[] GradesEntered)//Total of Amount of Grades Sum Method

        {

           double  TotalAmountofGrades = GradesEntered[0] + GradesEntered[1] + GradesEntered[2] + GradesEntered[3] + GradesEntered[4] + GradesEntered[5];

            Console.WriteLine("Total Amount of Grades: {0}", TotalAmountofGrades);

        }

I want to pass the value of the total amount of grades into the method to work out the percentage.

Thanks

#2
FlashM

FlashM

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
I don't really understand what you are trying to do, but you can use methods and pass them parameters like this:



static void Main(string[] args)
{
    int[] grades = new int[5];
    grades[0] = 5;
    grades[1] = 7;
    grades[2] = 3;
    grades[3] = 10;
    grades[4] = 8;

    double gradesPercentage = CalculateAverage(grades);
    ShowAverage(gradesPercentage);

    Console.ReadLine();
}


//Your CalculateAverage method has a return type of type double
private static double CalculateAverage(int[] gradesEntered)
{
    double gradesSum = 0;

    //Sum all grades in your 'gradesEntered' int array parameter
    foreach (int grade in gradesEntered)
    {
        gradesSum += grade;
    }

    //CalculateAverage method returns average grade
    return gradesSum / gradesEntered.Count();
}


//Your ShowAverage method has no return type or to be more precise, it returns void.
private static void ShowAverage(double average)
{
    Console.WriteLine("Grades average: {0}", average);
}


#3
FlashM

FlashM

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
Of course it is desirable to verify your input parameters data before using them in method:


private static double CalculateAverage(int[] gradesEntered)
{
    if (gradesEntered == null)
        throw new ArgumentNullException("GradesEntered parameter is null.");
    
    ...
    ...
}


#4
Alex_j

Alex_j

    Newbie

  • Members
  • PipPip
  • 29 posts
I wanted to try and pass the value of TotalAmountofGrades in the TotalAmountofGrades_Sum method, into the GradePercentages paramter, also can you explain this bit:

   foreach (int grade in gradesEntered)

    {

        gradesSum += grade;

    }


Is it just another way of finding the sum of the array??

#5
FlashM

FlashM

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
This is a foreach loop, a kind of mechanism that iterates through all the elements in the array. Inside foreach loop you can do whatever with this element. I used the loop to sum all the elements.


Here are few more examples to make things more clear:



foreach (int grade in grades)

{

    //Using the foreach loop only to output the grade value to console

    Console.WriteLine("Grade is: {0}", grade);

   

    //Add each grade new value and output it to console

    Console.WriteLine("New grade value: {0}", grade + 1);

}



//Foreach loop is almost identical to For-Next loop and the result is the same

for (int i = 0; i < grades.GetUpperBound(0); i++)

{

    Console.WriteLine("Grade value: {0}", grades[i]);

}



#6
Alex_j

Alex_j

    Newbie

  • Members
  • PipPip
  • 29 posts
I managed to get it working as I wanted however, I have to work out the individual percentage and use this code:


        public double GradePercentages_Sum(int[] GradesEntered, double total)

        {

            return ((GradesEntered[0] / total) * 100);

                              

        }


Could you use a for loop to make the [0] change to [1],[2] etc then output it??

Or would it be easier to just put all of the percentage calculations in one method?