C++ 将数组元素向右推送

c++ pushing array elements to the right

本文关键字:数组元素 C++      更新时间:2023-10-16

我是编程新手,有这个 c++ 学校测验。

我想将输入的数组元素推到右侧。

这是我现有的代码

块引用

// automatac++.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <list>
#include <iterator>
#include <conio.h>
#include <cstring>
//using namespace std;
using std::cout;
using std::cin;
using std::endl;
void menu_sel();
void push_element();
//void rem_element();
int in;
char array[100];
int p_elements;
int main()
{
menu_sel();
return 0;
}
void push_element() {
int p_elements;
for (int e = 0; e < 10; e++) {

}
cout << "Enter number of elements:";
cin >> p_elements;
cout << "Enter only  " << p_elements << " elements"<<endl;
for (int e = 0; e < p_elements; e++  ) {
cin >> array[e];
}
cout << "Pushed elements are :";
for (int e = 0; e < p_elements; ++e) {
cout << array[e]<<" ";
}
//getch();
menu_sel();
}
/*
void rem_element() {
char remove;
int arr_position;
cout << "You have selected Pop.nRemove elements from arrays n";
cout << "Enter data to remove";
cin >>remove;
for (int ie -) {
}

system("pause");

}
*/

void menu_sel() {

int input;
cout << "n****Menu Selection Here****";
cout << "n1. Push n2. Pop n3. Exit n";
cout << "select options here :";
cin >> input;
switch (input) {
case 1:
cout << "You have selected Push Stackn";
push_element();
break;
case 2:
//cout << "You have selected Pop Stackn";
//rem_element();
break;
default:
cout << "You have selected Invalid Options";    
break;
system("cls");
return;
}
}

这是输出

输出显示将元素向左推送

输出为 3 E R 4

我想显示这样的输出"4 r e 3">

感谢和问候,

如果你想以这种方式显示数组,你总是可以向后读取数组以进行输出。

for (int e = p_elements-1; e >= 0; --e) {
cout << array[e]<<" ";
}