?
Solved

Creating arrays and calculations

Posted on 2008-10-11
10
Medium Priority
?
211 Views
Last Modified: 2013-12-17
I need to create a program in C# that will accepts 10 scores with values of 1-10 and put them in an array, then create an other array representing what percentage of 100 the scores represent. I'm not really sure where to begin. Can anyone offer any suggestions?
~TIA
0
Comment
Question by:98fatboyrider
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22695189
you can create a two dimensional array:

int score[][] = new int[10][2];

so, for score write to score[i][0] and for percentages write to score[i][1]
0
 

Author Comment

by:98fatboyrider
ID: 22695248
That gives me a Error Bad array declarator error. Any other suggestions?
    class Program
    {
        static void Main(string[] args)
        {
           int score[][] = new int[10][2];
            int InValue;
 
            Console.WriteLine("Enter 10 scores: ");
            Score = Console.ReadLine();
        }

Open in new window

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22695285
sorry, mixing C# with C++ (too much work!!), correct way in C# is:

            int[,] score = new int[10,2];
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:98fatboyrider
ID: 22695333
I think I have the array filling with the scores, but now how do I do the calculation representing what percentage of 100 the scores are in the second array?
    class Program
    {
 
        static void Main(string[] args)
        {
            int[,] score = new int[10, 2];
            string InValue;
 
            for(int i = 0; i <= 10; i++)
            {
            Console.WriteLine("Enter scores: ");
            InValue = Console.ReadLine();
            score[i, 0] = Convert.ToInt32(InValue);
 
            }
        }
    }
}

Open in new window

0
 
LVL 6

Expert Comment

by:alex_paven
ID: 22696551
Something like this?

for (int i = 0; i < scores.GetUpperBound(0); i++) {
  scores[i][1] = Math.Round(scores[i][0] / maxScore, 2) * 100;
} //where maxScore is the maximum value of the score.

Open in new window

0
 
LVL 6

Expert Comment

by:alex_paven
ID: 22696556
Sorry, the correct line should be

scores[i,1] = Convert.ToInt32(Math.Round(scores[i,0] / maxScore, 2) * 100);

Open in new window

0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 1500 total points
ID: 22696589
each time you input a new number you should accumulate a total, so at the end of input you will be able to evaluate each score as percentage:

        static void Main(string[] args)
        {
            int[,] score = new int[10, 2];
            string InValue;
            int total = 0;   // <-------------- accumulator
            int value;   // <------------ instant value
 
            for(int i = 0; i <= 10; i++)
            {
               Console.WriteLine("Enter scores: ");
               InValue = Console.ReadLine();
               value = Convert.ToInt32(InValue);
               score[i, 0] = value;
               total += value;
            }
        }

well after this first for() loop, you will need a second for() loop to determine the percentage as:

      percentage = value * 100 / total;
0
 

Author Comment

by:98fatboyrider
ID: 22698257
Thanks for the help, but it still seem to be missing something. This is all fairly new to me, so any suggestions would be greatly appreciated.
        static void Main(string[] args)
        {
            int[,] score = new int[10, 2];
            string InValue;
            int total = 0;
            int value;
            int percentage;
            int maxScore;
 
            for(int i = 0; i <= 10; i++)
            {
            Console.WriteLine("Enter scores: ");
            InValue = Console.ReadLine();
            value = Convert.ToInt32(InValue);
            score[i, 0] = value;
            total += value;
 
            for (int j = 0; j < score.GetUpperBound(0); j++)
            {
                score[j, 1] = Convert.ToInt32(Math.Round(score[j, 0] / maxScore, 2) * 100);
                percentage = value * 100 / total;
                Console.WriteLine(score[i, j]);
            }
            }
        }
    }
}

Open in new window

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22699370
appears to be you need a litte variation:

 static void Main(string[] args)
        {
            int[,] score = new int[10, 2];
            string InValue;
            int total = 0;
            int value;
            int percentage;
            int maxScore;
 
            for(int i = 0; i <= 10; i++)
            {
                Console.WriteLine("Enter scores: ");
                InValue = Console.ReadLine();
                value = Convert.ToInt32(InValue);
                score[i, 0] = value;
                total += value;
            }    // close loop here

            for (int j = 0; j < score.GetUpperBound(0); j++)
            {
                score[j, 1] = Convert.ToInt32(Math.Round(score[j, 0] / maxScore, 2) * 100);
                percentage = value * 100 / total;
                Console.WriteLine(score[i, j]);
            }
        }
    }
}
0
 

Author Comment

by:98fatboyrider
ID: 22857379
static void Main(string[] args)
        {
            int[] score = new int[10];
            string InValue;
            int total = 0;
            int value;
            Double Ave;

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Enter scores: ");
                InValue = Console.ReadLine();
                value = Convert.ToInt32(InValue);
                //score[i, 0] = value;
                score[i] = value;
               // total += value;
            }

            Array.Sort(score);
            for (int i = 1; i < 9; i++)
            {
                total += score[i];
            }
            Ave = total / 8;
            Console.WriteLine("\nAverage = "+ Ave);
            Console.Read();
        }
    }
}
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question