Click here to Skip to main content
15,884,626 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
Your next assignment is to make another modification to your average calculating program. You must now, using an array of ten grades, ask the user to input these grades, calculate the average, display the average, and then output the letter equivalent using the same number range from the last assignment.

Name the Console Application ArrayGrades.

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

namespace Average {
    class Program {
        static void Main(string[] args) {
            double grade1,grade2,grade3,grade4,grade5,grade6,grade7,grade8,grade9,grade10,avg;
            double[] grade = new double[10];

            Console.WriteLine("Please enter a grade: ");
            grade[1] = double.Parse(Console.ReadLine());
            Console.WriteLine("Please enter a grade: ");
            grade[2] = double.Parse(Console.ReadLine());
            Console.WriteLine("Please enter a grade: ");
            grade[3] = double.Parse(Console.ReadLine());
            Console.WriteLine("Please enter a grade: ");
            grade[4] = double.Parse(Console.ReadLine());
            Console.WriteLine("Please enter a grade: ");
            grade[5] = double.Parse(Console.ReadLine());
            Console.WriteLine("Please enter a grade: ");
            grade[6] = double.Parse(Console.ReadLine());
            Console.WriteLine("Please enter a grade: ");
            grade[7] = double.Parse(Console.ReadLine());
            Console.WriteLine("Please enter a grade: ");
            grade[8] = double.Parse(Console.ReadLine());
            Console.WriteLine("Please enter a grade: ");
            grade[9] = double.Parse(Console.ReadLine());
            Console.WriteLine("Please enter a grade: ");
            grade[10] = double.Parse(Console.ReadLine());
            avg = grade[1] + grade[2] + grade[3] + grade[4] + grade[5] + grade[6] + grade[7] + grade[8] + grade[9] + grade[10] / 10;
            Console.WriteLine("The Average is: " + avg );
            Console.WriteLine(avg);
            Console.ReadLine();
        }
    }
}
Posted
Updated 26-Oct-11 2:28am
v3
Comments
Sergey Alexandrovich Kryukov 25-Oct-11 20:15pm    
What an abuse! You did not even take a labor of changing the sentence, so it sounds as the assignment you got: "your next assignment", "you must" (!).
This is so rude and impertinently!
People, please don't go in for such a shame, don't help this member.
--SA
RaisKazi 26-Oct-11 10:37am    
Totally agree with you SA. OP should have basic decency while posting Question.
OP wrote - "Your next assignment". This sounds like a competition organized by OP.
Nagy Vilmos 26-Oct-11 8:26am    
Gimmecode, homework.
Added code in.
RaisKazi 26-Oct-11 10:39am    
My vote-1(That's the least available). At least show some basic decency while posting Question.

As was suggested by Paul Conrad and lukeer, you need to implement a loop structure in your code. When you want to loop through something, you need to first decide how to define the loop. In this case, that would be the index of your grade array, so your loop initialization would be int gradeIndex = 0;. Next you require a condition for the loop to run within, that would be gradeIndex < 10;, and lastly you need to let the loop know how to alter the initialized variable on each loop iteration. gradeIndex ++.

You have an issue with your array in that you start with index 1 as your first item. In C#, arrays begin with index 0, not index 1 so your array indices should go from 0 to 9, not 1 to 10.

Within the body of your loop you need simply to put in the repeating portion of the code that is performed on each of the grade items using the gradeIndex variable in place of the hardcoded indices you have in your sample.
 
Share this answer
 
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Average
{
class Program
{
static void Main(string[] args)
{

double grade1,grade2,grade3,grade4,grade5,grade6,grade7,grade8,grade9,grade10,avg;

double[] grade = new double[10];

Console.WriteLine("Please enter a grade: ");
grade[1] = double.Parse(Console.ReadLine());
Console.WriteLine("Please enter a grade: ");
grade[2] = double.Parse(Console.ReadLine());
Console.WriteLine("Please enter a grade: ");
grade[3] = double.Parse(Console.ReadLine());
Console.WriteLine("Please enter a grade: ");
grade[4] = double.Parse(Console.ReadLine());
Console.WriteLine("Please enter a grade: ");
grade[5] = double.Parse(Console.ReadLine());
Console.WriteLine("Please enter a grade: ");
grade[6] = double.Parse(Console.ReadLine());
Console.WriteLine("Please enter a grade: ");
grade[7] = double.Parse(Console.ReadLine());
Console.WriteLine("Please enter a grade: ");
grade[8] = double.Parse(Console.ReadLine());
Console.WriteLine("Please enter a grade: ");
grade[9] = double.Parse(Console.ReadLine());
Console.WriteLine("Please enter a grade: ");
grade[10] = double.Parse(Console.ReadLine());
avg = grade[1] + grade[2] + grade[3] + grade[4] + grade[5] + grade[6] + grade[7] + grade[8] + grade[9] + grade[10] / 10;
Console.WriteLine("The Average is: " + avg );
Console.WriteLine(avg);
Console.ReadLine();



is what I got but I was absent for the lesson and its due 2morro
 
Share this answer
 
Comments
Paul Conrad 26-Oct-11 2:31am    
Your solution has a few problems to it. First suggestion is to do a for loop for all of your prompts asking for grade to be entered. Just having the console app say "Please enter a grade: " over and over and over is just terrible. Also, with the order of arithmetic operations, your average will come out wrong. You do not need grade1,grade2,grade3,grade4,grade5,grade6,grade7,grade8,grade9,grade10, as that defeats the whole idea of arrays. Suppose you do not know how many grades the user may have to enter?
lukeer 26-Oct-11 5:12am    
For the social aspect: What you posted here is not a solution, even though it is titled "Solution 2". Instead it belongs to the question.
Putting this code in the question in the first place would have caused a much more helpful reaction from forum posters.
Don't be afraid. You can still use the Improve question link to fix that.

For the problem:
You're right not using grade1, grade2 and so on. So you don't need to declare them.
As Paul Conrad said, the input and the calculation should be done in a loop. Are you familiar with the for(init;condition;repeat) construct?
Member 8349150 26-Oct-11 6:07am    
No sorry I am not familiar I was absent for for loops and arrays
Richard MacCutchan 26-Oct-11 7:38am    
There are plenty of references in your notes, programming books, on the internet and elsewhere if you try looking.
lukeer 26-Oct-11 8:05am    
Have a look at MSDN. It expains how a for-loop is used.
In your case, you would of course initiate your incrementing variable i to zero and loop while i < 10 (Arrays are zero-based. The first element's index is 0. All indexes, including the last one are less than the array's item count).

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