Write a C program which finds the multipliers of any integer and keeps them in an array. Your program will get the number from t | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Write a C program which finds the multipliers of any integer and keeps them in an array. Your program will get the number from t

How can I write this program? Could someone help me about that?

3rd Dec 2018, 6:22 PM
Nurullah Aydın
Nurullah Aydın - avatar
20 Answers
+ 2
No problem, if you don't manage to understand something, ask again. C can be pretty rough simply because it is so close to the machine and requires you to deal with stuff like memory allocation, etc.
3rd Dec 2018, 7:56 PM
Shadow
Shadow - avatar
+ 2
It is pretty much undefined behaviour. For example, here on SL the limit is actually 5 for that array. I found this thread on the web dealing with stuff like this, maybe you want to read it: https://stackoverflow.com/questions/30631020/why-can-i-set-values-outside-of-the-arrays-range The main point is that the array size is still 0, which means you are operating outside the bounds of the array, which is always equal to undefined behaviour. You can't tell what will be happening. It might even look like it runs fine, while it actually doesn't. Since it is undefined, I can't tell you why the limit is 4 where you ran the program. On a sidenote, I'd argue that I am using an array aswell. An array is nothing more than a pointer to a continuous block of memory. I just allocated that block of memory by myself. If you declare an array like int arr[5]; the allocation also happens, but behind the scenes so you can't really see it. Then again, that is something you should discuss with your teacher.
3rd Dec 2018, 7:47 PM
Shadow
Shadow - avatar
+ 2
Well, the easiest way would be to have a distinct variable that keeps track of that. You can initialize it to 0 and increment it whenever you find a divisor. That is how I did it in my sample program.
3rd Dec 2018, 8:16 PM
Shadow
Shadow - avatar
+ 1
The problem is that the size of an array needs to be determined at compile time, when the program hasn't started running. In your case, the size of your array is 0, and you cant simply add more elements to it because its size is fixed. You can't put any elements in that array. Because you don't know the number of divisors (I think that's what you meant?) beforehand, you need to allocate the memory for your array by yourself when you know how many divisors you have to store. Based on how I understood the task, this is how I'd probably do it: https://code.sololearn.com/cdaWK71NbY6i/?ref=app
3rd Dec 2018, 7:12 PM
Shadow
Shadow - avatar
+ 1
Sure, a number like 1000 will work for most numbers, unless you have to deal with extremely huge ones :) Just keep in mind that you have a lot of unused (and uninitialized) array elements after the ones holding the divisors, in case you ever have to loop over that array.
3rd Dec 2018, 8:08 PM
Shadow
Shadow - avatar
+ 1
Like this? https://code.sololearn.com/ceVdUC6g485k/?ref=app I didnt't mean you have to define an array like that. It's just so that you have most likely more array elements than divisors, so you dont give a value to every element of the array. The ones you haven't explicitly given a value will have any garbage value that was stored in that element before it was allocated for your array. Since these garbage values are pretty random, they might not be proper divisors of your number.
3rd Dec 2018, 8:32 PM
Shadow
Shadow - avatar
+ 1
Yep, true. So when you have to loop over the array, you can treat it like it has the size 'counter' instead of '1000'.
3rd Dec 2018, 9:14 PM
Shadow
Shadow - avatar
0
I understood the problem and saw your code but I need to use array for this homework
3rd Dec 2018, 7:20 PM
Nurullah Aydın
Nurullah Aydın - avatar
0
#include<stdio.h> int main() { int i; int t=0; int j[t]; printf("Enter an integer :"); scanf("%d",&i); for(int u=1; u<=i; u++) { if(i%u==0){ t++; j[t]=u; printf("%d ",j[t]); } } return 0; } I wrote like this and I determined the array memory with t variable but it still doesn't work correctly
3rd Dec 2018, 7:21 PM
Nurullah Aydın
Nurullah Aydın - avatar
0
Always the compiler accept the arrays limits as 4 . Why ?
3rd Dec 2018, 7:22 PM
Nurullah Aydın
Nurullah Aydın - avatar
0
I will try to understand thank you for your all explanations :))
3rd Dec 2018, 7:52 PM
Nurullah Aydın
Nurullah Aydın - avatar
0
I solved the problem as determine an array which is arr[1000].1000 is just symbol number to the find multipliers of the integers. #include<stdio.h> int main() { int i; int a=0; int t=0; int j[1000]; printf("Enter an integer :"); scanf("%d",&i); for(int u=1; u<=i; u++) { if(i%u==0){ j[t]=u; printf("%d ",j[t]); t++; } } return 0; } This code works correctly. Thank you again :)
3rd Dec 2018, 8:00 PM
Nurullah Aydın
Nurullah Aydın - avatar
0
Yep exactly but otherwise I cant do this homework. How can I know how many divisors for the taken integer ?
3rd Dec 2018, 8:11 PM
Nurullah Aydın
Nurullah Aydın - avatar
0
I tried this but it didn't work correctly. It print the screen just 6 numbers I dont understand why
3rd Dec 2018, 8:21 PM
Nurullah Aydın
Nurullah Aydın - avatar
0
Could you edit my code as your practical way and send me for checking to my code
3rd Dec 2018, 8:22 PM
Nurullah Aydın
Nurullah Aydın - avatar
0
Also you said that you need to define the array with using t numbers inside the array. How can I write a variable to inside the array ?
3rd Dec 2018, 8:26 PM
Nurullah Aydın
Nurullah Aydın - avatar
0
I think I understand :D We determine an array with 1000 and then check it with counter. By this way, we dont use memory with garbage rondom numbers
3rd Dec 2018, 9:03 PM
Nurullah Aydın
Nurullah Aydın - avatar
0
Is that true ?
3rd Dec 2018, 9:03 PM
Nurullah Aydın
Nurullah Aydın - avatar
0
Okey I understand completely. I am glad to meet you . Thank you . Good night :)))
3rd Dec 2018, 9:15 PM
Nurullah Aydın
Nurullah Aydın - avatar
- 1
#include<stdio.h> int main() { int i; int t=0; int j[]={}; printf("Enter an integer :"); scanf("%d",&i); for(int q=1; q<=i; q++) { if(i%q==0){ j[t]=q; t++; } printf("%d\t",j[0]); } return 0; } I wrote like this but it doesn't work correctly
3rd Dec 2018, 6:23 PM
Nurullah Aydın
Nurullah Aydın - avatar