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);
}
}
Page 1 of 1
pointers and calculating average Can't get average
#1
pointers and calculating average
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.
#2
Re: pointers and calculating average
Posted 26 October 2008 - 06:08 PM
SleepingOlive, 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
#3
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???
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???
#5
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:
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.
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
#6
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.
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!
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???
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!
This post has been edited by n8wxs: 26 October 2008 - 09:01 PM
Page 1 of 1

Ask A New Question
Reply





MultiQuote





|