What's new

Closed Reverse array

Status
Not open for further replies.

Kiss21

Eternal Poster
Established
Joined
Oct 24, 2015
Posts
553
Reaction
2,012
Points
459
Ano po yung condition kaoag ang user ay mag iinput ng 1 5 7 2 3 the mag rereverse po yung output nga “ 3 2 7 51” pahelp naman po ng code sa c++ using arrays
 
Kung int ang array with 5 elements para ma-reverse yung array:
Code:
for(i = 0; i < 5; i++){
    cin >> inputArray[i];
}
for(i = 0; i < 5; i++){
    outputArray[i] = inputArray[5-i-1];
}
for(i = 0; i < 5; i++){
    cout << outputArray[i] << " ";
}
 
Easy:
Code:
#include <iostream>

int main(int argc, char *argv[]) {
    int myarray[] = { 1, 5, 7, 2, 3 };
    size_t length = (sizeof(myarray)/sizeof(*myarray))-1;
    std::cout << "\nCurrent Array" << std::endl;
    for (int& i : myarray) {
        std::cout << i << " " << std::flush;
    }
    std::cout << "\n\nNow displaying in reverse order..." << std::endl;
    for (int i=length; i>-1; i--) {
        std::cout << myarray[i] << " " << std::flush;
    }
    std::cout << std::endl;
    return 0;
}
 
Kung int ang array with 5 elements para ma-reverse yung array:
Code:
for(i = 0; i < 5; i++){
    cin >> inputArray[i];
}
for(i = 0; i < 5; i++){
    outputArray[i] = inputArray[5-i-1];
}
for(i = 0; i < 5; i++){
    cout << outputArray[i] << " ";
}
Ako po sana mag iinput ng elements while running tapos yung program nlng po mag rereverese
 
Can you try this:
Code:
#include <iostream>

int main(int argc, char *argv[]) {
    int length, *array;
    std::cout << "Enter length of array: " << std::flush;
    std::cin >> length;
    int *myarray = new int[length];
    for (int i=0; i<length; i++) {
        std::cout << "Enter new element: " << std::flush;
        std::cin >> myarray[i];
    }

    std::cout << "\nDisplaying the elements in the array..." << std::endl;
    for (int i=0; i<length; i++) {
        std::cout << myarray[i] << " " << std::flush;;
    }
    std::cout << std::endl;

    std::cout << "\n\nNow displaying the reversed elements in the array..." << std::endl;
    for (int i=(--length); i!=-1; i--) {
        std::cout << myarray[i] << " " << std::flush;;
    }
    std::cout << std::endl;
    return 0;
}
 
Mga master ano po ba mali sa condition ko? Bakit ganyan po ba out nya mga sir? Pasuggest naman mga sir
 

Attachments

Status
Not open for further replies.

Similar threads

Back
Top