Jagged array exceeding input c# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
`
for(int i=0;i<jagged.Length;i++)
{
for(int j=0;j<jagged[i].Length;i++)
{
jagged[i][j] = Convert.ToInt32(Console.ReadLine());
}
}
`
i want this piece of code to take 2 score inputs in jagged[0] and 3 score inputs in jagged1 but it is taking 5 inputs in both the cases.
jagged array input problem
For "n" number of teams with "at" attempts, i want to store the scores of these teams in "at" attempts in the jagged array
example :
if used inputs n = 2 and at= 2 for the first team.
a jagged[2][2] should be created,
i want to store the scores in this way-
jagged[0] should be = new int[2]; (which i did at line 15)
The lines starting from 21 are supposed to store these scores,
but when i run the program, it takes input of scores more than the Length.
What i want is -
jagged[0][0] should store the first score input
jagged0 should store the second score input
and it should stop there. but it doesnt , it takes input 3 more times.(5 times total)
Similarly, the attempts for 2nd team is input as 3 and still it is taking the input 2 more times (5 times total)

You are using a triple cicle, but you just need 2. Remove the k for cicle,and change the condition for the j cicle to j <jag[i].Length

Related

How to declare and store value in 2D array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I tried to print every pair of element while it's factorial
language: language-c
#include<studio library>//
in main function I try to store 2D array which size is
a[1][3];
And, I write these code
integer a[1][3] = {1,2,3,4,5};
and I find factorial of 5 = 120
I have to print three element together
it is possible 120 times;
so ,I can try 120 different value using print function function
in a[1][3];
like this output
1,2,3 is 1.
1,2,4 is 2.
1,2,5 is 3.
1,3,4 is 4.
1,3,5 up to 120
I want to know that how to store value in 2D array
but the array is a[1][3]
You may set the values of j and k to be "i-dependent" but they are not updating their value, you should add a j++,k++ next to the i++

How do I take an indefinitely large set of user inputs and save them into an array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am making a program (in school for intro to computer programming(dont worry, this is all extra credit stuff, not asking for cheat answers)) that will take a user decided set of user inputs and save them all to do math (more specifically, finding the median, but I want to figure that out myself) on them. I am fairly sure that I need an array to do this (even my teacher hinted that that is what you needed to do).
My plan is to have a variable x that will decide the amount of separate numbers in the array (will not only be the number in the array, but also the number to check how many times I want to run the loop for asking for numbers), and then to have that many user inputted numbers inputting by the user, and then to be able to take those numbers and find the median of them (I will probably have to check if the number is even or odd first, then sort the numbers (somehow), then find the middlemost number.)
Thanks!
Instead of using array which has fixed size, try using List.
List allows you to add indefinitely many (until it fits into memory) elements.
So, in your for loop you could read user input and add it to the list.
After that you can sort entire list using sort method and return middle element, which is a median.
It could look like that:
int n = //TODO: read number of user inputs
List<int> elems = new List<int> ();
for (int i = 0; i < n; i++) {
int x = // get input
elems.Add(x);
}
Console.WriteLine(elems[elems.Length / 2]);
You can take a look at the List<T> class here for storing the numbers.
For reading input, see Console.ReadLine.
For converting string to int, take a look at int.TryParse.
I gave you the tools, now start coding! :D

Iterate with threshold stored in database [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a variable number of transactions in every month saved in the database. I have to calculate a payment according a structure like as 0-100 transaction, 1 € for every transaction, 101-200 2€ for each, 201 to 300 3€ for each, etc. The first 100 transactions I’ll pay 100 €, the next 100 transactions I’ll pay 200 € and in this way. Until here is easy for me, the problem become because the number of thresholds (in the earlier example was 3), can be variable, sometimes is 3, other times could be 2, 4, 5 or whatever. These thresholds are stored in a table in the database. Please, can someone help me. Thanks in advance.
For each threshold in the database, you will sore a LOWER and UPPER bound and Price (0,100,1),(101,200,2),(201,300,3).
Load the thresholds into a List with each member containing the lower, upper, and price
List<int[]> thresholds = new List<int[]>
thresholds.Add(new int[]{0,100,1});
thresholds.Add(new int[]{101,200,2);//etc.
private void calculateTotal(int numberSold){
int total =0;
foreach(int[] bound in thresholds){
if(numberSold>=bound[0]&&numberSold<=bound[1]{
total+= bound[2]*numberSold;
}
}
return total;
}
That's about the best I can do for you based on your limited question. Play with this and if you have a problem, ask a specific question based on the code you create and the specific problem you are having.

Beginner trying to program simple change calculator [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm new to coding and need to write a program for C#
the goal is to write a program that prompts the user to enter an amount including decimals. and the program gives the user back the remander as money. ie...
quater dimes nickle pennie
First you want to get the users input and store it in a variable.
The input will be text so you have to convert that to a decimal.
Then you will need to do your calculations. That is probably the part you need help on.
Lets say the user entered in 25 dollars and 43 cents - 25.43.
I would first divide the dollars by 4 to get the number or quarters.
Then go from there. You will likely use mod %.
Have fun, im sure you can figure it out if you try.
I think the problem isn't with your int.Parse, but with the way you were concatenating your Console.WriteLine string. Here's the fix, I'm not sure if that's what you were needing.
When you concatenate object to a string, add {}. Starting with the number 0 add those inside depending on how many object you want to add to the string like so: {0}. Once you're done with the string you can add the objects after the it separated by a comma.
int dollar = 100;
decimal quarter = .25m;
decimal dime = .10m;
decimal pennie = .01m;
Console.WriteLine("tell me how much money you have, make sure you include doolars and cents. ");
string userMoney = Console.ReadLine();
double userMoney1 = double.Parse(userMoney);
Console.WriteLine("that equals, {0} ", userMoney);
Console.ReadLine();

Regarding logical equation on my C programming [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a situation on my C programming here and just wondering whether my solution is the correct way:
I have a LED display with particle count sensor and will show 6 digit of seven segment numbers as the count value. The sensor will give voltage input value. The input is from 0V to 10V. So the range of 0V-10V need to be shown in the display as 000000 to 999999 count.
My solution is:
Display number = Input voltage * 99999.9
For example:
Display number = 10.000*99999.9=999999
Display number = 5.500*99999.9=549999
Display number = 2.300*99999.9=229999
Is this the correct solution? I notice that I will get a lot of 9 on the display value.
The most usable and user friendly solution is to ignore the fact that your most significant digit is capable of displaying up to 9 and simply multiply by 10000 unless you desperately need the maxim resolution in which case simply use a scale factor of 100000 and document that your range is 0-9.99999.
My reasoning is that it is better to either loose one digit in the accuracy across the whole range or clip just the maximum value than to have an error across the entire range.

Categories