So I am doing a Homework assignment for c#. The assignment requires the use of 2 arrays, one to hold names and the other to hold scores. We are then to calculate the average score, display the below average scores, and display the scores according to name. I am having a significant amount of difficulty passing the arrays by reference/value in order to use the data in them and maintain my code using separate modules. The arrays have to hold up to 100 individual pieces of data.
class Program
{
static void Main(string[] args)
{
string[] playerNames = new string[100];
int[] playerScores = new int [100];
int numPlayers = 0;
double averageScore;
DisplayData(playerNames);
}
static void InputData(string[] playerNames, int[] playerScores, ref int numPlayers)
{
string playerName;
do
{
int i = 0;
Console.WriteLine("Enter the name of the player...Enter \"Q to exit...");
playerName = Console.ReadLine();
if (playerName == "Q" || playerName == "q")
Console.WriteLine("No further user input needed...\n");
else
playerNames[i] = playerName;
Console.WriteLine("Enter the score of the player...");
int playerScore = Convert.ToInt32(Console.ReadLine());
playerScores[i] = playerScore;
numPlayers += i;
i++;
}
while (playerName != "Q");
}
static void CalculateAverageScore(int[] playerScores, ref int averageScore)
{
InputData(playerScores);
InputData(ref numPlayers);
int averageScores = 0;
averageScores = playerScores / numPlayers;
}
The biggest question I have is that I cannot seem to pass the array data. I am plagued by "No overload method for "Input Data" or whatever method I am using. So I'm curious as to how I can pass the array data to another method.
Since this is homework, I won't give you the full answer, but here is a way forward.
Instead of using the ref keyword, just pass the array into the method. Have the method return an int instead of void. Calculate the average within the method and have the final line of the method return the int that you've calculated
static int CalculateAverageScore(int[] playerScores)
{
// calculate average
return average; // the int you've calculated.
}
Call the method from the main, when you need to acquire the average.
Also, are you missing some braces { } for the 6 lines that come after your else statement?
Related
I'm following along in a book and trying the 'challenges'. Here I'm running into a problem with properly returning and passing an array between methods. Something goes wrong with returning the array, especially from the second method, and then passing it to third to print.
I understand the stack, heap, values and references on the conceptual level, but something clearly goes
wrong.
Each method appears to work otherwise.
using System;
namespace PracticeArray
{
class Program
{
static int[] GenerateNumbers()
{
Console.WriteLine("Enter a number to generate array from:");
string input = Console.ReadLine();
int inputNumber = Convert.ToInt32(input);
int[] generatedArray = new int[inputNumber];
for (int i = 0; i < generatedArray.Length; i++)
{
generatedArray[i] = i;
}
return generatedArray;
}
static int[] Reverse(int[] arrayToReverse)
{
int count = arrayToReverse.Length;
int[] arrayToReturn = new int[count];
count--;
for (int i = 0; i < arrayToReturn.Length; i++)
{
arrayToReturn[i] = arrayToReverse[count--];
}
return arrayToReturn;
}
static void PrintNumbers(int[] numbersToPrint)
{
foreach (int singleNumber in numbersToPrint)
{
Console.Write(singleNumber + ", ");
}
}
static void Main(string[] args)
{
int[] numbers = GenerateNumbers();
Reverse(numbers);
PrintNumbers(numbers);
}
}
}
The problem
The array you return is a new array:
Take a look in your Reverse function. You create a new array called arrayToReturn and return that array. You also never modify the original input array arrayToReverse.
static int[] Reverse(int[] arrayToReverse) // this input array is never modified
{
int count = arrayToReverse.Length;
// ...
arrayToReturn = new int[count]; // Here is the new array
// ...
return arrayToReturn; // you return the new array
}
Now look at where you called Reverse you passed in some generated numbers, but you ignore the return value of the function so you never find out what the new array is. Remember we already established above that you don't modify the elements of the input array. So numbers will remain unchanged.
static void Main(string[] args)
{
int[] numbers = GenerateNumbers();
Reverse(numbers); // you ignored the return value of `Reverse`
PrintNumbers(numbers); // These are the original unreversed numbers
}
To Fix
Option #1 - New variable to store the result
To fix this, store the array returned from Reverse in a variable and print those numbers.
static void Main(string[] args)
{
int[] numbers = GenerateNumbers();
int[] reversedNumbers = Reverse(numbers);
PrintNumbers(reversedNumbers);
}
Option #2 - Reuse the same variable to store the result
In this option you simply discard the original array by taking the new reversed numbers array returned from Reverse and assigning it to the numbers variable. After that assignment, numbers will refer to the reversed array that was returned from Reverse. The original array is orphaned and forgotten - eventually garbage collected.
static void Main(string[] args)
{
int[] numbers = GenerateNumbers();
// repurpose the `numbers` variable to store the result.
numbers = Reverse(numbers);
PrintNumbers(numbers);
}
I am quite new to programming and I just need help with what I am doing wrong.
This is the code I have so far. Yes, this is for homework, but I am confused on what I have to do next.
In the CreateRandomlyFilledArray method, I have to create an allocated array. This method will take as it's only parameter an integer, The array is then created inside the method, filled with values that have been randomly created by the method. (values can be from 0 to 100).
The array will then be passed (as a parameter) to the PrintArray method, which will take as it's single parameter an array of integers, and will print out everything in the array.
class Returning_An_Array
{
public void RunExercise()
{
ArrayReturnMethods m = new ArrayReturnMethods();
int[] nums1;
nums1 = m.CreateRandomlyFilledArray(10);
m.PrintArray(nums1);
}
}
class ArrayReturnMethods
{
public int[] CreateRandomlyFilledArray( int size )
{
int[] newNums = new int[size];
for (int value = 0; value < newNums.Length; value++)
{
return newNums;
}
return newNums;
}
public void Printarray( int[] value )
{
for(int i = 0; i < value.Length; i++)
{
Console.WriteLine("value is: {0}", value[i]);
}
}
}
Thank you so much!!
Avoid asking homework question here. Especially when a bit of reading would solve your issue. Good luck with your homework. : )
class Program
{
/*
I assume you are trying to
1. Create an array of integers
2. Store random numbers (between 0 and 100) inside that array
3. Print the numbers in the array
You have alot of reading to do as theres alot of fundemental mistakes in both your approach and code.
*/
static void Main(string[] args)
{
// creating an array with random numbers
ArrayMethods m = new ArrayMethods();
int[] nums1;
nums1 = m.CreateRandomlyFilledArray(10);
m.Printarray(nums1);
}
class ArrayMethods
{
/*
- First you have to fill the array with random numbers
In your solution, you have created "CreateRandomlyFilledArray".
1. You created the a new array of integers which is good
2. The way you attempted to fill the new array is incorrect
*/
public int[] CreateRandomlyFilledArray(int size)
{
int[] newNums = new int[size];
Random numGen = new Random(); // This will be used to generate random numbers
for (int elementNum = 0; elementNum < newNums.Length; elementNum++)
{
// here we will put a random number in every position of the array using the random number generator
newNums[elementNum] = numGen.Next(0, 100); // we pass in you minimum and maximum into the next function and it will return a random number between them
}
// here we will return the array with the random numbers
return newNums;
}
/*
- This function prints out each item in an integer array
1. You do not need to a return value as you will not be returning any thing so, Use "void".
*/
public void Printarray(int[] value)
{
for (int i = 0; i < value.Length; i++)
{
Console.WriteLine("value is: {0}", value[i]);
}
}
}
}
Now, I may get negative points because perhaps somewhere in vast internet there is already an answer to this but I tried to look for it and I simply couldnt find it.
The gist of the problem is that HackerRanks wants you to create an array with a size decided by the user, then have the user add its values (integers) and finally have the program sum its values.
There are plenty of ways to do it and I already know how to but my problem is that I just can't understand Hackerrank's code sample in C# it gave me. I commented the parts I don't understand, which is most of it:
static int simpleArraySum(int n, int[] ar) {
// Complete this function
int sum = 0;
foreach( var item in ar){
sum += item;
}
return sum;
}
static void Main(String[] args) {
//I know what this does
int n = Convert.ToInt32(Console.ReadLine());
//I am lost here, just why create a string array and add the split method?
string[] ar_temp = Console.ReadLine().Split(' ');
//I dont understand here neither, what is it converting? What is the parse for?
int[] ar = Array.ConvertAll(ar_temp,Int32.Parse);
//Why send the n when all you need is the array itself?
int result = simpleArraySum(n, ar);
Console.WriteLine(result);
}
I know some people hate HackerRank, and honestly, I do too but it does gives me some nice ways to test my limited skills in coding with c# and testing my logic. So, if there are better sites that helps you test your logic as a CS please share them with me.
Here is the code I made to solve this problem in Visual Studio but for some stupid reason Hackerrank wont accept it unless I make custom inputs:
//This code can be potentially shorter using the code commented further below.
//For practice's sake, it was made longer.
static int simpleArraySum(int[] arr_temp)
{
int total = 0;
foreach (var item in arr_temp)
{
total += item;
}
return total;
}
static void Main(String[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
int[] arr_temp = new int[n];
for (int i = 0; i < n; i++)
{
arr_temp[i] = Convert.ToInt32(Console.ReadLine());
}
int result = simpleArraySum(arr_temp);
//int result = arr_temp.Sum();
Console.WriteLine(result);
Console.ReadLine();
}
You need to convert to string array since if you're on the main method, all it gets are string values from the argument list. To get the sum then you need to convert the string into a usable number / integer.
I agree that it doesn't make sense to send the first argument n in simpleArraySum because n is simply unused.
as for the part int[] ar = Array.ConvertAll(ar_temp,Int32.Parse); it simply tries to take in all the integers into the array. It is also risky because if you accidentally pass in a string then it will throw an error i.e. pass in "3 4 1 f" <- f will throw an exception, unless this is the desired behaviour.
Personally I think the main method should not be interested in getting involved too much with the data, the heavy lifting should be done in the methods. The better version perhaps would be to modify simpleArraySum and refactor that line in like:
static int simpleArraySum(string input)
{
String[] fields = input.Split(null);
List<int> vals = new List<int>();
foreach (string i in fields)
{
var j = 0;
if (Int32.TryParse(i, out j)) vals.Add(j);
}
int sum = 0;
foreach (var item in vals)
{
sum += item;
}
return sum;
}
I introduced the use of generic list because it's more readable if not cleaner, although the use of List might look overkill to some programmers and might not be as light weight as just using an array, hence on the other hand you can easily stick to using arrays except that it needs to be initialized with the length i.e. int[] vals = new int[fields.Length]; Roughly:
static int simpleArraySum(string input)
{
String[] fields = input.Split(null);
int[] vals = new int[fields.Length];
for (int i = 0; i < fields.Length; i++)
{
var j = 0;
if (Int32.TryParse(fields[i], out j)) vals[i] = j;
}
int sum = 0;
foreach (var item in vals)
{
sum += item;
}
return sum;
}
here my code i hope that helps
static int simpleArraySum(int[] ar,int count) {
if (count > 0 && count <= 10000)
{
if (count == ar.Length)
{
if (!ar.Any(item => (item < 0 || item >= 10000)))
{
return ar.Sum();
}
}
}
return 0;
}
and in main
int arCount = Convert.ToInt32(Console.ReadLine());
int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arTemp => Convert.ToInt32(arTemp));
int result = simpleArraySum(arr, arCount);
Console.WriteLine(result);
since Array.ConvertAll() takes a string and convert it to one type array
int or float for example
For users still looking for a 100% C# solution: In above mentioned coding websites do not modify the main function. The aim of the test is to complete the function via the online complier.
using System.Linq;
public static int simpleArraySum(List<int> ar)
{
int sum = ar.Sum();
return sum;
}
Hi I am trying to make a mastermind game where I having the user guess a number sequence between 4-10 instead of colours but for some reason my GetRandomNumberCount and my GenerateRandomNumber give me the error not all code paths return a value.
Any guidance would be appreciated
public static int GetRandomNumberCount()
{
//Create the secret code
Random RandomClass = new Random();
int first = RandomClass.Next(1, 5);
int second = RandomClass.Next(1,5);
int third = RandomClass.Next(1,5);
int forth = RandomClass.Next(1,5);
Console.WriteLine ("You are playing with M#sterB#t");
Console.WriteLine ("Bot Says : You Go First");
Console.WriteLine("Game Settings ");
Console.WriteLine("The Game Begins");
}
That's because they don't return a value. For example GetRandomNumberCount has Intset as its return type, but has no return statement. If you want to return nothing set the return type to void. Using that as an example
public static int[] GetRandomNumberCount()
{
//Create the secret code
Random RandomClass = new Random();
int first = RandomClass.Next(1, 5);
int second = RandomClass.Next(1,5);
int third = RandomClass.Next(1,5);
int forth = RandomClass.Next(1,5);
Console.WriteLine ("You are playing with M#sterB#t");
Console.WriteLine ("Bot Says : You Go First");
Console.WriteLine("Game Settings ");
Console.WriteLine("The Game Begins");
//This is where you would return a value, but in this case it seems you want to return an array of ints
//Notice how I changed the return type of the method to Int[]
int[] numbers = new int[4];
numbers.Add(first);
numbers.Add(second);
numbers.Add(third);
numbers.Add(fourth);
//This is the actual return statement that your methods are missing
return numbers;
}
regardless of whether you actually want to return an int array is moot though, Im only guessing. The real take away is that the int[] in
public static int[] GetRandomNumberCount()
declares a return type meaning you need a a return statement.
You are getting that error cause your method signature says it returns a int but your are not returning anything. What I see is, you meant to have a method with void return type like below since you are just printing the lines
public static void GetRandomNumberCount()
{
//Create the secret code
Random RandomClass = new Random();
int first = RandomClass.Next(1, 5);
int second = RandomClass.Next(1,5);
int third = RandomClass.Next(1,5);
int forth = RandomClass.Next(1,5);
Console.WriteLine ("You are playing with M#sterB#t");
Console.WriteLine ("Bot Says : You Go First");
Console.WriteLine("Game Settings ");
Console.WriteLine("The Game Begins");
}
I can't seem to figure out how to fix my code so that it works. I need the user to be able to input their first name then space then the what they scored. Then I need to split the array into two different arrays and pass them to the four different methods to display to the user what they scored, etc. Can anyone help me figure this problem out?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace proj09LEA
{
class Program
{
static void Main(string[] args)
{
// declare and array of integers
int[] array = new int[10];
Console.WriteLine("\nSaturday Coder's Bowling Team");
Console.WriteLine("Enter in a name and score for each person on the team.");
Console.WriteLine("For example, Mary 143. Just hit Enter when you are done.\n");
// fill an array with user input
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine("Enter in a name and score: ");
string userInput;
string[] parsedInput;
parsedInput = userInput.Split();
string name = parsedInput[0];
int score = int.Parse(parsedInput[1]);
}
Console.WriteLine("------------ Input Complete ------------\n");
Console.WriteLine("Here are the scores for this game:");
DisplayScore(array);
HighScore(array);
LowScore(array);
AverageScore(array);
Console.WriteLine("Press Enter to continue. . .");
Console.ReadLine();
}
static void DisplayScore(int[] array)
{
foreach (int n in array)
{
Console.WriteLine("{0}'s score was {0}.\n", array);
}
}
static void HighScore(int[] array)
{
int max = array.Max();
Console.WriteLine("Congratulations {0}, your score of {0} was the highest.", max);
}
static void LowScore(int[] array)
{
int min = array.Min();
Console.WriteLine("{0}, your score of {0} was the lowest. Better get some practice.", min);
}
static void AverageScore(int[] array)
{
int sum = array.Sum();
int average = sum / array.Length;
Console.WriteLine("The average score for this game was {0:d}.", average);
}
}
}
If you absolutely have to use simple primitive arrays, you would need two distinct arrays of the same size, to hold the names as strings and scores as ints:
class Program
{
const int MaxScores = 10; // .. Use a constant to ensure the sizes remain in sync
static void Main(string[] args)
{ ///
string[] names = new int[MaxScores];
int[] scores = new int[MaxScores];
// ... parse names into names[] and scores into scores[]
DisplayScore(names, scores);
You would then need to pass both arrays to the various methods:
static void DisplayScore(string[] names, int[] scores)
{
for(int i=0; i < MaxScores; i++)
{
Console.WriteLine("{0}'s score was {1}.\n", names[i], scores[i]);
}
}
// etc
However, there are better ways to do this, e.g. by defining a custom class for the tuple of Name, Score:
class PersonScore
{
public string Name {get; set;}
public int Score {get; set;}
}
You can then declare and pass the single array of PersonScore[] around.
PersonScore[] personScores = new PersonScore[MaxScores];
for (... prompting the user for data)
{
... parsing user input
personScores[i] = new PersonScore{Name = name, Score = score};
}
DisplayScore(personScores); // Pass around the single array
static void DisplayScore(IEnumerable personScores)
{
foreach(var personScore in personScores)
{
Console.WriteLine("{0}'s score was {1}.\n", personScore.Name, personScores.Score);
}
}
// etc - other methods
As others have mentioned, other collections are also possible alternatives to an array, most commonly List.
You can do like this. Just use Console.ReadLine() to get user input. This is what you do in your code. There are better ways to do this but following will solve your problem.Also you need to perform validation as well.
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine("Enter in a name and score: ");
string userInput = Console.ReadLine();
string[] parsedInput;
parsedInput = userInput.Split(' ');
string name = parsedInput[0];
int score = int.Parse(parsedInput[1]);
array[i] = score;
}
Why you need to split array in to two arrays on containing names and other containing score. Its better to create a structure having String field for name and integer field for score and write Comparator for sorting the Array containing elements of this Data structure type and sort them.
It will solve all your problems and that too efficiently.
Not many data integrity checks in the methods you are using, but here are the extensions I use to split arrays or any type of enumerable. I have not tested these all that much, so I cannot guarantee that they will work. I have removed all my input validation, but I suggest you add those back your own way.
public static List<List<T>> Split<T>(this IEnumerable<T> collection, Int32 groupSize)
{
var collectionList = collection.ToList();
if (groupSize > collectionList.Count)
groupSize = collectionList.Count;
var chunks = new List<List<T>>();
while (collectionList.Any())
{
var chunk = collectionList.Take(groupSize);
chunks.Add(chunk.ToList());
collectionList = collectionList.Skip(groupSize).ToList();
}
return chunks;
}
public static List<List<T>> Split<T>(this IEnumerable<T> collection, Func<T, Boolean> splitFunction)
{
var collectionList = collection.ToList();
if (collectionList.IsNullOrEmpty())
return new List<List<T>>();
var indices = collectionList.FindIndices(splitFunction); // Custom method that searches for the indices that satisfy the predicate and returns the index of each matching item in the list.
if (indices.IsNullOrEmpty()) // equivalent to indices == null || !indices.Any()
return new List<List<T>> { collectionList };
var chunks = new List<List<T>>();
var lastIndex = 0;
if (indices[0] > 0)
{
chunks.Add(collectionList.Take(indices[0]).ToList());
lastIndex = indices[0];
}
for (var i = 1; i < indices.Count; i++)
{
var chunkSize = indices[i] - lastIndex;
var chunk = collectionList.Skip(lastIndex).Take(chunkSize).ToList();
if (chunk.IsNullOrEmpty())
{
break;
}
chunks.Add(chunk);
lastIndex = indices[i];
}
if (collectionList.Count - lastIndex > 0)
{
var lastChunk = collectionList.Skip(lastIndex).ToList();
chunks.Add(lastChunk);
}
return chunks;
}