0

Here is the set of screen resolution which I want to validate in pair when user input in.

Aspect ratio - 4:3
800  x 600
1024 x 768
1152 x 864
1280 x 960
etc..

Currently here is what I am thinking:

int arrWidth[] = {800, 1024, 1152, 1280};
int arrHeight[] = {600, 768, 864, 960};
for ( int i = 0; i < sizeof(arrWidth)/sizeof(arrWidth[0]); ++i) {
    if ( pInputWidth == arrWidth[i] ) {
        if ( pInputHeight == arrHeight[i] ) {
            // both width and height is good
            return true;
        }
        // width good, but height doesn't match
        return false;
    }
}
// width doesn't match at all
return false;

Is there any better way to do this?

Trung Nguyen
  • 177
  • 1
  • 12
  • 2
    Of course there's a better way: the shown code is completely broken, and the only reason it works for you is because your `sizeof(int *)` is 4. Be prepared for your code to die a fiery death, when it is compiled for a 64 bit platform. Hint: `sizeof(arrWidth)` doesn't do what you think it does. – Sam Varshavchik Dec 01 '16 at 03:49
  • yep, my bad, edited. – Trung Nguyen Dec 01 '16 at 03:52
  • 1
    Looks better, but a better place for this question would be http://codereview.stackexchange.com/ – Sam Varshavchik Dec 01 '16 at 03:55
  • @Ari0nhh any 4:3 aspect ratio would be fine, just don't know how to use that – Trung Nguyen Dec 01 '16 at 03:57
  • @TrungNguyen If all you're looking for is a 4:3 aspect ratio, divide the width by the height and see if it's equal to 4/3. You'll have to use floating point division to make the comparison, of course. – MrEricSir Dec 01 '16 at 04:02
  • Is there a reason you're specifically trying to make your program break on unconventional display sizes? :-) – R.. GitHub STOP HELPING ICE Dec 01 '16 at 04:03
  • 4
    @MrEricSir: That's a very bad idea because the result is never exact. If you do the opposite division it will be exact, but instead you can multiply: `if (4*height==3*width) ...` – R.. GitHub STOP HELPING ICE Dec 01 '16 at 04:04
  • ^ What @R.. said. Integer math would be a better solution here. – MrEricSir Dec 01 '16 at 04:05
  • Why not just make your own data types with overloaded operators? `struct resolution {int width,height};` – Trevor Hickey Dec 01 '16 at 04:21
  • @MrEricSir nice one, I just don't want to mess with double comparison, interger math is good choice, but there should be low and high boundary or the program will break down if there is input like 40x30 :) – Trung Nguyen Dec 01 '16 at 04:23
  • 1
    Check for 4:3 ratio (see above) and `width>=320 && width<65536` or something like that. – R.. GitHub STOP HELPING ICE Dec 01 '16 at 04:39
  • The C (i.e. C99) and C++ (i.e. C++14) standards do not mention any screens. You can have a computer without screen (e.g. the typical web server in a data center is running Linux without any screen). You could need additional libraries (such as [Qt](http://qt.io/) or [SFML](http://www.sfml-dev.org) etc...) to get information at runtime about your screens or displays. – Basile Starynkevitch Dec 01 '16 at 06:23
  • Please specify a single language. The solutions for C and C++ may be different (and likely are given the available libraries) – Vality Dec 06 '16 at 01:32

1 Answers1

2

If you are simply looking to validate which resolutions match a given ratio, you can simply cross-multiply the resolutions and test if they are equal. Here is a short example:

#include <stdio.h>

int main (void) {

    int ar[] = {4, 3},
        aw[] = {800, 1024, 1152, 1280, 1280},
        ah[] = {600, 768, 864, 960, 1024},
        n = sizeof aw/sizeof *aw;

    for (int i = 0; i < n; i++)
        if (ar[0] * ah[i] == ar[1] * aw[i])   /* simple cross-comparison */
            printf (" resolution OK : %4d x %d\n", aw[i], ah[i]);

    return 0;
}

Example Use/Output

$ ./bin/resolution
 resolution OK :  800 x 600
 resolution OK : 1024 x 768
 resolution OK : 1152 x 864
 resolution OK : 1280 x 960

The 1280 x 1024 resolution is not shown because it is not 4:3. If I missed the intent of your question, just let me know. If possible, stay with an integer comparison to avoid some of the gotchas with floating point equality.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • that's close to what I am looking for, cross multiply can help me remove the for loop and 2 array in my question and make it faster. Just need to have low/ high boundary for width and height so it will be good to go – Trung Nguyen Dec 01 '16 at 05:15