Jump to content

Loops in C#

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Bobthecoder

Bobthecoder

    Newbie

  • Members
  • Pip
  • 2 posts
Hi, nice to meet you all. Anyways I will get straight to the point, I am having difficulty with a programming assignment it involves loops, I ended up missing the week we were discussing them. Anyways after going back through all my notes and reading my great C# textbook :(. I still have no solution. I have put some work into it but I'm still lost at where to start.

I'll link the assignment at the bottom just first going to show you what I have done!

Here are some constants I assuming I will need

const double MIN = 12.0

const double MAX = 36.0

const double SMALL = 20.0

const double MED = 24.0

const double LRG = 30.0

const int SLICES_SMALL = 8

const int SLICES_MED = 12

const int SLICES_LRG = 16

const int SLICES_XLRG = 24

const int INCREMENT = 4

And some variables

bool continue

double diameter

double pizzaArea

int maxSlices

int slices

double sliceArea

Now I know how to ask for the diameter of the pizza which is simply:

Console.Write("Please enter the diameter of your pizza: ");

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

Up to here is as far as I really know, I'm really bad at programming just need to get through this one course and never take it again. Oh here is the download link: Wikisend: free file sharing service assignment.docx

Edit: Guys i'm trying to learn here so if you do post some code or whatever please explain it to me!

Thanks in advance!

Edited by Alyn, 28 October 2011 - 03:15 PM.
added code tag


#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
What, exactly, is the program supposed to do? What is the first step? What should it do after that? Do you have a plan?

As for the loops, how much do you know right now? To be honest, I don't know C#, but I do have some experience with programming, so I'll try to do my best with this.

#3
Bobthecoder

Bobthecoder

    Newbie

  • Members
  • Pip
  • 2 posts
Well if you click my link, thats my assignment on what I am suppose to do. What the first step is to take the input from user. Then next is to run a loop I am assuming that finds out with the diameter given how many slices it will be. Then the area of each slice. As for loops I know pretty much nothing. But if you click my link you can get a better understanding at what i am trying to explain but failing :). Thanks! for responding!

#4
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Well, in that case, you'll need to find the area of the pizza. Something like (JavaScript)[noparse]:[/noparse]
var input= prompt ("Enter the diameter:"); // Ask the user for the diameter. 

var diameter= parseFloat (input);  // The input value is a string (text) right now; 

// we have to convert it to a float and save that in the variable 'diameter'. 

var radius= diameter / 2;          // The radius is half the diameter. 

var area_of_pizza= radius * radius * Math.PI; // r^2 * pi = r * r * pi = area of circle 

var slice_count; 

slice_count= /* however many slices there would be... let's just use 8 as an example */ 8; 

var area_per_slice= area_of_pizza / slice_count;        // The area of each slice is the total area over the number of slices. 

// ... 

I don't really have much time to read through the whole document you posted, but above is probably some of what needs to be done.

Note: The code I posted is in JavaScript; that's not too bad, especially because JavaScript is similar to Java, and Java is similar to C# (well, a lot of programming languages are similar to each other).



* * *

Well, I'm guessing, from the constants you posted, that you're supposed to check the diameter, and then make the number of slices based on that. So how about this?:
// ... get user input, etc. ... 

var slice_count; 

if (diameter <= 20) slice_count= 8; 

else if (diameter <= 24) slice_count= 12; 

else if ... and so on... I think you get the point. 

else alert ("You entered too big of a diameter; try again, next time, with a smaller value."); 

So that's the determining the number of slices part.




Bobthecoder said:

...
bool continue
...

Woaw, don't name variables with names like 'continue' ; even though I don't know C#, I am pretty sure that 'continue' is probably a keyword, as it is in C/C++ and JavaScript and PHP and probably a lot more languages. Some other keywords are 'if', 'else', 'break' (some languages), 'next' (other languages), 'last' (other languages).




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users