๐Ÿ”’ Closed Reverse array

Status
Not open for further replies.

Kiss21

Eternal Poster
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

  • 534049DE-A601-4B11-9970-F0D77FEDC7DA.webp
    534049DE-A601-4B11-9970-F0D77FEDC7DA.webp
    389.1 KB · Views: 16
  • 11917E2A-5BAA-445B-901D-99706ABA1A29.webp
    11917E2A-5BAA-445B-901D-99706ABA1A29.webp
    31.7 KB · Views: 16
Status
Not open for further replies.

About this Thread

  • 12
    Replies
  • 684
    Views
  • 6
    Participants
Last reply from:
Kiss21

Online now

Members online
1,054
Guests online
1,054
Total visitors
2,108

Forum statistics

Threads
2,275,670
Posts
28,964,688
Members
1,231,899
Latest member
jeron05
Back
Top