Home > Software engineering >  Program to output repeated integers from user-inputted group in ascending order is not printing cont
Program to output repeated integers from user-inputted group in ascending order is not printing cont

Time:09-10

I have been tasked with writing a simple program that takes in two groups of integers as user input, with the size of those groups being inputted by the user as well, then prints all integers that appear more than once in ascending order. For example, an input of

5
7
20
8
7
15
3
8
14
7

would result in an output of

Answer:7 8

To accomplish this task, I elected to use an array of arbitrary size that is filled via user input, then sorted with a selection sort function before the unique elements are printed using a nested for loop.

I cannot verify the functionality of these elements at the moment, as the program has been ending prematurely upon inputting a value for size2 and none of the array contents entered up that point are printed, even when I placed a for loop to print all contents of the array following the first group of integers.

I did not face this problem during earlier tests, so I imagine that I made a minor mistake, but it is one that I have not been able to identify.

#include <iostream>
using namespace std;

void swap (int *num1, int *num2) {
  int temp = *num1;
  *num1 = *num2;
  *num2 = temp;
}

void sortArray (int arr[], int size) {
  int i, j, min;
  for (i = 0; i < size - 1; i  ) {     
    min = i;
    for (j = i   1; j < size; j  ) {
      if (arr[j] < arr[min]) {
        min = j;
      }
    }
    if (min != i) {
      swap(&arr[min], &arr[i]);
    } 
  }
}

int main() {
  int arr[100], size1, size2, size3 = size1   size2;
  
  cin >> size1;
  for (int i = 0; i < size1; i  ) {
    cin >> arr[i];
  }
  
  cin >> size2;
  for (int i = size1; i < size3; i  ) {
    cin >> arr[i];
  }
  
  sortArray(arr, size3);
  
  cout << "Answer:";
  
  int visited[size3];
  for (int i = 0; i < size3; i  ) {
    if (visited[i] != 1) {
      int count = 1;
      for (int j = i   1; j < size3; j  ) {
        if (arr[i] == arr[j]) {
          count  ;
           visited[j] = 1;
        }
      }
      if (count != 1) {
        cout << arr[i] << " ";
      }
    }
  }
   
  return 0;
}

CodePudding user response:

It was n't a valid program to do the tests. I slightly modified it. I used vector for array. However your algorithm is correct. Now it works ok. Please check the source and try it in your compiler.

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

using namespace std;

void swap(int *num1, int *num2) {
    int temp = *num1;
    *num1 = *num2;
    *num2 = temp;
}

void sortArray(int arr[], int size) {
    int i, j, min;
    for (i = 0; i < size - 1; i  ) {
        min = i;
        for (j = i   1; j < size; j  ) {
            if (arr[j] < arr[min]) {
                min = j;
            }
        }
        if (min != i) {
            swap(&arr[min], &arr[i]);
        }
    }
}

void sortVector(vector<int> arr) {
    int i, j, min;
    int size = arr.size();
    for (i = 0; i < size - 1; i  ) {
        min = i;
        for (j = i   1; j < size; j  ) {
            if (arr[j] < arr[min]) {
                min = j;
            }
        }
        if (min != i) {
            swap(&arr[min], &arr[i]);
        }
    }
}


int main() {
    vector<int> arr;
    int  data;
    int size1, size2, size3;
    cin >> size1;
    for (int i = 0; i < size1; i  ) {
        cin >> data;
        arr.push_back(data);
    }

    cin >> size2;
    for (int i = 0; i < size2; i  ) {
        cin >> data;
        arr.push_back(data);
    }

    size3 = arr.size();
    sortVector(arr); //your can use sort(arr.begin(), arr.end()); here

    

    cout << "Answer:";

    vector<int> visited;
    visited.reserve(size3);
    for (int i = 0; i < size3; i  ){
        visited.push_back(0);
    }
    
    

    for (int i = 0; i < size3; i  ) {
        if (visited[i] != 1) {
            int count = 1;
            for (int j = i   1; j < size3; j  ) {
                if (arr[i] == arr[j]) {
                    count  ;
                    visited[j] = 1;
                }
            }
            if (count != 1) {
                cout << arr[i] << " ";
            }
        }
    }

    return 0;
}
  • Related