Thread: Help with reverse number

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    18

    Help with reverse number

    Hi,
    i have this exersize and i need help

    "The harmonic mean of two numbers is obtained by taking the inverses of the two
    numbers, averaging them, and taking the inverse of the result. Write a function
    that takes two double arguments and returns the harmonic mean of the two
    numbers."

    This is what i have so far:

    Code:
    #include <stdio.h>
    void reverse (int *a, int *b);
    
    
    int main (void)
    {
        int c,d,sab;
    
    
        printf ("Unesite dva broja: ");
        scanf ("%d %d", &c,&d);
        reverse(&c,&d);
        printf ("%d %d\n",c,d);
        sab=c+d;
        printf ("%d",sab);
    }
    
    
    
    
    void reverse (int *a,int *b)
    {
        int g,h;
    
    
        g=*a;
        *a=*b;
        *b=g;
    }
    How can i reverse sab( inverse of the result)? With array maybe?

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Firstly, you need to use floating point numbers, not integers. I suggest using doubles (as the question itself says).

    Secondly, it doesn't say "reverse", it says "inverse". The inverse of a number is simply 1 divided by that number:
    Code:
    #include <stdio.h>
    int main() {
        double d = 5.0;
        double id = 1.0 / d;
        printf("d: %lf   id: %lf\n", d, id);
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    18
    I didnt underestand question good , my english is not so good

    btw can you explain me how can i reverse double or int, i would like to know that?

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I don't know what you mean by "reverse" a double or int.
    I showed how to calculate the inverse of a double in the example above (1.0 / d).
    If you still have problems, show your attempt.

  5. #5
    Registered User
    Join Date
    Feb 2017
    Posts
    18
    I underestand what mean inverse of a number 1/number. I mean if i have int a =12 how can i show 21 ?

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by kzzmzz View Post
    I underestand what mean inverse of a number 1/number. I mean if i have int a =12 how can i show 21 ?
    Use the modulus "%" and division "/" operators and a loop.

    Hint added below is NOT complete C code.
    Code:
    x % 10
    x / 10
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Feb 2017
    Posts
    18
    I got it , thanks

    I have one more problem if you can give me some hint ?

    If i have char array for example

    char small_letters = ["a","b","c", "d"............];
    char caps_letters = ["A", "B", "C", "D" ..............];

    and i ask user to type one letter

    how can i output for example if a user type d or D i have to show output "Letter D is in a 3 place in a array" ?

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    If the letters are going to be in alphabetical order anyway, there is a chance that this formula will work: ch - 'a' + 1 for lower case and ch - 'A' + 1 for upper.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you are supposed to use the arrays as shown, and you correct the syntax ...

    Code:
    char small_letters[] = { 'a', 'b', 'c', 'd' /* etc */ };
    char caps_letters[] = { 'A', 'B', 'C', 'D' /* etc */ };
    ... then simply loop through the first array. If the value at i is equal to the letter of interest, then i is the index of interest. Done. If not, move on to the next array and do the same thing.

    You should also determine whether or not the character was found, so that (in the case of the latter) you can print the appropriate "not found" message.

  10. #10
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    If you just want to know the position in the alphabet of a letter then:
    Code:
        // 1-based position in alphabet (a is 1, b is 2, etc)
        // (for 0-based, get rid of the + 1)
        if (c >= 'a' && c <= 'z')
            printf("lowercase: position %d\n", c - 'a' + 1);
        else if (c >= 'A' && c <= 'Z')
            printf("uppercase: position %d\n", c - 'A' + 1);
        else
            printf("%c is not a letter\n", c);
    To search for the position in an array of characters (0-based offset):
    Code:
        char arr[] = "thequickbrownfxjmpsvlazydg";
    
        char *p = strchr(arr, c); // include string.h for strchr
    
        if (p == NULL)
            printf("%c is not in the array\n", c);
        else
            printf("%c is at offset %d\n", c, (int)(p - arr));
    Last edited by algorism; 02-22-2017 at 09:31 PM.

  11. #11
    Registered User
    Join Date
    Feb 2017
    Posts
    18
    Matticus:

    I misspelled array declaration because im also learning php and you can declare array in php array = [ "a"]

    I just started to learn pointers and arrays and im not sure how to loop index and compare value in index with input, can i do something like this

    Code:
    char small_letters [25] = { 'a', 'g', 'r',..........};
    char input;
    int *i;
    
    for (*i=0; *i<25; *i++)
       if (small_letters[*i] ==input) 
    ..
    ...
    or something else?



    Algorism i dont know about that command yet (strchr ) but i will googe for it.


    Thank you all for helping me!
    Last edited by kzzmzz; 02-23-2017 at 04:21 AM.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by kzzmzz View Post
    I just started to learn pointers and arrays and im not sure how to loop index and compare value in index with input, can i do something like this
    i does not need to be a pointer, just an int. Other than that, it looks like you're on the right track.

    Have you tried actually compiling and running the code? That is the best way to find out if your idea will work.

  13. #13
    Registered User
    Join Date
    Feb 2017
    Posts
    18
    Just need to add line for cleaning buffer

    Code:
    //Write a program that reads characters from the standard input to end-of-file. For//each character, have the program report whether it is a letter. If it is a letter, also
    //report its numerical location in the alphabet. For example, c and C would both be
    //letter 3. Incorporate a function that takes a character as an argument and returns
    //the numerical location if the character is a letter and that returns –1 otherwise.
    
    
    #include <stdio.h>
    
    
    
    
    int main (void){
    
    
    
    
    char ch;
    int i;
    const char s_letters[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    const char c_letters[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    
    
    
    
    printf ("Enter one letter: ");
    
    
    while ((ch=getchar())!=EOF && ch!='`'){
        for(i=0;i<26;i++){
           if (s_letters[i]==ch)
            printf("Position of %c in English Alphabets is %d\n\n",ch,i+1);
            else if (c_letters[i]==ch)
            printf("Position of %c in English Alphabets is %d\n\n",ch,i+1);
            }
            printf ("Enter one letter again or ` for quit: ");
            ch=getchar();
    }
    printf ("Exit...");
    return 0;
    }

    Thanks Matticus

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse of a number
    By vijaygame in forum C Programming
    Replies: 2
    Last Post: 01-11-2016, 11:04 AM
  2. Replies: 2
    Last Post: 10-31-2009, 06:49 PM
  3. Reverse a number
    By a2008 in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2008, 11:04 PM
  4. reverse a number digits
    By tootoo in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2007, 11:24 AM
  5. recursivly reverse a number
    By doug in forum C Programming
    Replies: 2
    Last Post: 02-24-2002, 09:59 AM

Tags for this Thread