Thread: Replace elements of array C with units

  1. #1
    Registered User
    Join Date
    Dec 2022
    Posts
    1

    Question Replace elements of array C with units

    I have a one-dimensional array in which I need to replace the elements of the array after the smallest element with one and find the sum of all the elements before the smallest element, but I don't know how to do it, please help.
    Code:
    #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 };
     int sum;
     float min;
     float Y[16];
         printf("massif Х\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]){
        Y[i]=X[i]; 
        min=X[i];
    } 
        }printf("\nMin = %2.3f", min);
    
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2022
    Posts
    55
    A couple of things
    - The main procedure returns an int and has to be declared accordingly.
    - The corresponding closing brace ends the main procedure. Look where it is in your code. Count opening and closing braces and ensure they are balanced. (Proper code indentation helps a lot.)
    - Initialize sum with 0 to be able to add the value of an element in each iteration of the loop. Consider to declare sum a float rather than an int.
    - Initialize min with the first element (at index 0) in order to have a value to compare with.
    - Declare another variable as int (e.g. indexOfMin) and initialize it with 0. Use it to remember the index of the current min value that you found.
    - You don't need the Y array. You know now the index of the smallest element and thus, you can perform the replacement in the original array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-20-2014, 06:53 PM
  2. find and replace duplicate numbers in array
    By Cathalo in forum C++ Programming
    Replies: 5
    Last Post: 02-17-2009, 11:05 AM
  3. Replace elements in vector
    By franse in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2008, 12:46 PM
  4. Replace Array
    By SARAHCPS in forum C Programming
    Replies: 9
    Last Post: 11-15-2005, 11:07 AM
  5. Replace elements in string
    By Micko in forum C++ Programming
    Replies: 1
    Last Post: 09-05-2005, 04:04 PM

Tags for this Thread