Question

In C++, "remove" elements in an array that are repeated (including those that are negative and...

In C++, "remove" elements in an array that are repeated (including those that are negative and vice versa i.e. 1 = -1 and -3 = 3) while keeping the original.

ex: original array = {1,2,-3,4,5,3, -1, 3, 5, 2,6,4) final array = {1,2,-3,4,5,6)

As a hint, To "remove" the array, it's something with array[j] = array[j+1] and decrement the length but I'm having trouble

thanks! (Can't use vectors, maps, sets, can only use for loops, if statements, and stuff like that)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

using namespace std;

int main()
{
//array declaration and initialization
   int originalArray[] = {1,2,-3,4,5, 3, -1, 3, 5, 2,6,4};
  
   //calculate the size of array
   int size = sizeof(originalArray)/sizeof(originalArray[0]);
  
   //display the original array
   cout<<"The original array is: "<<endl;
   for(int i=0;i<size;++i)
   {
       cout<<originalArray[i]<<" ";
   }
  
   //remove the duplicate elements
   for(int i=0;i<size;i++)
   {
       for(int j=i+1; j<size; j++)
       {
           if(originalArray[i]==originalArray[j] || originalArray[i]==-originalArray[j])
           {
               for(int k=j;k<size-1;++k)
               {
                   originalArray[k]=originalArray[k+1];
               }
               --size;
               j--;
           }
       }
   }
  
   //display the resultant array
   cout<<endl<<endl<<"The array after removing the duplicate element is: "<<endl;
   for(int i=0;i<size;++i)
   {
       cout<<originalArray[i]<<" ";
   }

   return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
In C++, "remove" elements in an array that are repeated (including those that are negative and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C programming only please 5.2.3: Printing array elements with a for loop. Write a for loop...

    C programming only please 5.2.3: Printing array elements with a for loop. Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then backwards. End each loop with a newline. Ex: If courseGrades = {7, 9, 11, 10}, print: 7 9 11 10 10 11 9 7 Hint: Use two for loops. Second loop starts with i = NUM_VALS - 1. (Notes) Note: These activities may test code with...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • AQueue.java class AQueue implements Queue { private E queueArray[]; // Array holding queue elements private static...

    AQueue.java class AQueue implements Queue { private E queueArray[]; // Array holding queue elements private static final int DEFAULT_SIZE = 10; private int maxSize; // Maximum size of queue private int front; // Index of front element private int rear; // Index of rear element // Constructors @SuppressWarnings("unchecked") // Generic array allocation AQueue(int size) { //BUG #1: maxSize = size maxSize = size+1; // One extra space is allocated rear = 0; front = 1; queueArray = (E[])new Object[maxSize]; //...

  • Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names,...

    Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names, and read in course names from an input file. Then do the output to show each course name that was read in. See Chapter 8 section on "C-Strings", and the section "Arrays of Strings and C-strings", which gives an example related to this lab. Declare the array for course names as a 2-D char array: char courseNames[10] [ 50]; -each row will be a...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • c++ can use if else, loop,function ,array,file i.o. Can not use swtich, global variables etc..   Please...

    c++ can use if else, loop,function ,array,file i.o. Can not use swtich, global variables etc..   Please do all parts. previously I post 3 time for part1 ,2 and 3 but I can't make it into one complete program code. I even try to post a questions ask and post the 3 parts of code for experts to put it together. But experts said it's not enough information. please help... Thank you ! Program Objectives: Allow students to create arrays Allow...

  • you will analyse two algorithms for finding the median of an array of integers. You will...

    you will analyse two algorithms for finding the median of an array of integers. You will compare both algorithms in terms of timing, and hopefully design a hybrid algorithm that uses both, depending on input size. You will write a report describing your experiments and results. the following Java program implements two algorithms for finding the median of an array of integers. The first uses merge sort, and the other implements the recursive linear time selection algorithm, the task is...

  • C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string usi...

    C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string using the RSA algorithm (C90 language only). It can contain ONLY the following libraries: stdio, stdlib, math and string. I want to use the following function prototypes to try and implement things: /*Check whether a given number is prime or not*/ int checkPrime(int n) /*to find gcd*/ int gcd(int a, int h) void Encrypt(); void Decrypt(); int getPublicKeys(); int getPrivateKeys();...

  • C++ Programz

    Program 0 (50%): In many computer science courses, when you study sorting, you also study “runtime” - and it can be confusing to understand when you first see it.  For example, they say that BubbleSort “runs in O(n2)”  time (pronounced “Big-Oh of n-squared") - but what does that mean?  For simplicity, it means if you have n elements, the worst it could run would be n2 low-level computer operations (like comparisons, assignment statements and so on).  For example, if we...

  • This is a C++ assignment that I'm trying to create and would like some help understanding...

    This is a C++ assignment that I'm trying to create and would like some help understanding while loops, with possible integration of for loops and if statements. This program only uses while, for, and if. I have some code that I have started, but where to go from there is what's giving me some trouble. This is involves a sentinel controlled while loop, and there are a lot of specifications below that I must have in the program. The program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT