3

I am trying to ask the user to input 10 numbers. After receiving the numbers, I am storing them in an array followed by printing the array. I came up with the following code to do the task but it is not printing the array.

Also feel that I may have rattled on way too much code for a simple task. Do note that I am very new to c# thus not familiar with advanced stuff or possibly even most of basic stuff. Even the "convert.toInt32", I adopted from reading around and not taught in class yet.

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

namespace test_Array
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            int b;
            int c;
            int d;
            int e;
            int f;
            int g;
            int h;
            int i;
            int j; 

            Console.WriteLine("Please input 10 numbers. Press 'ENTER' after each number.");
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            c = Convert.ToInt32(Console.ReadLine());
            d = Convert.ToInt32(Console.ReadLine());
            e = Convert.ToInt32(Console.ReadLine());
            f = Convert.ToInt32(Console.ReadLine());
            g = Convert.ToInt32(Console.ReadLine());
            h = Convert.ToInt32(Console.ReadLine());
            i = Convert.ToInt32(Console.ReadLine());
            j = Convert.ToInt32(Console.ReadLine());

            int[] newArray = {a,b,c,d,e,f,g,h,i,j};

            Console.WriteLine(newArray);

            Console.ReadLine();
        }
    }
}
1
  • 3
    Consider using a loop that runs 10 times instead Commented Oct 2, 2013 at 20:18

4 Answers 4

7

use a for loop.

int[] newArray = new int[10];
for (int i = 0; i < newArray.Length; i++)
{
    newArray[i] = Convert.ToInt32(Console.ReadLine());
}

You can use the same loop to display as well:

for (int i = 0; i < newArray.Length; i++)
{
    Console.WriteLine(newArray[i]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

The array is constructed properly (albeit not concisely); the issue is printing it.
3

The ToString method of arrays (which is what Console.WriteLine is calling in your code) isn't overloaded to print out the contents of the array. It leaves the basic object implementation of just printing the type name.

You need to manually iterate the array and print out the individual values (or use a method that will do that for you).

I.e.

foreach(var item in array)
    Console.WriteLine(item)

or

Console.WriteLine(string.Join("\n", array));

1 Comment

While the 1st technique is quite routine, the 2nd technique is quite interesting.
1
    static void Main(string[] args)
    {
        int[] rollno = new int[10];
        Console.WriteLine("Enter the 10 numbers");
        for (int s = 0; s < 9; s++)
        {
            rollno[s] = Convert.ToInt32(Console.ReadLine());
            rollno[s] +=  110;

        }
        for (int j = 0; j < 9; j++)
        {
            Console.WriteLine("The sum of first 10 numbers is : {0}", rollno[j]);
        }
        Console.ReadLine();
    }
}

}

Comments

0

You could simplify things a lot with the following:

static void Main(string[] args)
{
    int newArray = new int[10];

    Console.WriteLine("Please input 10 numbers. Press 'ENTER' after each number.");
    for (int i = 0; i < 10; i++) {
        newArray[i] = Convert.ToInt32(Console.ReadLine());
    }

    Console.WriteLine("The values you've entered are:");
    Console.WriteLine(String.Join(", ", newArray));

    Console.ReadLine();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.