1

I have to ask the user to put in an array size and then to ask the user to fill it out. When the user puts in a duplicate, the program should say "invalid" and the user is asked to replace the number. I am supposed to use traversing array search.

Like this example here:

Enter list size: 4
Enter value for index 0: 1
Enter value for index 1: 1
Invalid. Enter a new number: 2
Enter value for index 2: 5
Enter value for index 3: 6

This is my code so far:

#include <iostream>

using namespace std;

int main() {
  int size;
  cout << "Enter list size: ";
  cin >> size;
  int array1[size];

  for (int i = 0; i < size; i++) {
    cout << "Enter value for index " << i << ": ";
    cin >> array1[i];
    for (int j = i + 1; j < size; j++) {
      if (array1[i] == array1[j]) {
        cout << "Invalid! Enter a new value for index " << i << ": ";
        cin >> array1[i];
      }
    }
  }
  return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
adover
  • 21
  • 2
  • 1
    I assume you can't use a `std::set` – drescherjm Mar 14 '22 at 17:28
  • 3
    `for (int j = i + 1; j < size; j++){` your loop is looking for the duplicates in the elements that are not currently added yet, instead of looking only in the elements that are in the array. Think about where you should look for duplicates. – drescherjm Mar 14 '22 at 17:29
  • 4
    As C++ [doesn't have variable-length arrays](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) your code is invalid from the start. Please learn how to use `std::vector`. – Some programmer dude Mar 14 '22 at 17:31
  • `int array1[size];` -- That should be `std::vector array1(size);`. If you say "I can't use std::vector`, well, your code that you were using before, as pointed out, was invalid to begin with. – PaulMcKenzie Mar 14 '22 at 17:33
  • @fabian Evil. :) You named your vector array but then used vector later. – Goswin von Brederlow Mar 14 '22 at 18:01
  • `size_t size; std::cin >> size; std::vector array(size); for (auto insertPos = array.begin(); insertPos != array.end(); ++insertPos) { int input; std::cin >> input; while(std::find(array.begin(), insertPos, input) != insertPos) { std::cerr << "Duplicate value; enter another one\n"; std::cin >> input; } *insertPos = input; }` – fabian Mar 14 '22 at 18:03

2 Answers2

1

It does what was specified but the exercise probably was to write std::ranges::find.

#include <iostream>
#include <vector>
#include <cstddef>
#include <algorithm>

int main() {
    size_t size;
    std::cout << "Enter list size: ";
    std::cin >> size;

    std::vector<int> arr;
    arr.reserve(size);

    while(arr.size() < size) {
        int t;
        std::cout << "Enter value for index " << arr.size() + 1 << ": ";
        std::cin >> t;
        if (std::ranges::find(arr, t) == arr.end()) {
            arr.push_back(t);
        } else {
            std::cout << "Invalid! ";
        }
    }
}
Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
1

Try this approach, Every time user enter value helper function will check duplicate from already filled array

#include<iostream>

// Helper Function that will check duplicate from array
bool IsDuplicate (int arr[] ,const int idxSoFar, int element){
    for(int i =0 ; i < idxSoFar ; i += 1 )
        if( arr[i] == element){
          std::cout << "Invalid! Enter a new value for index "<< idxSoFar + 1 << " : ";     
          return  arr[i] == element;
        }
   return false;       
}

int main () {
   int size;
   std::cout << "Enter list size: ";
   std::cin >> size;
   int array1[size];

   for (int i = 0; i < size; i++) {
     std::cout << "Enter value for index " << i << ": ";
     do
       std::cin >> array1[i];
     while(IsDuplicate(array1 , i , array1[i]));
   }
   return 0;
}
zain ul din
  • 1
  • 1
  • 2
  • 23