Passing a value to a class keeps returning 0
up vote
-1
down vote
favorite
this might be a simple/dumb question but I'm trying to figure out why my code keeps returning 0. I don't think my syntax for passing the value is correct but I cant figure out the proper way of doing it.
class ICESMARK
static int ICECount = 0;
public double average = 0;
public double ICES = new double[8];
public ICESMARK(double Mark)
Mark = ICES[ICECount];
if (ICECount == (ICES.Length - 1))
for (int x = 0; x < ICES.Length; x++)
average += ICES[x];
average /= ICES.Length;
ICECount++;
class Program
static void Main(string args)
ICESMARK ICE = new ICESMARK[8];
//LABSMARK LAB = new LABSMARK[6];
double userInput;
for (int counter = 0; counter < ICE.Length ; counter++)
Console.Write("Please enter your mark for ICE0: ", counter + 1 );
bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = new ICESMARK(userInput);
Console.WriteLine(ICE[1].average);
Console.ReadLine();
ICE[1].average -
Displays 0
Also if anyone has a more efficient way of doing this, feel free to let me know. Except for the average, is gotta be a calculation, cant use the built in method.
c#
|
show 5 more comments
up vote
-1
down vote
favorite
this might be a simple/dumb question but I'm trying to figure out why my code keeps returning 0. I don't think my syntax for passing the value is correct but I cant figure out the proper way of doing it.
class ICESMARK
static int ICECount = 0;
public double average = 0;
public double ICES = new double[8];
public ICESMARK(double Mark)
Mark = ICES[ICECount];
if (ICECount == (ICES.Length - 1))
for (int x = 0; x < ICES.Length; x++)
average += ICES[x];
average /= ICES.Length;
ICECount++;
class Program
static void Main(string args)
ICESMARK ICE = new ICESMARK[8];
//LABSMARK LAB = new LABSMARK[6];
double userInput;
for (int counter = 0; counter < ICE.Length ; counter++)
Console.Write("Please enter your mark for ICE0: ", counter + 1 );
bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = new ICESMARK(userInput);
Console.WriteLine(ICE[1].average);
Console.ReadLine();
ICE[1].average -
Displays 0
Also if anyone has a more efficient way of doing this, feel free to let me know. Except for the average, is gotta be a calculation, cant use the built in method.
c#
where are you using marks parameter which you passing to the constructor? Did not get your code sorry.. What you want to achieve?
– Prasad Telkikar
Nov 9 at 6:16
5
There are a lot of problems here. You should be defining a function that takes values, has a return type, and returns a value. Instead you're directly modifying members of the class you've defined as public. The point of a class is to encapsulate data, and only expose what is needed.
– Brian White
Nov 9 at 6:18
like you get the list of ints from the user, then pass them into a getAvg function. I'm not sure why your constructor calculates an average. Just change that to a function, and pass in List<int> or int
– Brian White
Nov 9 at 6:21
Okay sorry if is messy, I'm very new, specially to classes/methods. ICE = quiz, I want the user to input the mark he got on each quiz, and calculate his average.
– Brian Naranjo
Nov 9 at 6:21
2
alsoMark = ICES[ICECount];
Is this some kind of 2020 technology or why are setting Mark to 0 even before the ICES are set... your Mark parameter becomes useless. Your code is waay to messy, sorry. That's hard to help
– sLw
Nov 9 at 6:50
|
show 5 more comments
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
this might be a simple/dumb question but I'm trying to figure out why my code keeps returning 0. I don't think my syntax for passing the value is correct but I cant figure out the proper way of doing it.
class ICESMARK
static int ICECount = 0;
public double average = 0;
public double ICES = new double[8];
public ICESMARK(double Mark)
Mark = ICES[ICECount];
if (ICECount == (ICES.Length - 1))
for (int x = 0; x < ICES.Length; x++)
average += ICES[x];
average /= ICES.Length;
ICECount++;
class Program
static void Main(string args)
ICESMARK ICE = new ICESMARK[8];
//LABSMARK LAB = new LABSMARK[6];
double userInput;
for (int counter = 0; counter < ICE.Length ; counter++)
Console.Write("Please enter your mark for ICE0: ", counter + 1 );
bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = new ICESMARK(userInput);
Console.WriteLine(ICE[1].average);
Console.ReadLine();
ICE[1].average -
Displays 0
Also if anyone has a more efficient way of doing this, feel free to let me know. Except for the average, is gotta be a calculation, cant use the built in method.
c#
this might be a simple/dumb question but I'm trying to figure out why my code keeps returning 0. I don't think my syntax for passing the value is correct but I cant figure out the proper way of doing it.
class ICESMARK
static int ICECount = 0;
public double average = 0;
public double ICES = new double[8];
public ICESMARK(double Mark)
Mark = ICES[ICECount];
if (ICECount == (ICES.Length - 1))
for (int x = 0; x < ICES.Length; x++)
average += ICES[x];
average /= ICES.Length;
ICECount++;
class Program
static void Main(string args)
ICESMARK ICE = new ICESMARK[8];
//LABSMARK LAB = new LABSMARK[6];
double userInput;
for (int counter = 0; counter < ICE.Length ; counter++)
Console.Write("Please enter your mark for ICE0: ", counter + 1 );
bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = new ICESMARK(userInput);
Console.WriteLine(ICE[1].average);
Console.ReadLine();
ICE[1].average -
Displays 0
Also if anyone has a more efficient way of doing this, feel free to let me know. Except for the average, is gotta be a calculation, cant use the built in method.
c#
c#
edited Nov 9 at 6:42
Mrinal Kamboj
8,21821946
8,21821946
asked Nov 9 at 6:12
Brian Naranjo
6318
6318
where are you using marks parameter which you passing to the constructor? Did not get your code sorry.. What you want to achieve?
– Prasad Telkikar
Nov 9 at 6:16
5
There are a lot of problems here. You should be defining a function that takes values, has a return type, and returns a value. Instead you're directly modifying members of the class you've defined as public. The point of a class is to encapsulate data, and only expose what is needed.
– Brian White
Nov 9 at 6:18
like you get the list of ints from the user, then pass them into a getAvg function. I'm not sure why your constructor calculates an average. Just change that to a function, and pass in List<int> or int
– Brian White
Nov 9 at 6:21
Okay sorry if is messy, I'm very new, specially to classes/methods. ICE = quiz, I want the user to input the mark he got on each quiz, and calculate his average.
– Brian Naranjo
Nov 9 at 6:21
2
alsoMark = ICES[ICECount];
Is this some kind of 2020 technology or why are setting Mark to 0 even before the ICES are set... your Mark parameter becomes useless. Your code is waay to messy, sorry. That's hard to help
– sLw
Nov 9 at 6:50
|
show 5 more comments
where are you using marks parameter which you passing to the constructor? Did not get your code sorry.. What you want to achieve?
– Prasad Telkikar
Nov 9 at 6:16
5
There are a lot of problems here. You should be defining a function that takes values, has a return type, and returns a value. Instead you're directly modifying members of the class you've defined as public. The point of a class is to encapsulate data, and only expose what is needed.
– Brian White
Nov 9 at 6:18
like you get the list of ints from the user, then pass them into a getAvg function. I'm not sure why your constructor calculates an average. Just change that to a function, and pass in List<int> or int
– Brian White
Nov 9 at 6:21
Okay sorry if is messy, I'm very new, specially to classes/methods. ICE = quiz, I want the user to input the mark he got on each quiz, and calculate his average.
– Brian Naranjo
Nov 9 at 6:21
2
alsoMark = ICES[ICECount];
Is this some kind of 2020 technology or why are setting Mark to 0 even before the ICES are set... your Mark parameter becomes useless. Your code is waay to messy, sorry. That's hard to help
– sLw
Nov 9 at 6:50
where are you using marks parameter which you passing to the constructor? Did not get your code sorry.. What you want to achieve?
– Prasad Telkikar
Nov 9 at 6:16
where are you using marks parameter which you passing to the constructor? Did not get your code sorry.. What you want to achieve?
– Prasad Telkikar
Nov 9 at 6:16
5
5
There are a lot of problems here. You should be defining a function that takes values, has a return type, and returns a value. Instead you're directly modifying members of the class you've defined as public. The point of a class is to encapsulate data, and only expose what is needed.
– Brian White
Nov 9 at 6:18
There are a lot of problems here. You should be defining a function that takes values, has a return type, and returns a value. Instead you're directly modifying members of the class you've defined as public. The point of a class is to encapsulate data, and only expose what is needed.
– Brian White
Nov 9 at 6:18
like you get the list of ints from the user, then pass them into a getAvg function. I'm not sure why your constructor calculates an average. Just change that to a function, and pass in List<int> or int
– Brian White
Nov 9 at 6:21
like you get the list of ints from the user, then pass them into a getAvg function. I'm not sure why your constructor calculates an average. Just change that to a function, and pass in List<int> or int
– Brian White
Nov 9 at 6:21
Okay sorry if is messy, I'm very new, specially to classes/methods. ICE = quiz, I want the user to input the mark he got on each quiz, and calculate his average.
– Brian Naranjo
Nov 9 at 6:21
Okay sorry if is messy, I'm very new, specially to classes/methods. ICE = quiz, I want the user to input the mark he got on each quiz, and calculate his average.
– Brian Naranjo
Nov 9 at 6:21
2
2
also
Mark = ICES[ICECount];
Is this some kind of 2020 technology or why are setting Mark to 0 even before the ICES are set... your Mark parameter becomes useless. Your code is waay to messy, sorry. That's hard to help– sLw
Nov 9 at 6:50
also
Mark = ICES[ICECount];
Is this some kind of 2020 technology or why are setting Mark to 0 even before the ICES are set... your Mark parameter becomes useless. Your code is waay to messy, sorry. That's hard to help– sLw
Nov 9 at 6:50
|
show 5 more comments
2 Answers
2
active
oldest
votes
up vote
2
down vote
Simplest code to get your work done:
void Main()
double ICE = new double[8];
double userInput = 0.0;
for (int counter = 0; counter < ICE.Length; counter++)
Console.WriteLine($"Please enter your mark for ICE counter: ");
bool isNumerical = false;
while(!isNumerical)
isNumerical = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = userInput;
Console.WriteLine("Average : " + ICE.Average());
Console.ReadLine();
How it works:
- Removed all the frills, you don't need an extra class just for this purpose
- Made it mandatory for code to enter valid double value to fill all the slots
- Finally used
Linq Average
to calculateAverage
value
add a comment |
up vote
0
down vote
accepted
I want to clarify that yes there are easier ways to solve this, but the entire point of the project was to use a class and it specifically says I cant use built in methods such as "array.average".
Sorry that my code was super messy, I was honestly all over the place and very confused. Having that said I finally arrived to this solution. Thanks to everyone who tried to help! really appreciate it, some tips here were very helpful in solving and cleaning up my code.
class ICESMARK
public static int ICECount = 0;
public static double average = 0;
public ICESMARK(double Mark)
average += Mark;
if (ICECount < 8) ICECount++;
if (ICECount == 8) average /= ICECount;
class Program
static void Main(string args)
ICESMARK ICE = new ICESMARK[8];
double userInput;
for (int counter = 0; counter < ICE.Length ; counter++)
Console.Write("Please enter your mark for ICE0: ", counter + 1 );
bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = new ICESMARK(userInput);
Console.WriteLine(ICESMARK.ICECount);
Console.WriteLine(ICESMARK.average);
Console.WriteLine(ICESMARK.ICECount);
Console.ReadLine();
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Simplest code to get your work done:
void Main()
double ICE = new double[8];
double userInput = 0.0;
for (int counter = 0; counter < ICE.Length; counter++)
Console.WriteLine($"Please enter your mark for ICE counter: ");
bool isNumerical = false;
while(!isNumerical)
isNumerical = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = userInput;
Console.WriteLine("Average : " + ICE.Average());
Console.ReadLine();
How it works:
- Removed all the frills, you don't need an extra class just for this purpose
- Made it mandatory for code to enter valid double value to fill all the slots
- Finally used
Linq Average
to calculateAverage
value
add a comment |
up vote
2
down vote
Simplest code to get your work done:
void Main()
double ICE = new double[8];
double userInput = 0.0;
for (int counter = 0; counter < ICE.Length; counter++)
Console.WriteLine($"Please enter your mark for ICE counter: ");
bool isNumerical = false;
while(!isNumerical)
isNumerical = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = userInput;
Console.WriteLine("Average : " + ICE.Average());
Console.ReadLine();
How it works:
- Removed all the frills, you don't need an extra class just for this purpose
- Made it mandatory for code to enter valid double value to fill all the slots
- Finally used
Linq Average
to calculateAverage
value
add a comment |
up vote
2
down vote
up vote
2
down vote
Simplest code to get your work done:
void Main()
double ICE = new double[8];
double userInput = 0.0;
for (int counter = 0; counter < ICE.Length; counter++)
Console.WriteLine($"Please enter your mark for ICE counter: ");
bool isNumerical = false;
while(!isNumerical)
isNumerical = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = userInput;
Console.WriteLine("Average : " + ICE.Average());
Console.ReadLine();
How it works:
- Removed all the frills, you don't need an extra class just for this purpose
- Made it mandatory for code to enter valid double value to fill all the slots
- Finally used
Linq Average
to calculateAverage
value
Simplest code to get your work done:
void Main()
double ICE = new double[8];
double userInput = 0.0;
for (int counter = 0; counter < ICE.Length; counter++)
Console.WriteLine($"Please enter your mark for ICE counter: ");
bool isNumerical = false;
while(!isNumerical)
isNumerical = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = userInput;
Console.WriteLine("Average : " + ICE.Average());
Console.ReadLine();
How it works:
- Removed all the frills, you don't need an extra class just for this purpose
- Made it mandatory for code to enter valid double value to fill all the slots
- Finally used
Linq Average
to calculateAverage
value
edited Nov 9 at 7:57
answered Nov 9 at 7:13
Mrinal Kamboj
8,21821946
8,21821946
add a comment |
add a comment |
up vote
0
down vote
accepted
I want to clarify that yes there are easier ways to solve this, but the entire point of the project was to use a class and it specifically says I cant use built in methods such as "array.average".
Sorry that my code was super messy, I was honestly all over the place and very confused. Having that said I finally arrived to this solution. Thanks to everyone who tried to help! really appreciate it, some tips here were very helpful in solving and cleaning up my code.
class ICESMARK
public static int ICECount = 0;
public static double average = 0;
public ICESMARK(double Mark)
average += Mark;
if (ICECount < 8) ICECount++;
if (ICECount == 8) average /= ICECount;
class Program
static void Main(string args)
ICESMARK ICE = new ICESMARK[8];
double userInput;
for (int counter = 0; counter < ICE.Length ; counter++)
Console.Write("Please enter your mark for ICE0: ", counter + 1 );
bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = new ICESMARK(userInput);
Console.WriteLine(ICESMARK.ICECount);
Console.WriteLine(ICESMARK.average);
Console.WriteLine(ICESMARK.ICECount);
Console.ReadLine();
add a comment |
up vote
0
down vote
accepted
I want to clarify that yes there are easier ways to solve this, but the entire point of the project was to use a class and it specifically says I cant use built in methods such as "array.average".
Sorry that my code was super messy, I was honestly all over the place and very confused. Having that said I finally arrived to this solution. Thanks to everyone who tried to help! really appreciate it, some tips here were very helpful in solving and cleaning up my code.
class ICESMARK
public static int ICECount = 0;
public static double average = 0;
public ICESMARK(double Mark)
average += Mark;
if (ICECount < 8) ICECount++;
if (ICECount == 8) average /= ICECount;
class Program
static void Main(string args)
ICESMARK ICE = new ICESMARK[8];
double userInput;
for (int counter = 0; counter < ICE.Length ; counter++)
Console.Write("Please enter your mark for ICE0: ", counter + 1 );
bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = new ICESMARK(userInput);
Console.WriteLine(ICESMARK.ICECount);
Console.WriteLine(ICESMARK.average);
Console.WriteLine(ICESMARK.ICECount);
Console.ReadLine();
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
I want to clarify that yes there are easier ways to solve this, but the entire point of the project was to use a class and it specifically says I cant use built in methods such as "array.average".
Sorry that my code was super messy, I was honestly all over the place and very confused. Having that said I finally arrived to this solution. Thanks to everyone who tried to help! really appreciate it, some tips here were very helpful in solving and cleaning up my code.
class ICESMARK
public static int ICECount = 0;
public static double average = 0;
public ICESMARK(double Mark)
average += Mark;
if (ICECount < 8) ICECount++;
if (ICECount == 8) average /= ICECount;
class Program
static void Main(string args)
ICESMARK ICE = new ICESMARK[8];
double userInput;
for (int counter = 0; counter < ICE.Length ; counter++)
Console.Write("Please enter your mark for ICE0: ", counter + 1 );
bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = new ICESMARK(userInput);
Console.WriteLine(ICESMARK.ICECount);
Console.WriteLine(ICESMARK.average);
Console.WriteLine(ICESMARK.ICECount);
Console.ReadLine();
I want to clarify that yes there are easier ways to solve this, but the entire point of the project was to use a class and it specifically says I cant use built in methods such as "array.average".
Sorry that my code was super messy, I was honestly all over the place and very confused. Having that said I finally arrived to this solution. Thanks to everyone who tried to help! really appreciate it, some tips here were very helpful in solving and cleaning up my code.
class ICESMARK
public static int ICECount = 0;
public static double average = 0;
public ICESMARK(double Mark)
average += Mark;
if (ICECount < 8) ICECount++;
if (ICECount == 8) average /= ICECount;
class Program
static void Main(string args)
ICESMARK ICE = new ICESMARK[8];
double userInput;
for (int counter = 0; counter < ICE.Length ; counter++)
Console.Write("Please enter your mark for ICE0: ", counter + 1 );
bool ifInt = double.TryParse(Console.ReadLine(), out userInput);
ICE[counter] = new ICESMARK(userInput);
Console.WriteLine(ICESMARK.ICECount);
Console.WriteLine(ICESMARK.average);
Console.WriteLine(ICESMARK.ICECount);
Console.ReadLine();
edited Nov 10 at 3:53
answered Nov 9 at 8:37
Brian Naranjo
6318
6318
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53220691%2fpassing-a-value-to-a-class-keeps-returning-0%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
where are you using marks parameter which you passing to the constructor? Did not get your code sorry.. What you want to achieve?
– Prasad Telkikar
Nov 9 at 6:16
5
There are a lot of problems here. You should be defining a function that takes values, has a return type, and returns a value. Instead you're directly modifying members of the class you've defined as public. The point of a class is to encapsulate data, and only expose what is needed.
– Brian White
Nov 9 at 6:18
like you get the list of ints from the user, then pass them into a getAvg function. I'm not sure why your constructor calculates an average. Just change that to a function, and pass in List<int> or int
– Brian White
Nov 9 at 6:21
Okay sorry if is messy, I'm very new, specially to classes/methods. ICE = quiz, I want the user to input the mark he got on each quiz, and calculate his average.
– Brian Naranjo
Nov 9 at 6:21
2
also
Mark = ICES[ICECount];
Is this some kind of 2020 technology or why are setting Mark to 0 even before the ICES are set... your Mark parameter becomes useless. Your code is waay to messy, sorry. That's hard to help– sLw
Nov 9 at 6:50