Menghapus Elemen
Menghapus Elemen Array :#include<iostream>
#include<conio.h>
#include <windows.h>
using namespace std;
int main()
{
int a[50];
int n, hapus;
cout<<"Masukkan banyaknya data :"; cin>>n;
for(int i=1; i<=n; i++){
cout<<"Data ke-"<<i<<" : "; cin>>a[i];
}
cout<<"Data yang lama : \n";
for(int i=1; i<=n; i++){
cout<<a[i]<<",";
}
cout<<endl;
cout<<"Data yang ingin dihapus : ";
cin>>hapus;
cout<<"\n Data yang baru : \n";
cout<<endl;
for(int i=1; i<=n; i++){
if(a[i] == hapus){
a[i]= a[i+1];
for(int j=i+1; j<=n; j++){
a[j]=a[j+1];
}
}
else{
}
}
for(int i=1; i<n; i++){
cout<<a[i]<<",";
}
getch();
}
outputnya :
0 komentar:
Posting Komentar