-5

I have a one-dimensional array where I need to replace the array elements after the smallest element with one, but I don't know what I need to write to make this condition work? I will be grateful for your help.

#include <stdio.h>
#include <stdlib.h>
void main() {
  float X[16] = {2.34, -3.42, 0.56, -3.71, 1.25, 6.37, 0.123,  -45.821,
                 32.5, 0.94,  0.54, -1.26, 2.36, 4.32, -5.345, 4.876};
  float sum;
  float min;
  float Y[16];
  printf("Massiv Х\n");
  for (int i = 0; i < 16; i++) {
    printf("%2.3f\t", X[i]);
  }
  for (int i = 0; i < 16; i++) {
    if (min > X[i]) {
      min = X[i];
    }
  }
  printf("\nMin= %2.3f", min);
  printf("\nMassiv \n");
  X[16] = 1;
  for (int i = 0; i < 16; i++) {
    printf("%2.3f\t", X[i]);
  }
  sum = 0;
  for (int i = 0; i < 7; ++i) {
    if (X[i]) {
      sum += X[i];
    }
  }
  printf("\nSum= %2.3f\t", sum);
}
  • 3
    Curious, why is type `float []`, yet constants are `double`? I'd recommend 1 FP type: `double` and save `float` for the cases when you know it is beneficial. – chux - Reinstate Monica Dec 26 '22 at 12:59
  • 2
    Hint: When `if(min>X[i]){` is first executed: what is the value of `min`? – chux - Reinstate Monica Dec 26 '22 at 13:02
  • 1
    You have figured out how to find the minimum element and store that value (with the caveat that chux hinted about above). What's stopping you from storing the index at which point the minimum element was found too? – Ted Lyngmo Dec 26 '22 at 13:02
  • 3
    Please learn about and apply indentation. It helps a lot with anaysing code. – Yunnosch Dec 26 '22 at 13:04
  • 2
    Addition to to @Yunnosch 's comment: it helps even more writing your own code – Jabberwocky Dec 26 '22 at 13:08
  • 4
    Before answering, show that you are interested in your post: edit it and properly indent your code. – Costantino Grana Dec 26 '22 at 13:08
  • `write the code please` We are not a coding service. You are not welcome to come here and demand people write code for you. – Qix - MONICA WAS MISTREATED Dec 26 '22 at 15:52
  • @Qix-MONICAWASMISTREATED I did not ask to write me the entire code, but only the part of the condition that I do not understand – Who is Antonio Dec 26 '22 at 15:59
  • @CostantinoGrana I'm very interested in solving this problem, but I don't know how to properly indent the code. If you can't help me because the code is unclear, I'm sorry, there's nothing I can do about it – Who is Antonio Dec 26 '22 at 16:06
  • @chux-ReinstateMonica Thanks, but could you tell me what my problem is and how to fix it? I have tried many options but I still don't understand how to do it. – Who is Antonio Dec 26 '22 at 16:08
  • Have you tried [something like this](https://formatter.org/) – Shark Dec 26 '22 at 16:10
  • @Shark Oh, never used that but thanks for the tip – Who is Antonio Dec 26 '22 at 17:21
  • @WhoisAntonio --> I asked for [clarification](https://stackoverflow.com/questions/74920527/replace-elements-of-array-c-with-units?noredirect=1#comment132214200_74920527), and [here](https://stackoverflow.com/questions/74920527/replace-elements-of-array-c-with-units?noredirect=1#comment132214228_74920527) and received nothing except this question: "could you tell me what my problem is and how to fix it?" Only getting more questions and not some answers does not encourage assistance. Your call. – chux - Reinstate Monica Dec 26 '22 at 19:31

1 Answers1

-2

First, you have to find the smallest element. You also need a variable set to 0 until you find the min, then set to 1. If the variable is 1, then you set the remaining elements in the array to 1. If the variable is 0, check if the current element is the smallest. Also, another version is saving the position of the min and changing all elements after that position to the end.

  int after_min = 0;    
    float minn = x[0];
    for(int i = 1; i < 16; i++)
        if (minn > x[i])
            minn = x[i];
    for(int i = 0; i < 16; i++) {
        if (after_min)
            x[i] = 1;
        else 
            if (x[i] == minn)
                after_min = 1;
    }

When you do x[16] = 1, you are out of bounds. The first element is x[0], the last is x[15].

BBogdan
  • 14
  • 1
  • 3