C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

pointers and calculating average Can't get average Rate Topic: -----

#1 SleepingOlive  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 16-September 08


Dream Kudos: 0

Share |

pointers and calculating average

Post icon  Posted 26 October 2008 - 05:52 PM

I am writing a program that will take input from the user the amount of test scores that they want to enter. Then the test scores will be printed out in ascending order followed by the average. I am haveing trouble getting the average to work. When I compile it, the error says that Tscore does not exist in the current context.

using System;
	class testScore
	{
		static unsafe void Main(string[] args)
		{
			//ask how many scores
			Console.WriteLine("How many scores are you going to enter?");
			string userInput = Console.ReadLine();
			uint size = uint.Parse(userInput);
			
			// Tscore will total up the scores as they are entered 
			int Tscore = 0;
			
			long* pArray = stackalloc long [(int)size];
			// ask for scores
			int temp = 1;
			while (temp <= size)
			{
				Console.WriteLine("What The Score of Test "+temp);
				string testScore = Console.ReadLine();
				int score = int.Parse(testScore);
				
				Tscroe = Tscore + score;
				temp++;
			}
			for (int i=0; i < size; i++)
			{
				pArray[i] = size; 
			}
			for (int i=0; i < size; i++)
			{
				Console.WriteLine("Test Score {0} = {1}", i, *(pArray+i));
			}
			
			// get average and print to prompt
			int average;
			average = (Tscore/size);
			Console.WriteLine("The Average is " +average);
		}
	}


Was This Post Helpful? 0
  • +
  • -


#2 deedee66  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 21-September 08


Dream Kudos: 0

Re: pointers and calculating average

Posted 26 October 2008 - 06:08 PM

View PostSleepingOlive, on 26 Oct, 2008 - 06:52 PM, said:

I am writing a program that will take input from the user the amount of test scores that they want to enter. Then the test scores will be printed out in ascending order followed by the average. I am haveing trouble getting the average to work. When I compile it, the error says that Tscore does not exist in the current context.

using System;
	class testScore
	{
		static unsafe void Main(string[] args)
		{
			//ask how many scores
			Console.WriteLine("How many scores are you going to enter?");
			string userInput = Console.ReadLine();
			uint size = uint.Parse(userInput);
			
			// Tscore will total up the scores as they are entered 
			int Tscore = 0;
			
			long* pArray = stackalloc long [(int)size];
			// ask for scores
			int temp = 1;
			while (temp <= size)
			{
				Console.WriteLine("What The Score of Test "+temp);
				string testScore = Console.ReadLine();
				int score = int.Parse(testScore);
				
				Tscroe = Tscore + score;
				temp++;
			}
			for (int i=0; i < size; i++)
			{
				pArray[i] = size; 
			}
			for (int i=0; i < size; i++)
			{
				Console.WriteLine("Test Score {0} = {1}", i, *(pArray+i));
			}
			
			// get average and print to prompt
			int average;
			average = (Tscore/size);
			Console.WriteLine("The Average is " +average);
		}
	}


one of your Tscores is spelled wrong
Was This Post Helpful? 0
  • +
  • -

#3 SleepingOlive  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 16-September 08


Dream Kudos: 0

Re: pointers and calculating average

Posted 26 October 2008 - 06:11 PM

nevermind I just miss spelled the variable .......now the problem I am having is printing the correct score to the screen.

when i compile and run it when entering 3 scores and all scores are 5. The average is correct but it is not printing
the proper individual test score.
Test score 0 = 3
Test score 1 = 3
Test score 2 = 3
It is just printing out the amount of test scores that I entered???
Was This Post Helpful? 0
  • +
  • -

#4 SleepingOlive  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 16-September 08


Dream Kudos: 0

Re: pointers and calculating average

Posted 26 October 2008 - 08:17 PM

any idea's yet???
Was This Post Helpful? 0
  • +
  • -

#5 jacobjordan  Icon User is offline

  • class Me : Perfection
  • Icon

Reputation: 83
  • View blog
  • Posts: 1,495
  • Joined: 11-June 08


Dream Kudos: 1725

Re: pointers and calculating average

Posted 26 October 2008 - 08:36 PM

I must say, it seems using pointers for something this simple is way overkill. I also have no idea what the point behind half of that code is. Anyway, i did away with all the unsafe code, and this seems to work fine:
        static void Main(string[] args)
        {
            //ask how many scores
            Console.WriteLine("How many scores are you going to enter?");
            string userInput = Console.ReadLine();
            uint size = uint.Parse(userInput);
            
            // Tscore will total up the scores as they are entered
            int Tscore = 0;

            System.Collections.ObjectModel.Collection<int> Scores = new System.Collections.ObjectModel.Collection<int>();
            // ask for scores
            int temp = 1;
            while (temp <= size)
            {
                Console.WriteLine("What The Score of Test "+temp);
                string testScore = Console.ReadLine();
                int score = int.Parse(testScore);
                Scores.Add(score);
                Tscore = Tscore + score;
                temp++;
            }
            for (int i=0; i < size; i++)
            {
                Console.WriteLine("Test Score {0} = {1}", i, Scores[i]);
            }
            
            // get average and print to prompt
            int average;
            average = (int)(Tscore/size);
            Console.WriteLine("The Average is " +average);
            Console.ReadLine();
        }


I created a simple collection to hold the test scores. It was that simple.
Edit: i also added in a Console.ReadLine() at the end so your console will actually stay open long enough for you to see the result. ;)

This post has been edited by jacobjordan: 26 October 2008 - 08:37 PM

Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • Icon

Reputation: 566
  • View blog
  • Posts: 2,751
  • Joined: 06-January 08


Dream Kudos: 50

Re: pointers and calculating average

Posted 26 October 2008 - 09:01 PM

You don't need to use unsafe code for this problem. Pointer dereferencing is so 'C'. :)
The ArrayList Class in .NET will handle lists of unknown, at runtime, sizes.
See http://msdn.microsof....arraylist.aspx.
I've taken the liberty of changing you code. :)
using System;
using System.Collections;

namespace TestScores1
{
    class testScore
    {
        static void Main(string[] args)
        {

//ask how many scores
            Console.WriteLine("How many scores are you going to enter?");
            string userInput = Console.ReadLine();
            int size = int.Parse(userInput);

// Tscore will total up the scores as they are entered 
            int Tscore = 0;
			
/*
        long* pArray = stackalloc long [(int)size];
*/
            ArrayList pArray = new ArrayList();

// ask for scores
            for (int i = 0; i < size; i++)
            {
                Console.WriteLine("What is the Score of Test {0}", (i + 1));
                string testScore = Console.ReadLine();
                int score = int.Parse(testScore);

                pArray.Add(score);

                Tscore = Tscore + score;
            }

            for (int i = 0; i < size; i++)
                Console.WriteLine("Test Score {0} = {1}", (i + 1), pArray[i]);

// get average and print to prompt
            int average;
            average = (Tscore/size);
            Console.WriteLine("The Average is {0}", average);
            Console.ReadKey();
        }
    }
}


Quote

when i compile and run it when entering 3 scores and all scores are 5. The average is correct but it is not printing
the proper individual test score.
Test score 0 = 3
Test score 1 = 3
Test score 2 = 3
It is just printing out the amount of test scores that I entered???

Your example shows 3s not 5s as the scores. If you enter three 3s and take the average
of course it will also be 3!
B)

This post has been edited by n8wxs: 26 October 2008 - 09:01 PM

Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users