Thread: Some quick help please with functions.

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    Some quick help please with functions.

    Hey this is some of my homework and i only have 3 questions. Here are the questions and what i have tried for my answer. I also put the error that i recieve.

    1. Given the char * variables name1, name2, and name3, write a fragment of code that assigns the largest value to the variable max (assume all three have already been declared and have been assigned values).

    i put:
    Code:
    if(strcmp(name1,name2,name3)=name1)
                    max = name1;
                    else if(strcmp(name1,name2,name3)=name2)
                    max = name2;
                    else ( max = name3;)
    errors: You might want to recall what strcmp returns when
    one string is greater than another.
    You have to use a relational operator in this exercise.



    2. Write an expression that evaluates to true if the value of the string variable s1 is greater than the value of string variable s2.

    i put:
    Code:
    strcmp(*s1,*s2) = s1
    errors:You might want to recall what strcmp returns when
    one string is greater than another.
    You have to use a relational operator in this exercise.
    There is no need to use an assignment operator here.


    3.Write the definition of a function minMax that has five parameters. The first three parameters are integers. The last two are set by the function to the largest and smallest of the values of the first three parameters. The function does not return a value.

    The function can be used as follows:

    Code:
    int a=31, b=5, c=19 big, small;
    	minMax(a,b,c,&big,&small);
    	    /* big is now 31 */
                        /* small is now 5 */
    my code:
    Code:
      void minMax(int a,int b, int c, int &big, int &small){(if(a>b && a>c){
         a=big};
      if(b>a && b>c){
         b=big};
      else if(c>a && c>b){
         c=big};
      else if(a<b && a<c){
         a=small};
      else if(b<a && b<c){
         b=small};
      else (c=small);
    
      }
    errors:Mismatched ( and }
    Common Errors:
    - Make sure your if/else statements are correct.
    - Make sure you have semicolons at the end of each statement.
    - Make sure you are assigning the correct values to big and small.



    thanks so much guys

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    your third question seemes pretty simple to solve your syntax for that function is either really weird and barely used or wrong here is how I would write it
    Code:
    void minMax(int a,int b, int c, int &big, int &small){
    	if(a>b && a>c){
         		a=big;
    	}
    	if(b>a && b>c){
         		b=big;
    	}
    	else if(c>a && c>b){
         		c=big;
    	}
    	else if(a<b && a<c){
         		a=small;
    	}
    	else if(b<a && b<c){
         		b=small;
    	}
    	else{
    		c=small;
    	}
    }

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    This is what the "relational operator" comments are referring to:

    When you say
    a = b
    you are assigning the value of b to a. In other words, changing the value of a.

    If you want to compare two variables, but not change the value of either, you say
    if (a == b)

    The "mismatched" comments are telling you that the number of left parentheses in your code is not equalto the number of right parentheses. They must be equal. Same with left and right brackets.

    Code:
    In other words, every ( must be matched with a ), every { must be matched with a }
    You are using strcmp incorrectly. You should read a manual or textbook explanation of how it works.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    nothing has helped me

    The code that you posted still says it is incorrect.
    how do i use strcmp?

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    NAME
    strcmp, strncmp - compare two strings

    SYNOPSIS
    #include <string.h>

    int strcmp(const char *s1, const char *s2);

    int strncmp(const char *s1, const char *s2, size_t n);

    DESCRIPTION
    The strcmp() function compares the two strings s1 and s2. It returns
    an integer less than, equal to, or greater than zero if s1 is found,
    respectively, to be less than, to match, or be greater than s2.

    The strncmp() function is similar, except it only compares the first
    (at most) n characters of s1 and s2.

    RETURN VALUE
    The strcmp() and strncmp() functions return an integer less than, equal
    to, or greater than zero if s1 (or the first n bytes thereof) is found,
    respectively, to be less than, to match, or be greater than s2.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    For example:

    Code:
    result = strcmp(string1,string2)
    if (result < 0)
            printf("string1 is less\n");
    else if (result > 0)
            printf("string2 is less\n);
    else
            printf("both strings are equal\n");

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    56

    i still dont get it

    I still cant get them, will someone please help

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well post your latest code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding What Functions Are In A DLL/Lib?
    By Geolingo in forum Windows Programming
    Replies: 2
    Last Post: 09-27-2003, 09:58 PM
  2. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 10:56 AM
  3. DLL class member functions
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2003, 06:59 AM
  4. Borland, libraries, and template member functions
    By roktsyntst in forum C++ Programming
    Replies: 0
    Last Post: 06-01-2003, 09:52 PM
  5. inheritance & virtual functions
    By xagiber in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2002, 12:10 PM