CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2011
    Posts
    13

    [RESOLVED] C# Loop Help :$

    Ernesto’s Eggs has different wholesale prices for their eggs, based upon the number sold:
    0 up to but not including 4 dozen $0.50 per dozen
    4 up to but not including 6 dozen $0.45 per dozen
    6 up to but not including 11 dozen $0.40 per dozen
    11 or more dozen $0.35 per dozen
    Extra eggs are priced at 1/12 the per dozen price

    a) Write a program that asks the user for the number of eggs, and then calculates the bill. The program output should look similar to:
    Enter number of eggs: 126
    Your cost is $0.40 per dozen or 0.033 per egg.
    Your bill comes to $4.20

    b) A computer is not always available, so Mr. Ernesto would like a chart showing the price for any number of eggs up to 120. Write a program that displays a chart similar to:
    1 0.04
    2 0.08
    3 0.12
    ...
    48 1.80
    ...

    Basically thats the question im suppose to solve. I created the part A program, but Im lost as to what i should do for the part B program. I know its probably simple, but im a new student.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: C# Loop Help :$

    We don't do homework here, but we're willing to help you solve particular problems.

    We ask that you try to solve the problem and post your code with what you have so far. Then ask specific questions on the part of the code you are having trouble with.

    Btw, welcome to the site.

  3. #3
    Join Date
    Oct 2011
    Posts
    13

    Re: C# Loop Help :$

    Quote Originally Posted by Arjay View Post
    We don't do homework here, but we're willing to help you solve particular problems.
    Seriously... No offense, but in my situation that's just not going to work at all. As I've said I'm NEW. I barely know any codes, and the ones I do know, I've learned from examples or people showing me to do it. I have no idea how to program this without knowing the language. I'd appreciate it if you could help me solve this question, it would only be teaching me. BTW, this assignment is only a practice, its not for actual marks in the course.

    I hope, that I have made my self clear, and that someone will be able to answer this for me.
    I'd greatly appreciate it, as I've been stuck on it for hours.

  4. #4
    Join Date
    Jul 2005
    Location
    Louisville, KY
    Posts
    201

    Cool Re: C# Loop Help :$

    @Arjay - Well said.

    @Nin9tySe7en - If you make an honest attempt and get stuck, the community is very open to helping people. Us doing the work for you, doesn't end up teaching you anything. Crack open a compiler, that'll be the only way you get really good at it, or try simpler problems until you can try and approach this problem.

    I'll be more than happy to help once you get started and try.

    Regards,
    Quinn

  5. #5
    Join Date
    Oct 2011
    Posts
    13

    Re: C# Loop Help :$

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

    Console.WriteLine("\n\n");


    for (double eggsCounterZeroToFour = 1; eggsCounterZeroToFour <= 48; eggsCounterZeroToFour += 1)
    for (double counter = 0.50; counter <= 24; counter += 0.50)
    Console.WriteLine("\t Eggs:" + eggsCounterZeroToFour + "\t Cost:" + counter);


    //Sustains the program until key is pressed
    Console.ReadKey(true);
    }
    }
    }

    This is what I got. I thought i might do each price as a separate loop. When I run this
    it displays one column from 42 to 48 but the numbers are repeated multiple times. The other column displays 0 to 24, but it restarts the loop afterwords. I dont get how to make the two
    columns loop next to eachother without repeating and such.

    Help now? :$

  6. #6
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: C# Loop Help :$

    Code:
    namespace Assignment___ErnestEggs___Part_2
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                Console.WriteLine("\n\n");
    
    
                for (double eggsCounterZeroToFour = 1; eggsCounterZeroToFour <= 48; eggsCounterZeroToFour += 1)
                    for (double counter = 0.50; counter <= 24; counter += 0.50)
                        Console.WriteLine("\t Eggs:" + eggsCounterZeroToFour + "\t Cost:" + counter);
    
    
                //Sustains the program until key is pressed
                Console.ReadKey(true);
            }
        }
    }
    As you know when you do not enclose a loop or an if function's code within brackets {} only the next line of code after it will be run.

    In your example
    Code:
                for (double eggsCounterZeroToFour = 1; eggsCounterZeroToFour <= 48; eggsCounterZeroToFour += 1)
                    for (double counter = 0.50; counter <= 24; counter += 0.50)
                        Console.WriteLine("\t Eggs:" + eggsCounterZeroToFour + "\t Cost:" + counter);
    You have a loop within a loop so what happens?

    1) The first loop tells runs the application to run the next piece of code 48 times. Which in this case is you 2nd loop.
    2) Your second loop tells the application to run the next piece of code 48 times again. In the second loops case you have it writing out information into the console.

    This is whats happening in your application:
    Code:
        Eggs: 1    Cost: 0.50
        Eggs: 1    Cost: 1.00
        .
        .
        .
        .
        Eggs: 1    Cost: 24.00
    The same thing being displayed for 1 - 48 eggs. Your console can't display all that information so it appears as if only 42 - 48 are being processed.

    The for loop is a good way to go about this but what about including If statements into your application.

    Code:
    // You can make the counter an Int since you can't really buy half an egg.
    for (int eggsCounter = 0; eggsCounter < eggsQuantity; eggsCounter ++; 
    {
        // This checks if the amount user entered to buy is less than 4 dozens
        If (eggsQuantity < 48)
        {
            // Place code to run inside these brackets
        }
        else if (// Set secondary condition here)
        {
            // Place code to run inside these brackets
        }
    }
    You can keep on making else if statements for as many different conditions as you need.

    The reason people tell you to try it out yourself is you will never learn from your mistakes if others do it for you. Sure you may learn from examples but there are plenty of examples out there already on loops. They may not be exact examples of your problem but they do show you the general idea of how the function works. Also better to learn the material now than when the test comes around

  7. #7
    Join Date
    Oct 2011
    Posts
    13

    Unhappy Re: C# Loop Help :$

    Quote Originally Posted by Ubiquitous View Post
    Code:
    
    
    As you know when you do not enclose a loop or an if function's code within brackets {} only the next line of code after it will be run.

    In your example
    Code:
                for (double eggsCounterZeroToFour = 1; eggsCounterZeroToFour <= 48; eggsCounterZeroToFour += 1)
                    for (double counter = 0.50; counter <= 24; counter += 0.50)
                        Console.WriteLine("\t Eggs:" + eggsCounterZeroToFour + "\t Cost:" + counter);
    You have a loop within a loop so what happens?

    1) The first loop tells runs the application to run the next piece of code 48 times. Which in this case is you 2nd loop.
    2) Your second loop tells the application to run the next piece of code 48 times again. In the second loops case you have it writing out information into the console.

    This is whats happening in your application:
    Code:
        Eggs: 1    Cost: 0.50
        Eggs: 1    Cost: 1.00
        .
        .
        .
        .
        Eggs: 1    Cost: 24.00
    The same thing being displayed for 1 - 48 eggs. Your console can't display all that information so it appears as if only 42 - 48 are being processed.

    The for loop is a good way to go about this but what about including If statements into your application.

    Code:
    // You can make the counter an Int since you can't really buy half an egg.
    for (int eggsCounter = 0; eggsCounter < eggsQuantity; eggsCounter ++; 
    {
        // This checks if the amount user entered to buy is less than 4 dozens
        If (eggsQuantity < 48)
        {
            // Place code to run inside these brackets
        }
        else if (// Set secondary condition here)
        {
            // Place code to run inside these brackets
        }
    }
    You can keep on making else if statements for as many different conditions as you need.

    The reason people tell you to try it out yourself is you will never learn from your mistakes if others do it for you. Sure you may learn from examples but there are plenty of examples out there already on loops. They may not be exact examples of your problem but they do show you the general idea of how the function works. Also better to learn the material now than when the test comes around

    I don't stand what you've explained to me here. I need to make something like:
    1 0.04
    2 0.08
    3 0.12
    .....
    48 1.80
    49 1.83
    ...
    all the way to 120 eggs.

    There is no user input for this part, it just on long chart/list of 1-120 eggs and the price.

    Could you explain to me in "stupid" language, cuz I don't comprehend exactly what your telling me.

    Thanks for the attempt though


  8. #8
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: C# Loop Help :$

    The answer is within the code I provided you. All you need to do is play around with the code and make it work how you need it to.

  9. #9
    Join Date
    Oct 2011
    Posts
    13

    Unhappy Re: C# Loop Help :$

    Quote Originally Posted by Ubiquitous View Post
    The answer is within the code I provided you. All you need to do is play around with the code and make it work how you need it to.
    Lol, your mistaking me for someone who knows how to use c#.
    I have no idea how to use it. So when you say "play around with the code and make it work how you need it to" your basically telling a 3 year old to solve an algebraic equation.
    In other words, I'm letting you know I'm an idiot when it comes to c# and for that reason I need
    you to be much more specific.

    sorry, again.

  10. #10
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: C# Loop Help :$

    Play around with the code is the best way to learn and understand how to use it. Make some changes, step through the code, look at the contents of your vars at various points along the way and it will start to come together.

    If someone just tells you what to write and you write it then it will work but you will not know how to do it and will not really leanr anything.
    Always use [code][/code] tags when posting code.

  11. #11
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: C# Loop Help :$

    If I were mistaking you for someone who knew c# I myself wouldn't be offering help since I am still a beginner myself

    I am not very different than you as I learn through examples as well but hands on experience is the best teacher. Think of the code as a set of rules you are setting for the computer and the computer must follow them.

    A loop will keep repeating what is inside that loop until the condition is met.

    Ex. While Loop
    Code:
                string userInput = "";
    
                while (userInput != "y")
                {
                    Console.Write("Quit Program? ");
                    userAnswer = Console.ReadLine();
                }
    That loop will keep running until the user enters the letter "y" in lowercase.


    Ex. For Loop
    Code:
               for (int i = 1; i <= 3; i++)
               {
                    Console.WriteLine("This loop has run {0} time(s)", i);
                    //Console.WriteLine("This loop has run " + i + " time(s)"); has same effect as above.
               }
    That loop will run 3 times each time changing the text to display the change in variable i.


    If and else If statements are helpful so you are able to set your program to run a set of different rules when the condition is met.

    Ex. Using If and else if statements
    Code:
                int someNumber = 32; // Put whatever number you want here.
                int userGuess = 0;
    
                while (userGuess != someNumber) // Will keep running while the numbers do not match (!= means not equal to)
                {
                    Console.Write("What is your guess? ");
                    userGuess = int.Parse(Console.ReadLine());
    
                    // Checks to see if number entered is lower or higher or equal to
                    if (userGuess > someNumber)
                    {
                        Console.WriteLine("\nThe number you entered is higher than the number you are looking for.\n");
                    }
                    else if (userGuess < someNumber)
                    {
                        Console.WriteLine("\nThe number you entered is lower than the number you are looking for.\n");
                    }
                    else if (userGuess == someNumber)
                    {
                        Console.WriteLine("\n\nCongratulations you have found the number!\n");
                    }
                }
    
                // Runs after the loop is closed due to conditions being met.
                Console.Write("Press any key to continue...");
                Console.ReadKey();
    While in that loop the if and else if statements check to see if their conditions are met. If either of their conditions is met then the code inside its brackets will be run. In the case you enter a number higher than the number you are looking for it will tell you that you entered a higher number. In the case of a lower number it will tell you that your number is lower and when both numbers match it will congratulate you and end the loop as well.


    Play around with the examples I've given you. Enter things you know well already and see how they are affected when you put them into the loops. Remove parts of code and see what is affected. Did it give you an error? Learn about the error so in the future you can pin point it right away.

    You will find a lot of the people here are willing to help you but not give you code as you will not learn anything that way. You say you don't know c# so take some time make some code and post it here people will guide you in the right path. I know its frustrating trying to make something in the beginning but in the end there is nothing more satisfying than seeing your creation working after many failures

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured