Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 273 274 [275] 276 277 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 882988 times)

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4110 on: March 06, 2013, 04:11:24 am »

Yeah, you have the right idea,

or you could use a flag (bool) to have it know after at least one number has been entered and have it use the first number entered as the largest and the smallest and compare the rest against them.

bool flag = 0;
int largest, smallest, sum, average, numbers, number;

if (flag==0)
{
   largest = number;
   smallest = number;
   flag = 1;
}

if (number > largest)
   largest = number;

if (number < smallest)
   number=smallest;

sum+=number;
numbers++;  //holds how many numbers have been entered, used to find average.
average= sum/numbers;


just wrote this up real fast,  you'll also want to initialize all the ints to 0 during declaration.


or if you don't want to do it with a bool, you can set smallest to a value that you know won't be used,
like since they only going to use positive numbers, you could initialize smallest to -1  and have it check
if (smallest == -1)
  {smallest = number}

then that'd give you your starting point and you could check the rest against it.
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Soadreqm

  • Bay Watcher
  • I'm okay with this. I'm okay with a lot of things.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4111 on: March 06, 2013, 01:08:40 pm »

If you let the user input any number they want, you CAN'T trust that you'll only be getting positive numbers. You might not even get numbers. Letters, punctuation, empty strings, control characters, whatever is even remotely feasible for the user to input. Assign the variables to the actual largest and smallest numbers available. In Java, you'd use Integer.MIN_VALUE and Integer.MAX_VALUE, but for all I know about C, you'll have to assign them manually. How many bits are C integers, anyway?

If you want to do this thing properly, you'll also want to actively decide what your function does when it gets zero numbers, and then document it somewhere. :P
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #4112 on: March 06, 2013, 02:25:43 pm »

How many bits are C integers, anyway?

char - Smallest addressable unit of the machine that can contain basic character set.
short - At least 16 bits in size.
int - At least 16 bits in size.
long - At least 32 bits in size.
long long - At least 64 bits in size.
float - single precision, unspecified but usually IEEE 754 (32 bits)
double - double precision, unspecified but usually IEEE 754 (64 bits)
long double - extended precision, unspecified but usually IEEE 754 quadruple (96 bits), non-IEEE double-double (80), or same as double.

int is fairly frequently 32 bits instead of 16 (key word there: minimum), and long on 64-bit compilers has been known to be 64-bits. Heck, can't even be guaranteed that int isn't 64 bits.
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4113 on: March 06, 2013, 03:37:55 pm »

Here's a little trick that I learned last semester that's quite ingenious:

Let's say you have an array of ints.

Code: [Select]
int arr[SIZE]; // SIZE is some value determined elsewhere

You initialize the array with values, and now you want to find the largest or smallest value in the array.

Code: [Select]
int largest = arr[0]; // assume that the first value is the largest
int i;
for(i = 1; i < SIZE; i++) {
    if(arr[i] > largest) largest = arr[i];
}

The smallest value can be found in the same way, just by flipping the comparison operator. No need for flags or anything like that. Just assume that the first value is the one you want, and check that assumption as you iterate through the array.

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4114 on: March 06, 2013, 03:59:06 pm »

Basically the same method as above, but as after-task processing instead of dynamic processing.

Basically, if you want to display a running min/max, go with the first method. If you want to display it after-the-fact, Mego's is probably a lot more clear. (And you can do smallest in the same loop)

Make sure to insure you have at least one value!

(Note: You'll probably be better off using iterators instead of indices if you're using a more powerful container model, which I assume you are)
« Last Edit: March 06, 2013, 04:06:54 pm by GlyphGryph »
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4115 on: March 06, 2013, 04:18:59 pm »

(Note: You'll probably be better off using iterators instead of indices if you're using a more powerful container model, which I assume you are)

I wrote that example in C, since that was the language that the question was asked in. If it was an object-oriented language, the solution would be trivial:

Code: (Java 1) [Select]
int[] arr = new int[SIZE];
// initialize
java.util.Arrays.sort(arr);
// arr[0] is the smallest, arr[arr.length-1] is the largest

Code: (Java 2) [Select]
java.util.ArrayList<Integer> al = new java.util.ArrayList<Integer>();
// initialize
java.util.Collections.sort(al);
// al.get(0) is the smallest, al.get(al.size()-1) is the largest

Code: (C++) [Select]
std::vector<int> vec;
// initialize
std::sort(vec)
// vec.at(0) is the smallest, vec.at(vec.size()-1) is the largest

Don't want to modify the original container? Copy it before sorting.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4116 on: March 06, 2013, 04:40:32 pm »

Here's what I'd use:

Code: (After-task version (C)) [Select]
typedef yourNumericalTypeHere T;
T max(T data[], uint dataLength) {
  T max = T_MINIMUM_VALUE;
  for (uint index = 0; i < dataLength; ++i) {
   if (max < next) max = next;
  }
  return max;
}
Code: (OO running total version (C++)) [Select]
template<typename T>
class RunningMaxCalculator {
 public:
  T currentMax;
  RunningMaxCalculator(T initValue): max(initValue) {}
  inline void addValue(const T& value) {
    if (currentMax < value) currentMax = value;
  }
}

-SORTING-
Oh Mego, the processing overhead of sorting a whole list just to get its maximum makes me shudder.
« Last Edit: March 06, 2013, 04:43:33 pm by MagmaMcFry »
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4117 on: March 06, 2013, 04:44:57 pm »

Do we really care for the overhead?
Between the fact that it is a tiny program, and one designed for learning I think if there is a sorting method all the better!

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4118 on: March 06, 2013, 04:59:40 pm »

RE: Minimum integers.

Man, I just assume 100000 == infinity and start my count there.  Works in 99% of the cases.   :P
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4119 on: March 06, 2013, 05:07:47 pm »

In C++ I use numeric_limits<int>::min() and max(), though doubles and floats have to use lowest() instead of min() I think.
Also, another method can be setting the number to NaN, then comparing so as...

if (array[index]<max) ; else max = array[index];
Reason: NaN, +-1.#INF, and #IND all always fail comparison checks. :D
So if x:=NaN, x==x is false.

Correct me if I'm wrong.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Soadreqm

  • Bay Watcher
  • I'm okay with this. I'm okay with a lot of things.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4120 on: March 06, 2013, 05:13:56 pm »

Man, I just assume 100000 == infinity and start my count there.  Works in 99% of the cases.   :P

If it works in 99% of the cases, that means that for every 100 numbers you handle in a computer program, one is going to be wrong. Have fun tracking down which one. >:]
Logged

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4121 on: March 06, 2013, 05:17:10 pm »

Man, I just assume 100000 == infinity and start my count there.  Works in 99% of the cases.   :P

If it works in 99% of the cases, that means that for every 100 numbers you handle in a computer program, one is going to be wrong. Have fun tracking down which one. >:]

I don't think it'd be that hard to track down which one it is, off the bat, I'd guess that it's going to be the one over 100000.
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4122 on: March 06, 2013, 05:36:31 pm »

Do we really care for the overhead?
Between the fact that it is a tiny program, and one designed for learning I think if there is a sorting method all the better!
Yes. Worst case for a linear search in an unsorted list is O=n time, O=1 space. Average case for any sort is no better than O=nlog(n), with a possibility of between O=1 and O=n space. If you are sorting to find a min or max, you're doing it wrong. I may learn how to shoot a gun by aiming and firing it at my foot, but I don't recommend that as a means of learning.

If you sorted a list just to find the min/max value, you would be held liable for homicide charges when your professor facepalms so hard their skull shatters and lodges itself deeply into the wall behind them.
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4123 on: March 06, 2013, 05:41:09 pm »

Do we really care for the overhead?
Between the fact that it is a tiny program, and one designed for learning I think if there is a sorting method all the better!
Yes. Worst case for a linear search in an unsorted list is O=n time, O=1 space. Average case for any sort is no better than O=nlog(n), with a possibility of between O=1 and O=n space. If you are sorting to find a min or max, you're doing it wrong. I may learn how to shoot a gun by aiming and firing it at my foot, but I don't recommend that as a means of learning.

If you sorted a list just to find the min/max value, you would be held liable for homicide charges when your professor facepalms so hard their skull shatters and lodges itself deeply into the wall behind them.

The goal was a simple-to-program method. Yes, there are far better methods. I am well aware of this, and would never use sorting as an actual approach. I just chose sorting because it's very easy to demonstrate as an alternative method.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4124 on: March 06, 2013, 05:46:29 pm »

Programmers, always trying to find solutions, never trying to solve problems...
If it says anywhere on the marking criteria 'Must solve for n integers within x time' I would be very surprised.
Pages: 1 ... 273 274 [275] 276 277 ... 796