Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C#] Char array - Basic Example
04-03-2010, 08:56 AM (This post was last modified: 04-03-2010 09:28 AM by Back_track.)
Post: #1
[C#] Char array - Basic Example
Code:
/*
* Created by SharpDevelop.
* User: Back_Track
* Date: 4/2/2010
* Time: 6:47 PM
*/
using System;

namespace nd_
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter in Length of your char array: ");//Get user to input value
            int length = Int32.Parse(Console.ReadLine());
            char[] arr = new char[length];//create a char array with the length of the user input
            for(int i =0; i < length; i++)//Basic for loop
            {
                if(i % 2 == 0)//modulus operator to determine if even or odd
                {
                    arr[i] = 'x';//arr[number the loop is on] assign it the value of x
                }
                else{//If not even, odd
                    arr[i] = 'y';// arr[number loop in on] assign the value of y
                    
                }
            }
            
            for ( int j = 0; j != length; j++)//Just for printing out our array
            {
                Console.WriteLine(arr[j] + "\t");//Write arr[value loop is on] then tab
            }
            Console.ReadLine();
        }
    }
}

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
04-03-2010, 08:59 AM
Post: #2
RE: [C#] Char array - Basic Example
Not bad, imo an example should be commented as much as possible to help the reader. Also are character arrays really needed in C#, why not just use string?

Good job on it though

[Image: pgsig copy.png]
Visit this user's website Find all posts by this user
Quote this message in a reply
04-03-2010, 09:25 AM
Post: #3
RE: [C#] Char array - Basic Example
I'll go though and add some comments, i just thought char arrays are just a good introduction to some parts of programming so .. i made this Tongue

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
04-03-2010, 12:17 PM
Post: #4
RE: [C#] Char array - Basic Example
Much better!

Like it now!

Yes char/arrays are very important parts of programming and a good way of learning about data types and memory.

[Image: pgsig copy.png]
Visit this user's website Find all posts by this user
Quote this message in a reply
04-03-2010, 06:32 PM
Post: #5
RE: [C#] Char array - Basic Example
Very good example. On even positions are "x" characters and on odd "y".
Instead of "Int32.Parse" you could use "Convert.ToInt32" but that is the same.
Very good example !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
04-05-2010, 06:44 AM
Post: #6
RE: [C#] Char array - Basic Example
Found a better way to find the odds than the else statement
Code:
if((i%2) != 0)
Basically, if If the remainder of i divided by two is anything other than 0 it assigns odd.

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
04-05-2010, 07:00 AM
Post: #7
RE: [C#] Char array - Basic Example
(04-05-2010 06:44 AM)Back_track Wrote:  Found a better way to find the odds than the else statement
Code:
if((i%2) != 0)
Basically, if If the remainder of i divided by two is anything other than 0 it assigns odd.
Or i think that odd number is if it is equal to 1 like :
Code:
if((i%2) == 1)

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
04-05-2010, 07:18 AM
Post: #8
RE: [C#] Char array - Basic Example
Good old modulus! Smile

[Image: pgsig copy.png]
Visit this user's website Find all posts by this user
Quote this message in a reply
08-25-2010, 08:49 PM
Post: #9
RE: [C#] Char array - Basic Example
I don't quite get the purpose. Incidentally
Code:
char[] text = Console.ReadLine().ToCharArray();

foreach(char c in text)
{
    Console.WriteLine("{0}\t", c.ToString());
}

Poor code, 't is poor.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: