Why my C# Code isn't Printing the Required Output - c#

I attempting to create a program that uses multiple methods that would print out base numbers, exponents, and their resulting solutions. I am trying to get it to run and it's nearly completed, but I am encountering a couple issues. The code itself seems to run, but doesn't appear to print out on Visual Studio. I did run it on an online compiler and got this as an output:
It seems I am missing something in my code, but I am unclear as to what I may be missing. This is the code I have created for the project:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project
{
class Program
{
static void Main(string[] args)
{
//Our initialized variables.
int intMinBase = 1;
int intMaxBase = 100;
int intMinExpo = 1;
int intMaxExpo = 10;
//Our arrays for the project, all at a length of 5.
long[] baseNumbers = new long[5];
long[] exponents = new long[5];
long[] results = new long[5];
//Randomize the baseNumbers and exponents!
Random randInt = new Random();
for (long i = 0; i < 5; i++)
{
baseNumbers[i] = randInt.Next(intMinBase, intMaxBase);
exponents[i] = randInt.Next(intMinExpo, intMaxExpo);
}
PrintArrays(baseNumbers, exponents, results);
}
//This is potentially experimental code for the Power Method.
public static int Power(int baseNum, int exponent)
{
int answer;
if (exponent == 1)
{
answer = 1;
}
else
{
answer = baseNum * Power(baseNum, exponent - 1);
}
return answer;
}
//The new method to be printed. Is this the correct manner to display this?
public static void PrintArrays(long[] baseNum, long[] exponent, long[] result)
{
Console.WriteLine($"Base\tExponent\tResult");
for (int print = 0; print < result.GetUpperBound(0); print++)
{
Console.WriteLine(baseNum[print]+"\t"+exponent[print]+"\t"+result[print]);
}
}
}
}
My question is mainly am I missing something and why isn't it appearing to print in Visual Studio yet it's appearing on an online compiler? I suspect the answer to the first part of the question has to do with the methods I used, but I am unsure.

First error: Nowhere is the method Power called and nowhere is the array results filled.
Solution example:
for (long i = 0; i < 5; i++)
{
baseNumbers[i] = randInt.Next(intMinBase, intMaxBase);
exponents[i] = randInt.Next(intMinExpo, intMaxExpo);
results[i] = Power(baseNumbers[i], exponents[i]);
}

Related

.NET fiddle not letting me write in console

NOTE: I have figured out that this only affects .NET fiddle if you are using .NET 6. If you aren't using .NET 6 it works fine.
Whenever I use the Console.WriteLine() function on .NET fiddle, it doesn't let me interact with the console. I'm new at coding and .NET fiddle, so it might be a simple fix. I just hope you can figure it out
I've tried just having the Console.WriteLine. I've triple checked I wrote it right. I rewrote the Convert.ToInt32 several times. I looked up to see if anyone else had this problem. I even checked if Console.WriteLine being under the list maker was the problem, it wasn't.
Here is the code:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<string> shoppingList = new List<string>();
Console.WriteLine("How many items on the list?");
int listLength = Convert.ToInt32( Console.ReadLine() );
for(var i = 0; i < 4; i++) {
shoppingList.Add(Console.ReadLine());
}
for(var i = 0; i < shoppingList.Count; i++) {
Console.WriteLine(shoppingList[i]);
}
}
}
It's working fine here on my side, as you can see here. Have you tried cleaning the cookies of your browser? Note that you have to click on the console to type. Also, I fixed your function where it gives the correct size of the List<string>.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<string> shoppingList = new List<string>();
Console.WriteLine("How many items on the list?");
int listLength = Convert.ToInt32( Console.ReadLine() );
for(var i = 0; i < listLength; i++) {
shoppingList.Add(Console.ReadLine());
}
for(var i = 0; i < shoppingList.Count; i++) {
Console.WriteLine(shoppingList[i]);
}
}
}

Lucky Dip Program

I was trying to make a lucky dip program where 6 random numbers,1-59, are chosen then printed out in an array. I managed to get this to work, however you needed to use an IndexOf method so that no same number was printed twice by checking if the new number is already in the array.
using System;
namespace LuckyDip
{
class Program
{
static void Main(string[] args)
{
int[] luckyNumbers = new int[6];
Random random = new Random();
for (int x = 0; x<6; x++)
{
num[x] = random.Next(1,59);
Console.WriteLine(num[x]);
}
Console.ReadLine();
}
}
}
It prints out numbers, but sometimes they are the same.
You state that you want to use IndexOf, but that method is used for strings (see docs). Your example has an int array so the solution below uses Contains. This solution adds a check within your loop and generates a new number if this number already exists within your array. If you really need to use IndexOf, create a string array and convert the numbers using String.valueOf(randomNumber)
using System;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
int[] luckyNumbers = new int[6];
Random random = new Random();
for (int x = 0; x<6; x++)
{
int randomNumber = random.Next(1,59);
while (luckyNumbers.Contains(randomNumber))
{
randomNumber = random.Next(1,59);
}
luckyNumbers[x] = randomNumber;
Console.WriteLine(luckyNumbers[x]);
}
}
}
Another possible solution would be:
using System;
using System.Collections;
using System.Linq;
public class Program
{
public static void Main()
{
int arraySize = 6;
int[] luckyNumbers = Enumerable.Range(1,59).OrderBy(g => Guid.NewGuid()).Take(arraySize).ToArray();
for (int x = 0; x < luckyNumbers.Length; x++)
{
Console.WriteLine(luckyNumbers[x]);
}
}
}

How do I roll a dice multiple times without getting same result using the Random method? [duplicate]

This question already has answers here:
Random number generator only generating one random number
(15 answers)
Closed 4 years ago.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DieRoller
{
public class Program
{
public static void Main()
{
for (int a = 0; a < 20; a = a + 1)
{
Console.WriteLine(RollDie());
}
Console.ReadLine();
}
public static int RollDie()
{
Random roll = new Random();
int test = roll.Next(1, 6 + 1);
return test;
}
}
}
When I execute this code I get the number 4 multiple times or the number 2 multiple times...etc.
Isn't it supposed to execute the RollDie function for each iteration of the loop? and isn't that supposed to yield a different value each time? pls halp!
EDIT: The thing is guys, I need to generate the randomness only inside the RollDie method, and I can't have any arguments for the RollDie method (Basically I have to generate the randomness only using the random method inside the RollDie method), other questions don't address that.
See comments for explanation of why it doesn't work. Here a possible way to make it work:
public static void Main()
{
Random roll = new Random();
for (int a = 0; a < 20; a = a + 1)
{
Console.WriteLine(RollDie(roll));
}
Console.ReadLine();
}
public static int RollDie(Random roll)
{
int test = roll.Next(1, 6 + 1);
return test;
}
Or, for simplicity, just:
public static void Main()
{
Random roll = new Random();
for (int a = 0; a < 20; a = a + 1)
{
Console.WriteLine(roll.Next(1, 6 + 1));
}
Console.ReadLine();
}

Kattis phonelist issue

Once I run the following local, it is woking fast, but when I submit it to Kattis, It only exceeds 2/5 and I get Time Limit Exceeded.
Any suggestion?
I have tried with a input file with 10000 numbers and it is still fast localy :S
using System;
namespace phonelist
{
class Program
{
static void Main(string[] args)
{
int nrOfPhoneNrs = 0;
bool consistent;
int nrOfTestCases = Convert.ToInt32(Console.ReadLine().Trim());
for (byte i = 0; i < nrOfTestCases; i++)
{
consistent = false;
nrOfPhoneNrs = Convert.ToInt32(Console.ReadLine().Trim());
string[] phList = new string[nrOfPhoneNrs];
int n = 0;
while (n < nrOfPhoneNrs)
{
phList[n] = Console.ReadLine();
n++;
}
Array.Sort(phList);
int runs = nrOfPhoneNrs - 1;
for (int p = 0; p < runs; p++)
{
if (phList[p + 1].StartsWith(phList[p]))
{
consistent= true;
break;
}
}
Console.WriteLine(consistent? "NO" : "YES");
}
}
}
}
I think that your main problem is that you're using StartsWith and Array.Sort methods.
I don't want to give you too detailed advice (so that you can still solve it by yourself) but let me just suggest considering a different data structure than an array of strings, perhaps HashSet<string>.

Build Error with no Logical/Syntax Errors

I was beginning to create a command line program that allowed me to complete problem 4 on Project Euler.
https://projecteuler.net/problem=4
While I was in the middle of scripting the program, I decided to debug (I'm using Visual Studio Suite 2015)
When it began, it had a message pop up and show that the program itself has "Build Errors."
I have absolutely no idea what happened.
In the version below (C#), the script has no logical errors, and it has no syntax errors either.
Could you please tell me what's wrong with it?
Thank you!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Problem_4
{
class Program
{
static void Main(string[] args)
{
//Check Palindrome
//Not complete code.
int originalValue = 9009;
int oppositeValue = 0; ;
int carryOppositeValue;
int placeValue = 0;
bool completedCycle = false;
int numCycles = 0;
int numCycles2 = 0;
int[] array = { 0 };
for(placeValue = numCycles; completedCycle; placeValue++)
{
array[placeValue] = originalValue % 10;
originalValue = originalValue / 10;
if(originalValue >= 0)
{
numCycles++;
} else
{
completedCycle = true;
Console.WriteLine("The number has " + numCycles + "Place Values");
}
}
for(placeValue = 0; placeValue < 50; placeValue += 10)
{
if (numCycles2 == 0)
{
oppositeValue = array[0];
}
else
{
carryOppositeValue = placeValue * array[placeValue / 10];
oppositeValue += carryOppositeValue;
}
}
}
}
}
I compiled this code and got no errors. I would suggest doing a build/clean.
If that doesn't work I would delete the bin and obj folders then recompile.

Categories