Click here to Skip to main content
15,885,113 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I need to write a program that calculates the grade of a students semester. I can only use one function and the student is to enter how many grades there are in each category... (ex. "How many number of exams?" students response "5,2,90" whatever they choose and then they enter the grade for each number of exams, homework etc. After that is done the program should calculate the average and display it to the user and that is where I'm having the most difficulty. I dont know how to display the average for each individual category and then have the program continue to the next category. In the end I must take all of these averages and calculate a Final total grade and I dont know how to do that since all of the averages are to be calculated under one function.. this is my entire code so far..any input would be greatly appreciated.. I'm not asking you to do my work but for some guidance at least.. Sorry for how messy it appears but I'm newer to the forum Thank you

namespace HW2
{
  class IntroCS
  {
    public static void Main (string[] args)
    {
      WeightTotal ();

      double exAvg = FindAvg("exam");
      double hwAvg = FindAvg("homework");
      double labAvg = FindAvg("lab");
      double parAvg = FindAvg("participation");
      double projAvg = FindAvg("project");
    }

    public static void WeightTotal ()
    {
      int exam_weight, hw_weight, proj_weight, part_weight, lab_weight; 
      do {	
        Console.WriteLine ("Please enter the weight for each section ");
			
        exam_weight = UIF.PromptInt ("Enter your exam weight: ");
        hw_weight = UIF.PromptInt ("Enter your homework weight: ");
        proj_weight = UIF.PromptInt ("Enter your project weight: ");
        part_weight = UIF.PromptInt ("Enter your paricipation weight: ");
        lab_weight = UIF.PromptInt ("Enter your lab weight: ");

        if (exam_weight + hw_weight + proj_weight + part_weight + lab_weight != 100) {
          Console.WriteLine (" Total weights did not equal 100! Try again. ");
        }                     
      } while (exam_weight + hw_weight + proj_weight + part_weight + lab_weight != 100);
    }

    public static double FindAvg (string name)
    {
      double totalAvg = 0;
      double Num = UIF.PromptDouble ("Enter the number of " + name + "s: ");

      for (int i = 1; i <= Num; i++) {
        double grade = UIF.PromptDouble ("Enter the grade for "+name+" #" + i + ":");
      }

      totalAvg = totalAvg + grade;
      double Avg = totalAvg / Num;
      Console.WriteLine ("Calculated average " + name + " grade: " + Avg);
			
    }			
  }
}


[edit]Indexation changed for better readability - Nelek[/edit]
Posted
Updated 14-Mar-13 23:34pm
v3
Comments
frostcox 13-Mar-13 18:00pm    
Hey you have a couple of options here. For the overall average in main() under where you get the proAvg declare a double dOverAllAvg =(exAvg + hwAvg + labAvg + parAvg + proAvg) / 5, then just display the message to the user using another console.writeline("message here" + dOverAllAvg.toString()). Hope this helps

1 solution

By looks of things your problem is that the values you're receiving from the user disappear out of scope. If you're unfamiliar with that that phrase means, do search for it and read up as it's an important topic.

Essentially all these:
int exam_weight, hw_weight, proj_weight, part_weight, lab_weight; 

Disappear in a puff of smoke as soon as WeightTotal() completes.

What you need is to make those values Instance Variables, which means they will be available to both functions. Simply move the line of code written above in between:
C#
class IntroCS
{

and
C#
public static void Main (string[] args)


You can now reference those values in the second function.
 
Share this answer
 
Comments
Magda6347 13-Mar-13 17:59pm    
Ok Thank you so much. After doing that I receive and error "an object reference is required to access non-static member." sorry I'm not good at this
Nelek 15-Mar-13 5:29am    
Adam answered to your comment. See below
Adam David Hill 13-Mar-13 18:24pm    
Ah, sorry. For now you can just write "static" before the word "int". It's not ideal, but will achieve the desired result in a quick & dirty way. A better solution would involve making a new class to hold those values in, and then you'd pass an instance of it into the second function. I'm guessing those might also be new concepts (read up on classes, instantiation and static) in which case just stick "static" there for now. I'm not saying that's your final solution - there'll be some work to do in your 2nd function to decide which of those values to use according to the name you've passed in, but I'll leave that to you. (Hint: "switch" or "if")

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900