Answer to Question #270703 in C++ for heayat

Question #270703

Given the following array of 8 integers {2,3,2,1,2,5,8,2} you are required to write a C++ program that will loop through it and re-arrange it into the following final output {5,3,1,8,2,2,2,2} and display it . The idea is to position all values equal to 2 in the array to come after the other values not equal to 2. The order of the other values i.e 5,1,3 and 8 does not matter as long as they appear positioned before all 2s as illustrated in the hint below: Initial array: {2,3,2,1,2,5,8,2} NumbertoMove = 2 Final Array: {5,3,1,8,2,2,2,2} Write an algorithm as a pseudocode to solve the problem above.


1
Expert's answer
2021-11-24T00:43:08-0500
#include<iostream>
using namespace std;
int main(){
int arr[]= 	{2,3,2,1,2,5,8,2};
int arr1[8];
int count = 0;
for(int i=0; i<8; i++){
	if(arr[i] != 2){
		
		arr1[count] = arr[i];
		count++;
	}
	
}


for(int i=4; i<8; i++){
arr1[i] = 2;
}


for(int i=0; i<8; i++){
	cout<<arr1[i]<<" ";
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS