Advertisement
dmilicev

n-th_root_v1.c

Nov 23rd, 2019
218
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.30 KB | None
  1. /*
  2.  
  3.     n-th_root_v1.c      by Dragan Milicev
  4.  
  5.     for Dan Sanchez It-it
  6.     https://web.facebook.com/danzkie8888
  7.  
  8.     Task:
  9.     https://web.facebook.com/photo.php?fbid=987931031542534&set=p.987931031542534&type=3&theater
  10.  
  11.     The task is vague and confusing.
  12.  
  13.     What is int& k and int &p in
  14.     double findK(int& k, int &p) ?
  15.  
  16.     Why does a function get an argument k when it should return a value of k ?
  17.  
  18.     3 ^ 23 != 12167
  19.  
  20.     Arguments should be pointers to n and p.
  21.  
  22.  
  23.     You can find all my C programs at Dragan Milicev's pastebin:
  24.  
  25.     https://pastebin.com/u/dmilicev
  26.  
  27. */
  28.  
  29. #include <stdio.h>
  30.  
  31. double findK_no_pointers( int n, int p )
  32. {
  33.     int res=n;
  34.     double k=0.0;
  35.  
  36.     while( res <= p )
  37.     {
  38.         res = res * n;
  39.         k++;
  40.     }
  41.  
  42.     return k;
  43. }
  44.  
  45. double findK( int *n, int *p )
  46. {
  47.     int res=*n;
  48.     double k=0.0;
  49.  
  50.     while( res <= *p )
  51.     {
  52.         res = res * (*n);
  53.         k++;
  54.     }
  55.  
  56.     return k;
  57. }
  58.  
  59. int main(void)
  60. {
  61.     int n, p;
  62.     double k;
  63.  
  64.     printf("\n Enter n = ");
  65.     scanf("%d",&n);
  66.  
  67.     printf("\n Enter p = ");
  68.     scanf("%d",&p);
  69.  
  70.     if( n<1 || p<1)
  71.     {
  72.         printf("\n n and p must be greater than or equal to 1 \n\n");
  73.         return 1;
  74.     }
  75.  
  76.     k = findK_no_pointers( n, p );
  77.     printf("\n findK_no_pointers(), Result is k = %.0f \n\n", k);
  78.  
  79.     k = findK( &n, &p );
  80.     printf("\n findK(), Result is k = %.0f \n\n", k);
  81.  
  82.  
  83.     return 0;
  84. }
Advertisement
RAW Paste Data Copied
Advertisement