0

If the array element is twice as large as its predecessor (for example 1,2,3,4,.. in this case element 2 is twice as large as element before 2, which is 1) then program should make average value of these two elements (in this case 1 + 2 = 3, 3/2 = 1.5) and after making average value, program should put that value between these two numbers and print new array. (in this case new array would look like: 1, 1.5, 2, 3, 4, ...) Do you have any idea how to make this? Array consists of double numbers.

#include <math.h>
#include <stdio.h>
#define epsilon 0.0001
int main() {
  int i, k = 0, n;
  double arr[100];
  do {
    printf("Enter number of array elements: ");
    scanf("%d", &n);
  } while (n <= 0 || n > 100);
  printf("Enter array: ");
  for (i = 0; i < n; i++) {
    scanf("%lf", &arr[i]);
  }
  for (i = 0; i < n; i++) {
    if (fabs(arr[i + 1] - 2 * arr[i]) < epsilon) {
     
    }
  }
  return 0;
}
  • Your assignment is probably about learning float to integer conversion, which can result in loss of accuracy. I don't know if you see that in your output or not. New compilers can do really good job of this, you may not even see the problem unless you try more complicated calculations. Look up this issue and try to duplicate the fault first. – Barmak Shemirani Dec 13 '21 at 13:28
  • @BarmakShemirani assignment is about learning arrays, but double numbers just make it a little bit harder –  Dec 13 '21 at 17:38
  • @i486 homework from college class Introduction to Programming, I mentioned just a part of this assignment, after finding this I was supposed to count how many times numbers after comma appear in the whole programme. If the array was [1,2,3,4], new array would be [1, 1.5, 2, 3, 4], and program would printf number 5 appears 1 times, number 0 appears 4 times.... –  Dec 13 '21 at 17:41

2 Answers2

0

I think you need just check if next element is at least twice as large as previous and then print additional number in a sequence.

int main() {
  int i, k = 0, n;
  double arr[100];
  do {
    printf("Enter number of array elements: ");
    scanf("%d", &n);
  } while (n <= 0 || n > 100);
  printf("Enter array: ");
  for (i = 0; i < n; i++) {
    scanf("%lf", &arr[i]);
  }
  printf("%f ", arr[0]);
  double med = 0;
  for (i = 0; i < n - 1; i++) {
    if (arr[i+1] >= 2 * arr[i]) {
      med = (arr[i + 1] + arr[i]) / 2;
      printf("%f ", med);
    }
    printf("%f ", arr[i+1]);
  }
  return 0;
}

Also change number of iterations to n-1.

complikator
  • 235
  • 2
  • 8
  • thank you very much, this works... I would only change if (arr[i+1] >= 2 * arr[i]) to if (fabs(arr[i + 1] - 2 * arr[i]) < epsilon) where epsilon is 0.0001, because of the problems caused when working with double numbers –  Dec 13 '21 at 13:52
  • @coder also you should allocate `arr` dynamically with amount given by `n` – complikator Dec 13 '21 at 13:56
  • I'm new to programming, so I haven't learnt dynamic allocation yet. –  Dec 13 '21 at 13:59
  • _"should allocate arr dynamically"_ - [Look into using VLA](https://en.wikipedia.org/wiki/Variable-length_array) – ryyker Dec 13 '21 at 14:00
  • Yea, I mean declare it as automatic variable, but with length of `n` – complikator Dec 13 '21 at 14:02
  • @complikator - Yes, what you described then is a VLA. The caveat is that VLA, when used like this will only have automatic storage duration. But with this code example, that is not an issue. – ryyker Dec 13 '21 at 14:09
  • Yea, but you don't need to free this memory, so I think it's better for not so big amounts. – complikator Dec 13 '21 at 14:10
0

To address comments suggesting how to create array at runtime...

(and a few other suggestions are in comments in code below.)

Rather than creating an array with set size, create one with the number of elements decided by user by using a VLA. It is a form of dynamically placing an array into stack (as opposed to heap) memory, as such, it does not require use of calloc() et.al. so no need to call free() Example using your original code:

int main(void) {//main() is not a prototyped signature of main. Use void parameter.
  int i = 0 /*k = 0*/, n;//k is not used, init i

//double arr[100] = {0};//better to initialized before using
                    //and even better to use VLA (if using C99 or newer.)
  do {
        if(i++ > 3) {//prevent infinite loop possibility
           printf("%s", "Exiting\n"); 
           return 0;    
        }
        printf("Enter number of array elements: ");
        scanf("%d", &n);
  } while (n <= 0 || n > 100);
  double arr[n];//Variable Length Array (VLA)
  memset(arr, 0, sizeof arr);/initialize to zero
  ...

Be sure to read the VLA link above, as VLA does have its caveats, but will work fine in the case of your code.

ryyker
  • 22,849
  • 3
  • 43
  • 87