By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,589 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

How to find the product of all elements of an array modulo 10^9+7 ?

P: 56
I have written below code ,for the problem stated on below link under topic Array multiplication , I am unable to get the insight of why is the code not working for any test case ?

https://www.hackerearth.com/practice/math/number-theory/modulus-arithmetic/tutorial/


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include<inttypes.h>
  3. #include<stdint.h>
  4. #include<math.h>
  5.  
  6. int main()
  7. {
  8. uintmax_t size;
  9. uintmax_t product=1;
  10. scanf(" %ju",&size);
  11. uintmax_t i;
  12. if(size<=10000 && size>=1)
  13. {
  14.  
  15. uintmax_t array[size];;
  16. for(i=0;i<size;i++)
  17. {
  18.  
  19. scanf(" %ju",&array[i]);
  20. product=product*array[i];
  21.  
  22. }
  23.  
  24. uintmax_t a=pow(10,9);
  25. uintmax_t b=a+7;
  26. uintmax_t c=product % b;
  27. printf("%ju",c);
  28. }
  29.  
  30.   return 0;
  31. }
  32.  
Sep 4 '16 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
It might be right here:

Expand|Select|Wrap|Line Numbers
  1. uintmax_t array[size];;
At the time this line of code is executed, size has not been initialized so you get whatever value is in the variable.

Also, static arrays like this need t know the size before the array can be created.

I suggest:

unitnmax_t* array = 0;

scanf your size

array = malloc(size * sizeof(unitmax_t));

then initialize the array elements

then plug in your element values
Sep 4 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.