# Thread: Help with reverse number

1. ## 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. 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. 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. 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. I underestand what mean inverse of a number 1/number. I mean if i have int a =12 how can i show 21 ?

6. Originally Posted by kzzmzz
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.

7. 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. 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. 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. 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));```

11. 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!

12. Originally Posted by kzzmzz
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. 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