Jump to content

Please, help: succession processing

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
Geologist

Geologist

    Newbie

  • Members
  • Pip
  • 5 posts
Hello from Ukraine!

I need to create console application in C#. A lector does not allow to use array. In this program User has to enter integer number K and collection of integer numbers, which do not = 0 (r!=0). After user have entered 0, the collection ends and the program must show on the screen FIRST number of collection, which was > k. If there were no r > k, it must write "0".

I tried to write code, but have problems with data display.

using System;


namespace ConsoleApplication

{

    class Program

    {

        static void Main(string[] args)

        {


            double k, r, l;


            Console.WriteLine("Enter k");

            k = double.Parse(Console.ReadLine());

            r = System.Double.PositiveInfinity;

            Console.WriteLine("Enter r");

            do

            {

                r = double.Parse(Console.ReadLine());

                if (r > k)

                {

                    l = r;

                }

                else if (r == 0)

                {

                    l = 0;                    

                    break;

                }

                [U][B]Console.WriteLine(l);[/B][/U]

            } while (r != 0);

           

        }

    }

}

How can i restructure the program?
Please help!

Sorry for bad English. I hope it is understandable.

#2
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
Hello there!
If you can't use an array, use a list:
List <int> myList = new List<int>();
To add and loop through it:
x=Int32.Parse(Console.ReadLine());
myList.Add(x);

foreach (int number in myList)
{
    Console.WriteLine(number);
}

Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#3
jvaran

jvaran

    Newbie

  • Members
  • Pip
  • 2 posts
I think using the List<T> is the best option, as stated above. If you don't like that, for whatever reason, you can also use Collection<T> I suppose, but it would work the exact same way.

-John
The Code Connection

#4
Geologist

Geologist

    Newbie

  • Members
  • Pip
  • 5 posts
Thanks for Your comments! I can use collection or list and operator "foreach"! :) But how program has to find first number, which is > k? If there is one, application must show it on display, if there is no number > k, show 0.

I'm geologist and i learn C# at university only for 2 months (1 lecture per week), it's new and not so easy language for me now, i have created only 7 progs.;)

#5
jvaran

jvaran

    Newbie

  • Members
  • Pip
  • 2 posts
The more basic way to do it is to loop throught the List manually in a for-loop starting at index zero, comparing each value to k until you find one greater, or until you run out of the loop in which case you would show zero.

If your List is called myList, for example:


int numToDisplay = 0;

 

for(int i=0; i<myList.Count; i++) 

{

    if(myList[i] > k)

    {

        numToDisplay = myList[i];

        break;

    }

}


This will find the first number in your list that is bigger than k, or leave it at zero if there are none.

If you're familiar with LINQ, you can simply query the list directly to fetch the number you want, much like you would do against a database using SQL. If you're not familiar with LINQ, then learning it just for this small project would probably be overkill.


int? numToDisplay = (from n in myList

                     where n > k

                     select n).FirstOrDefault();


if (numToDisplay == null)

{

    numToDisplay = 0;

}


-John
The Code Connection

#6
Geologist

Geologist

    Newbie

  • Members
  • Pip
  • 5 posts
I'm not on my PC right now. Please, check, is this version of code right?


int i, k, x, y, z;

y=0; z=0; i=0;


Console.WriteLine(""Enter K");

k=int.Parse(Console.ReadLine());


do

 {


  Console.WriteLine("Enter collection of numbers x")

  x=int.Parse(Console.Readline());


   i++;

  

  if ((x>k) && (y==0)) //if y!=0, do not search next numbers x>k

    {

     y=x; z=i;


     }

 }

 while (x!=0);

Console.WriteLine("\nx= "+x".\nIt's ordinal number is "+z );


Edited by Geologist, 01 June 2010 - 05:35 AM.


#7
Geologist

Geologist

    Newbie

  • Members
  • Pip
  • 5 posts
Has anybody c# compilator? :) Please, check the code above. I'm not in my native city now.

#8
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts

Geologist said:

Has anybody c# compilator? :) Please, check the code above. I'm not in my native city now.

:P Use a pen and paper.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#9
Geologist

Geologist

    Newbie

  • Members
  • Pip
  • 5 posts

Davide said:

:P Use a pen and paper.

It's better to use a pencil - for mistakes correction.;)

I have created block-scheme, i think the code is right, isn't it? :-P