C# Practice Program (NOT HW)

Started by
11 comments, last by Darkmirrorforce 17 years, 11 months ago

//Holds a hardcoded array with 10 zipcodes
//Prompts user to enter Continuously  a zip code and display
//a message indicating whether the zip code is one of the ones in the 
//hardcoded array, so I know I need a while loop to keep the program doing and
//a sentinel value to stop the loop, i'm just stuck on how to do this

using System;
public class CheckZips
{
	public static void Main()
	{
		int[] zip = {64015,64016,64017,64018,64019,64020,64021,64022,64023,64024};
		int x;
		string entryString;
		int entryZip;
		Console.Write("Enter a Zip Code " );
		entryString= Console.ReadLine();
		entryZip= Convert.ToInt32(entryString);
		x= Array.BinarySearch (zip, entryZip);
		if(x<0)
			Console.WriteLine("Zip Code: {0} was not found", entryZip);
		else
			Console.WriteLine("Zip Code: {0} was found", entryZip);
	}
}
[Edited by - Darkmirrorforce on May 16, 2006 10:29:11 PM]
Advertisement
Humm..

1st: Plz post your message with a better subject... "I need help ASAP" isn't a valid subject. (Check the FAQ)

2nd: This smeel the homework!

3rd: Where's that? Do it! I won't tell you how... but you know what to do, do it! BinarySearch isnt a loop... do a loop... (that a good hint for you to finish this off).

Quote:
so I know I need a while loop to keep the program doing and a sentinel value to stop the loop, i'm just stuck on how to do this
Quote:Original post by LowRad
Humm..

1st: Plz post your message with a better subject... "I need help ASAP" isn't a valid subject. (Check the FAQ)

2nd: This smeel the homework!

3rd: Where's that? Do it! I won't tell you how... but you know what to do, do it! BinarySearch isnt a loop... do a loop... (that a good hint for you to finish this off).

Quote:
so I know I need a while loop to keep the program doing and a sentinel value to stop the loop, i'm just stuck on how to do this



Ok, this smeel the homework? If I knew how to do it I would have done it and there would be no reason for me to post it. I just know the objective, and I know what I need to use but I'm not sure what I need to compare to what to get this program working right. I use BinarySearch to search the array for a value that is entered (Binary has to be in numerical order). I might try a do loop then, but I'm really bad at creating the conditions. This isn't homework, it is a program to practice with.
Quote:
but I'm really bad at creating the conditions.


That's why you have homework, and why we're not going to do it for you.
You pretty much answered the question yourself.
"I need a while loop"

Steven Yau
[Blog] [Portfolio]

Geez, I'm not even sure if the while loop will work.. I've tried it many times with different configurations with no success. Forget it, no one here gives a damn about helping people out.
You have an array, and you need to loop through it. Put the loop around a statement the walks through the array and do your tests as you walk.

theTroll
i also smell homework. google even pointed me to an assignment with identical variable names, but i guess it could be a coincidence.
This space for rent.
Quote:Original post by gumpy macdrunken
i also smell homework. google even pointed me to an assignment with identical variable names, but i guess it could be a coincidence.


Its a practice program, not homework.. I get nothing for this. I wouldn't be surprised if you did find this on google since I did get a link to it from a professor. I'm just trying to solve this one so it will help me out on my Real Homework I'll be getting soon.
Look it's not about helping or not people out... if we give you the answer that WONT help you... So im here to help... that's why ill give you a crash course again....

// The while loop will loop until the condition evalutes to false
// this loop is sometimes not executed at all (condition is false)
while (condition)
{
... instruction ...
}

// The do/while loop is basicly the same as the while loop
// But, it always being executed at least 1 time
do
{
... instruction ...
}
while(condition)

// This loop do:
// init: before first iteration of the loop (normally init a counter)
// condition: a normal condition (stop iterating the loop when the condition is false)
// iter: this is being executed after each iteration of the loop
for(init; condition; iter)
{
// instruction
}


Conditions are expressed with operators and operands... (think of it as a normal 'if')

IE:

int aCounter = 10;

while(aCounter > 0)
{
cout << "Value:" << aCounter << ", ";
aCounter = aCounter - 1;
}


This will output:
10, 9, 8, 7, 6, 5, 4, 3, 2, 1

That's, this is the best i can do to help you out!

Good luck!

[Edited by - LowRad on May 16, 2006 11:01:45 PM]

This topic is closed to new replies.

Advertisement