?
Solved

How can I use foreach statement?

Posted on 2003-03-13
8
Medium Priority
?
263 Views
Last Modified: 2010-04-15
I wrote a program that reads 10 characters into an array and prints out the array sorted in alphabetic order.However, I want to use foreach statement in my program.How do I that?

Here is my code:
     static void Main(string[] args)
          {
               char []c = new char[10];
               for(int i = 0; i<c.Length ; i++)
               {
                Console.WriteLine("Enter a letter: ");
                    c[i]=Char.Parse(Console.ReadLine());

               }
               for(int i = 0;i < c.Length; i++)
                for(int j = 0; j < c.Length-1; j++)
                   {
                         if(c[i]>c[j])
                         {
                              char letter       = c[i];
                              c[i]              = c[j];
                              c[j]              = letter;
                         }
                }
             for(int i = 0;i < c.Length; i++)
                    Console.Write(c[i]+ " ");
         
          }
     }
0
Comment
Question by:tonino_31
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
8 Comments
 

Author Comment

by:tonino_31
ID: 8130000
thank you for your time
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8130368
where do you want to use foreach?


for example, you can do this:

           foreach(char letter in c)
                   Console.Write(letter + " ");
 
is that what you had in mind?

AW
0
 

Author Comment

by:tonino_31
ID: 8130848
thanks Arthur. Infact, I tried the code in the last for statement in my program and it compiles but instead of sorting the letters from the first letter is goes backward. for example if the letters were {s,e,u,j,z,k} it gives me {z,u,s,k,j,e}

this is what I did to my code:
static void Main(string[] args)
{
   char []c = new char[10];
   for(int i = 0; i<c.Length ; i++)
    {
      Console.WriteLine("Enter 10 letters: ");
        c[i]=Char.Parse(Console.ReadLine());
    }
    //for(int i = 0;i < c.Length; i++)
     //for(int j = 0; j < c.Length-1; j++)
     {
       if(c[i]>c[j])
       {
          char letter       = c[i];
          c[i]              = c[j];
          c[j]              = letter;
      }
        }
     
      foreach(char letter in c)
          Console.Write(letter + "");
}

I want to use the foreach statements where the comments are
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 160 total points
ID: 8131069
you can't use foreach there, as you would be changing the letter itself (the value in the foreach loop).

As the For loops are currently coded, you are not changing the value of the loop index, but rather interchanging the values of the array that the loop index points to.

change it like this:

for(int i = 0;i < c.Length; i++)
    for(int j = 0; j < c.Length-1; j++)
    {
      if(c[i]<c[j])
      {
         char letter       = c[i];
         c[i]              = c[j];
         c[j]              = letter;
     }
       }


that should reverse the sort.

AW
0
 

Author Comment

by:tonino_31
ID: 8131536
It worked thanks for your time.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8133110
you're welcome..

AW
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question