Two brother numbers
Solved
yassine
-
[Dal] Posted messages 6122 Registration date Status Contributor Last intervention -
[Dal] Posted messages 6122 Registration date Status Contributor Last intervention -
Bonjour,
Two integers N1 and N2 are said to be brothers if each digit of N1 appears at least once in N2 and vice versa.
Write a C program that takes two integers N1 and N2, verifies, and displays whether they are brothers or not.
without arrays, strings, or lists.
Two integers N1 and N2 are said to be brothers if each digit of N1 appears at least once in N2 and vice versa.
Write a C program that takes two integers N1 and N2, verifies, and displays whether they are brothers or not.
without arrays, strings, or lists.
2 answers
Hello,
We will not do your exercise for you.
Please describe your problem precisely and post the code you have already written.
Click here for tips on writing messages and here regarding school assignments or PFE.
When posting your code, please remember to consider syntax highlighting.
--
It is very difficult to catch a black cat in a dark room.
Especially when it isn't there...
We will not do your exercise for you.
Please describe your problem precisely and post the code you have already written.
Click here for tips on writing messages and here regarding school assignments or PFE.
When posting your code, please remember to consider syntax highlighting.
--
It is very difficult to catch a black cat in a dark room.
Especially when it isn't there...
Hello,
By default, I would have responded like baladur13, but since the exercise isn't that easy, I think we can at least give you a starting point and then you can show us what you have done, where you are, and what is blocking you.
Since you are not allowed to use strings, you will have to work only with these two integers and retrieve the digits that compose them one by one.
The natural approach (off topic!)
Your exercise, if you represent your two integers as a string of characters in O(N1+N2), relying on an auxiliary array (here
By the way, in higher-level languages (like Python) it’s written even more easily/naturally:
Back to your exercise
Too bad, if you respond with that, you are off topic mwahahaha :D Yes, we use arrays and strings.
Anyway, you will have to take a roundabout way (and this is where you will start showing us your programming abilities :p).
Basically, you need to retrieve each digit say from
Each time you extract a digit (in my example, 3 then 2 then 1), you need to go through
What I just described allows you to check that the digits of
Good luck!
By default, I would have responded like baladur13, but since the exercise isn't that easy, I think we can at least give you a starting point and then you can show us what you have done, where you are, and what is blocking you.
Since you are not allowed to use strings, you will have to work only with these two integers and retrieve the digits that compose them one by one.
The natural approach (off topic!)
Your exercise, if you represent your two integers as a string of characters in O(N1+N2), relying on an auxiliary array (here
tab) that keeps track of the digits encountered in N1 and N2:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> bool are_brothers(int n1, int n2) { unsigned i; bool tab[10]; char *pc; char buffer[100]; bool brothers = true; memset(&tab[0], 0, sizeof(bool) * 10); sprintf(buffer, "%d", n1); for (pc = buffer; *pc; pc++) { i = *pc - '0'; tab[i] = 1; } sprintf(buffer, "%d", n2); for (pc = buffer; *pc; pc++) { i = *pc - '0'; tab[i] = 0; } for (i = 0; i < 10; i++) { if (tab[i] != 0) { brothers = false; break; } } return brothers; } int main(){ int n1, n2; printf("n1? "); scanf("%d", &n1); printf("n2? "); scanf("%d", &n2); if (are_brothers(n1, n2)) { printf("%d et %d sont frères\n", n1, n2); } else { printf("%d et %d ne sont pas frères\n", n1, n2); } return 0; } By the way, in higher-level languages (like Python) it’s written even more easily/naturally:
#/usr/bin/env python3 # -*- coding: utf-8 -*- def are_brothers(n1, n2): return set(str(n1)) == set(str(n2)) n1 = int(input("n1 ? ")) n2 = int(input("n2 ? ")) if are_brothers(n1, n2): print(f"{n1} et {n2} sont frères") else: print(f"{n1} et {n2} ne sont pas frères") Back to your exercise
Too bad, if you respond with that, you are off topic mwahahaha :D Yes, we use arrays and strings.
Anyway, you will have to take a roundabout way (and this is where you will start showing us your programming abilities :p).
Basically, you need to retrieve each digit say from
n1, by doing integer divisions for each power of 10 (for example
123 == 1 * 100 + 2 * 10 + 3 * 1). I advise you to start with the units, then the tens, the hundreds, etc., and stop once you have completely broken down the number. For that, you will need an auxiliary variable to keep track of where you are. For example, if you have extracted the units and the tens, you have consumed
23 != 123, so you need to continue. As a reminder, in C, integer division is done with the
/operator and the remainder is retrieved with
%(for example
13 / 3 == 4and
13 % 3 == 1because
13 == 3 * 4 + 1).
Each time you extract a digit (in my example, 3 then 2 then 1), you need to go through
n2in the same way and see if you can find it.
What I just described allows you to check that the digits of
n1are included in
n2, but you also need to test in the other direction to ensure that
n1and
n2are brothers.
Good luck!
Hello mamiemando,
We also need to deal with the restriction mentioned by Yassine, "without arrays, strings or lists," particularly the "without arrays" part, and understand what this prohibition means.
For example, does this mean that we could store working data in memory allocated with malloc, but that we would refrain from accessing it using the
We also need to deal with the restriction mentioned by Yassine, "without arrays, strings or lists," particularly the "without arrays" part, and understand what this prohibition means.
For example, does this mean that we could store working data in memory allocated with malloc, but that we would refrain from accessing it using the
[]operator and could only access it using pointer arithmetic?
I also think that, in your code, you can replace:
with:
since the array does not need to be reinitialized in the function.
If it turns out that, in his exercise, he cannot use a type array, but only pointers and pointer arithmetic, then to initialize the allocated memory to 0, he can always use calloc() which will do both: allocation and initialization to zero.
https://www.cplusplus.com/reference/cstdlib/calloc/
bool tab[10]; memset(&tab[0], 0, sizeof(bool) * 10);
with:
bool tab[10] = { 0 }; since the array does not need to be reinitialized in the function.
If it turns out that, in his exercise, he cannot use a type array, but only pointers and pointer arithmetic, then to initialize the allocated memory to 0, he can always use calloc() which will do both: allocation and initialization to zero.
https://www.cplusplus.com/reference/cstdlib/calloc/
Hello [Dal],
Thank you for your feedback.
Regarding your comments:
In any case, I don’t think it’s worth spending more time on this question while waiting for Yassine to respond.
Have a good day.
Thank you for your feedback.
Regarding your comments:
- The entire first part is clearly off topic (and marked as such :p). It is only there to show how we would have solved this exercise with minimal complexity (which in itself has educational value, even if that is not the purpose of this exercise). That’s also why I provided ready-to-go code: it is off topic, and dear Yassine is still going to have his work cut out for him.
- Regarding the initialization you mentioned, I’m not familiar with that syntax. I would have said it should be written as
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}. But since it goes against the guidelines, it's not very important. - The second part of my message (see "return to your exercise") suggests taking a radically different path, without arrays/strings/lists. In itself, it’s interesting to show during an exercise that we don't always need to resort to "advanced" concepts to solve a problem. However, in this case, it’s poorly executed, as it comes at the expense of algorithmic complexity (as we will be moving from a linear time algorithm to a quadratic time algorithm), not to mention the extremely cumbersome code it will generate. In that sense, the exercise is not very well thought out and a bit sadistic.
In any case, I don’t think it’s worth spending more time on this question while waiting for Yassine to respond.
Have a good day.
Hello mamiemando,
Yes, I understood your approach, and sorry if my remark about your array initialization was not intended to be useful in this exercise, as the use of arrays seems indeed prohibited. I just wanted to point out a more practical way to do what you wrote.
The syntax
In the C99 standard, this is found in 6.7.8 Initialization, § 21, but it has existed since C89 :-)
21 If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
In short, if the number of elements provided for initialization is less than the elements that the aggregate (here an array) can hold, the remaining elements are implicitly initialized in the same way as static duration variables (which means to 0).
This is quite convenient on a daily basis, and we can do without memset(), which we can use additionally if the function needs to reset the content during its execution.
Regarding the subject and its interpretation...
With your additional explanations, I understand that your interpretation of the restriction is that the retrieved values cannot also be temporarily stored in memory allocated with malloc (and accessed without using the [] operator with pointer arithmetic as I mentioned), and that the only temporary storage would be that of the current digit retrieved in a simple variable (an
Have a nice day too!
P.S.: don’t feel obliged to respond to this post, I think, like you, that Yassine should express himself now :-)
Yes, I understood your approach, and sorry if my remark about your array initialization was not intended to be useful in this exercise, as the use of arrays seems indeed prohibited. I just wanted to point out a more practical way to do what you wrote.
The syntax
bool tab[10] = { 0 }; instead of bool tab[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; is equivalent according to the C standard. In the C99 standard, this is found in 6.7.8 Initialization, § 21, but it has existed since C89 :-)
21 If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
In short, if the number of elements provided for initialization is less than the elements that the aggregate (here an array) can hold, the remaining elements are implicitly initialized in the same way as static duration variables (which means to 0).
This is quite convenient on a daily basis, and we can do without memset(), which we can use additionally if the function needs to reset the content during its execution.
Regarding the subject and its interpretation...
With your additional explanations, I understand that your interpretation of the restriction is that the retrieved values cannot also be temporarily stored in memory allocated with malloc (and accessed without using the [] operator with pointer arithmetic as I mentioned), and that the only temporary storage would be that of the current digit retrieved in a simple variable (an
inthere). If this is indeed the case, the exercise is very convoluted. Perhaps Yassine should check this with his teacher, because if he can decompose numbers (as you propose by Euclidean division) and store the retrieved digits in memory to process them once he has gathered them all, that changes quite a bit...
Have a nice day too!
P.S.: don’t feel obliged to respond to this post, I think, like you, that Yassine should express himself now :-)