Elements
Elements

Reputation: 69

C# user input to set size of array and loop array to display elements

The goal is to create an array size from user input via how many test scores. Then create a loop that will populate an array by prompting the user for each test score from 0 to 100. Finally display the results, typically using another loop.

Question: Why is it when the test scores are entered example "50" it adds 50 elements of 0 to the array?

any assistance would be grateful, thank you. I've seen a few similar posts but couldn't resolve this issue. Also, one was in Spanish.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {


                // prompt user to ask how many test scores to build the size of the array


                Write("How many test scores total: ");
                string sSize = ReadLine();          
                int i = Convert.ToInt32(sSize);              
                int[] score = new int[i];



            // create the loop of asking the test scores limited to the array sSize

            for (int a = 1; a < i + 1; a++)
            {

                Write("Please enter a test score " + a + " from 0 to 100: ");
                string testArray = ReadLine();


                int g = Convert.ToInt32(testArray);

                int[] tests = new int[g];

                //create loop to display all test scores
                foreach (var item in tests)
                    Console.WriteLine(item);





                }

            }
        }
}

Upvotes: 3

Views: 7800

Answers (3)

juharr
juharr

Reputation: 32266

Because you create a new array inside of your loop that is the size of the "score" the user entered and then you loop over it. The values are all zero because when you create an array it is populated with the default value of the type, in this case 0. The second loop should be after the first one and you shouldn't be creating arrays inside of the first loop, just populating the original array (score) that you created to begin with.

Here's what you actually want. Note that you should index starting at 0 and not 1.

Write("How many test scores total: ");
string sSize = ReadLine();          
int i = Convert.ToInt32(sSize);              
int[] score = new int[i];

// create the loop of asking the test scores limited to the array sSize
for (int a = 0; a < i; a++)
{
    Write("Please enter a test score " + (a + 1) + " from 0 to 100: ");
    string testArray = ReadLine();
    int g = Convert.ToInt32(testArray);
    score[a] = g;
}

//create loop to display all test scores
foreach (var item in score)
    Console.WriteLine(item);

You may also want to consider using int.TryParse so you can determine if the user enters an invalid value.

Upvotes: 1

Vikhram
Vikhram

Reputation: 4394

I think you copied pieces of code from different places and haven't been able to put them together properly.

You are creating an unwanted array here : int[] tests = new int[g];. And then trying to use it just worsens your situation.

Also, you haven't handled your indexes properly. When you are learning to program, using proper formatting and good variable names, will help you understand your own code (potentially put together from different places) a lot better - improving your "debugging" skills.

I have a "fixed" version of your code which should be self-explanatory

using System;
using static System.Console;

namespace ConsoleApp3 {
    class Program {
        static void Main(string[] args) {
            // prompt user to ask how many test scores to build the size of the array
            Write("How many test scores total: ");
            string testCountStr = ReadLine();
            int testCount = Convert.ToInt32(testCountStr );
            int[] testScores = new int[testCount];

            // create the loop of asking the test scores limited to the array sSize
            for (int i = 0; i < testCount; i++) {
                Write($"Please enter score for test {i + 1} from 0 to 100: ");
                string scoreStr = ReadLine();
                int score = Convert.ToInt32(scoreStr);
                if (score > 100 || score < 0) {
                    //TODO: handle your error
                    WriteLine("Invalid score. Please try again");
                    --i;
                    continue;
                }
                testScores[i] = score;
            }

            WriteLine();
            // create loop to display all test scores
            for (int i = 0; i < testScores.Length; ++i)
                WriteLine($"Your score for test {i + 1} is {testScores[i]}");


        }
    }
}

Upvotes: 0

Umer
Umer

Reputation: 142

int[] tests = new int[g];

Here you are assigning the size of the array given by the user rather than populating the array, you are missing the populating statement or query for that.

Upvotes: 2

Related Questions