What's new

Closed How to remove empty elements in double array?

Status
Not open for further replies.

FourBytes

Eternal Poster
Joined
Feb 26, 2018
Posts
554
Reaction
61
Points
258
double[] arr = new double[100];

tapos ang nalagyan ko lang ng value halimbawa ay 6 elements lang. Pano ko buburahin yung 94 elements na walng laman?
 
Kung gusto mong burahin just for the sake na mabura eh wag mo na pag-aksayahan ng oras kasi sa ibang programming languages eh dynamic ang length ng array.

Kung may algo ka naman na gagawin, pakibigay yung kabuuhan nung gusto mong mangyari
 
Kung gusto mong burahin just for the sake na mabura eh wag mo na pag-aksayahan ng oras kasi sa ibang programming languages eh dynamic ang length ng array.

Kung may algo ka naman na gagawin, pakibigay yung kabuuhan nung gusto mong mangyari
ganto kase ser.
windows form kase gamit ko at nag initialize ako ng array na 100 elements .. tas gumamit ako ng datagridview para sa input ng user. tas pag naconvert ko na sa double yung mga input ng user cocomputin ko yung present worth(sa engineering economy project namin na pinapagawa na calculator) tas para macompute to kelangan kong alisin yung mga labis na elements.
 
Kung gusto mong burahin just for the sake na mabura eh wag mo na pag-aksayahan ng oras kasi sa ibang programming languages eh dynamic ang length ng array.

Kung may algo ka naman na gagawin, pakibigay yung kabuuhan nung gusto mong mangyari
nag initialize ako ng 100 elements din kase ibat iba ang bilang ng elements nainiinput ng user. pedeng 10, o kaya 11 o 3 lang for example
 
Regarding your problem and algorithm you are using. I think using a "Linked List" would be more efficient.

But if you don't to want to use a "Linked list" then I suggest you don't use a regular array and instead use a dynamic array. So you can choose its length at runtime.
 
Or you can roll your own. Eto sa C++ nga lang:

Code:
#include <iostream>

int main(void) {
    int len_newarray{0};
    int len_oldarray{100};
    float old_array[len_oldarray] { 2.30, 5,66, 1,23 }; // initialize the old_array with some values

    // Set some value some random blocks away from the initialized ones.
    // Be wary of array bounds
    old_array[15] = 1001;
    old_array[25] = 5301;

    // Printing the OLD array while deriving the length of the NEW array at the same time
    std::cout << "\n\nPrinting the OLD array..." << std::endl;
    for (int i=0; i<len_oldarray; i++) {
        std::cout << "Element [" << i++ << "]: " << old_array[i] << std::endl;
        if (old_array[i] != 0) {
            len_newarray++;
        }
    }
    std::cout << "Lengh of new array: " << len_newarray << std::endl;

    // Create a new array
    float new_array[len_newarray];
    int index_newarray{0};

    // Derive any non-empty values off of OLD array and copy that over to the NEW array
    for (int i=0; i<len_oldarray; i++) {
        if (old_array[i] != 0) {
            new_array[index_newarray++] = old_array[i];
        }
    }

    // Print the NEW array
    for (int i=0; i<len_newarray; i++) {
        std::cout << "Element [" << i << "]: " << new_array[i] << std:: endl;
    }
    return 0;
}
 
Last edited:
May 1 mali na di ko napansin (at line 16)
Code:
std::cout << "Element [" << i++ << "]: " << old_array[i] << std::endl;

nagka-doble yung increment operator ko sa 'for block, i++'

So eto yung mas tama:

Code:
#include <iostream>

int main(void) {
    int len_newarray{0};
    int len_oldarray{100};
    float old_array[len_oldarray] { 2.30, 5,66, 1,23 }; // initialize the old_array with some values

    // Set some value some random blocks away from the initialized ones.
    // Be wary of array bound
    old_array[15] = 1001;
    old_array[25] = 5301;

    // Printing the OLD array while deriving the length of the NEW array at the same time
    std::cout << "\n\nPrinting the OLD array..." << std::endl;
    for (int i=0; i<len_oldarray; i++) {
        std::cout << "Element [" << i << "]: " << old_array[i] << std::endl;
        if (old_array[i] != 0) {
            len_newarray++;
        }
    }
    std::cout << "\n\nLengh of new array: " << len_newarray << std::endl;

    // Create a new array
    float new_array[len_newarray];
    int index_newarray{0};

    // Derive any non-empty values of OLD array and copy that over to the NEW array
    for (int i=0; i<len_oldarray; i++) {
        if (old_array[i] != 0) {
            new_array[index_newarray++] = old_array[i];
        }
    }

    // Print the NEW array
    std::cout << "Values of NEW array" << std::endl;
    for (int i=0; i<len_newarray; i++) {
        std::cout << "Element [" << i << "]: " << new_array[i] << std:: endl;
    }
    return 0;
}
 
Last edited:
Status
Not open for further replies.
Back
Top