Help w/ complicated variable test

This is a discussion on Help w/ complicated variable test within the C Programming forums, part of the General Programming Boards category; ok i am pretty new to C here, and have limited programming experience. i know you are thinking "oh god ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    3

    Help w/ complicated variable test

    ok i am pretty new to C here, and have limited programming experience. i know you are thinking "oh god here we go," but don't worry. i don't think it will be that bad.

    i want to assign a value to the variable "type" based on tests of variables "build" and "feet". "type" should be either 1,2,3, or 0. I set it to 0 to start and my logic is that if none of the tests are true (i.e. build != b, B, h, or H) it will remain 0. the problem is that i can't get it to work out correctly.

    Code:
     
    int type_calc(build, feet){
    
    	int type, type1, type2, type3;
    	type = 0;
    
    
    /*if any test evaluates to true, the value 
    returned for that test is 1.  then it is just a matter of multiplying each different test by a different number.  the tests are mutually exclusive, so you can safely say that each number (1,2,3) corresponds to a different type (copper, coax, twisted 
    respectively) and 0 is an invalid entry.*/  
    	
    type = (((build == 'h') || (build == 'H')) && (feet < 20)) +
    
          2*((((build == 'h') || (build == 'H')) && (feet >= 20.0)) ||  
              (((build == 'b') || (build == 'B')) && (feet < 50.0))) +  
    
          3*(((build == 'b') || (build == 'B')) && (feet >= 50.0));
    
    	return (type);
    
    	}
    what am i doing wrong. i have put a million parantheses to attempt to make it work, but to no avail. i think this is true:

    1. test 1 never evaluates to true
    2. test 2 never evaluates to true for build = 'b' || 'B'

    that's what i've seen. i guess there's something wrong with this code, but i can't figure it out for the life of me...

    thanks for help in advance.
    jungle

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    hm... actually this code looks okay - are you sure you pass the right values to your function?

    btw: the types are missing here:
    int type_calc(build, feet)
    should be e.g.
    int type_calc(int build, float feet)

    [edit]
    try:
    int build = 'h';
    float feet = 10.0;
    (((build == 'h') || (build == 'H')) && (feet < 20.0)) //oops: .0 was missing.
    that should evaluate to true
    [/edit]
    Last edited by Raven Arkadon; 02-26-2005 at 12:39 PM.
    signature under construction

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Is this the input/output you would expect?
    type_calc('H',10) = 1
    type_calc('H',35) = 2
    type_calc('H',50) = 2
    type_calc('B',10) = 2
    type_calc('B',35) = 2
    type_calc('B',50) = 3
    type_calc('c',10) = 0
    Wouldn't it be easier to use a couple of temporaries for 'h' or 'b'? Is there some reason you are not testing feet in an if-tree? And is this the output you want?
    Code:
    #include <stdio.h>
    
    int type_calc(int build, double feet)
    {
       int h = build == 'h' || build == 'H';
       int b = build == 'b' || build == 'B';
       if ( feet < 20.0 )
       {
          return h;
       }
       if ( feet < 50.0 )
       {
          return 2 * (h || b);
       }
       return 3 * b;
    }
    
    int main(void)
    {
       int b[] = {'H','B','c'};
       double f[] = {10.0, 35.0, 50.0};
       size_t i, j;
       for ( i = 0; i < sizeof b / sizeof *b; ++i )
       {
          for ( j = 0; j < sizeof f / sizeof *f; ++j )
          {
             printf("type_calc('%c',%g) = %d\n", b[i], f[j], type_calc(b[i], f[j]));
          }
       }
    
       return 0;
    }
    
    /* my output
    type_calc('H',10) = 1
    type_calc('H',35) = 2
    type_calc('H',50) = 0
    type_calc('B',10) = 0
    type_calc('B',35) = 2
    type_calc('B',50) = 3
    type_calc('c',10) = 0
    type_calc('c',35) = 0
    type_calc('c',50) = 0
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    3
    as far as temporaries go, this is for a c class (i hope that doesn't violate any rules here) and we can't use anything we haven't learned yet. basically, it is either this or just a whole lot of if's. the if's would be far simpler, but once i thought of this method, it became a personal vendetta. you know how it goes. it MUST WORK!!!. i'm glad that the code appears to be ok.

    the reason i have no variable types is that it messed up the float variable. this is very very odd. i went through and had the program print 'feet' right before it sent it to the function type_calc (no changes afterward) and then inside type_calc. 20.0 became 2.83 something. i dont know what posessed me to remove the float and char, but that made it work to an extent. this is so odd. i am really considering just trying to cut and paste this tiny bit of code into a new file, since i guess there is some weird character throwing it off somewhere that i can't find. i think it's elsewhere in the code.

    thanks for the help by the way. any fresh insight is appreciated.

    jungle

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    3
    ok i have some input output for you... i just don't understand this. the calling code and beginning of the function follows the in/out. at least the test appears to work.

    Scanned... ID:123 Build:b Feet:50.000000
    First command of type_calc....
    Build: b Feet:3.140625 Type:2

    Scanned... ID:124 Build:B Feet:10.000000
    First command of type_calc....
    Build: B Feet:2.562500 Type:2

    Scanned... ID:125 Build:h Feet:20.000000
    First command of type_calc....
    Build: h Feet:2.812500 Type:1

    Scanned... ID:126 Build:h Feet:15.000000
    First command of type_calc....
    Build: h Feet:2.718750 Type:1

    Scanned... ID:127 Build:H Feet:100.000000
    First command of type_calc....
    Build: H Feet:3.390625 Type:1

    Scanned... ID:128 Build:B Feet:50.000000
    First command of type_calc....
    Build: B Feet:3.140625 Type:2

    Scanned... ID:129 Build:B Feet:100.000000
    First command of type_calc....
    Build: B Feet:3.390625 Type:2

    Scanned... ID:130 Build:c Feet:50.000000
    First command of type_calc....
    Build: c Feet:3.140625 Type:0

    Scanned... ID:131 Build:B Feet:100.000000
    First command of type_calc....
    Build: B Feet:3.390625 Type:2

    Scanned... ID:132 Build:h Feet:200.000000
    First command of type_calc....
    Build: h Feet:3.640625 Type:1

    Scanned... ID:133 Build:c Feet:50.000000
    First command of type_calc....
    Build: c Feet:3.140625 Type:0
    Code:
    scanf("%d %c %f", &id, &build, &feet);
    printf("Scanned... ID:%d Build:%c Feet:%f\n", id, build, feet);
    
    while(flag != EOF){
    	
    	type = type_calc(build, feet);
    Code:
    int type_calc(char build, float feet){
    	
    	printf("\nFirst command of type_calc....\nBuild: %c   Feet:%f", build, feet);
    it would be so great if somebody could explain what the !@#$% this thing is doing.

    jungle

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. My C++ test is COMING!!...
    By [Z-D] in forum C++ Programming
    Replies: 52
    Last Post: 12-01-2006, 08:02 PM
  3. undefined reference
    By 3saul in forum Linux Programming
    Replies: 12
    Last Post: 08-23-2006, 06:28 PM
  4. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 01:25 PM
  5. Trouble with a class variable
    By TravisDane in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2002, 12:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21