0

The problem that I am working on right now is here, but I am of course not looking for the complete answer to this homework problem. All I am asking for is the steps in the final parts of the question. This is what I have so far:

int main()
{
    cout << "Please enter the number of guests attending your party: ";
    int k;
    cin >> k;
    cout << "Please enter the number of rounds of removal you'd like to perform: ";
    int m;
    cin >> m;
    for (int i = 1; i <= m; i++) {
        cout << "Please enter the multiple at which you'd like the removal to be at for round " << i << ": ";
            int r;
            cin >> r;
                if (k % r == 0) {
                    k - r;
                }
                cout << k << endl;
    }
    system("pause");
}

This is all so confusing to me, and I truly have no idea where to turn to get answers. It seems like I'd need an array to solve this, but arrays in C++ cannot be variable, and the array length would be k, a variable input. Any help would be very appreciated.

Owen Pauli
  • 27
  • 3
  • You need to make your question more explicit (what are "steps in the final parts of the question?"). Explain your problem better without relying on external links. – Ivan Aksamentov - Drop Jan 21 '16 at 04:47
  • "arrays in C++ cannot be variable" Variable size? Well, true and false at the same time. I think that before proceeding further to any C++ you should first work out some good book: [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Ivan Aksamentov - Drop Jan 21 '16 at 04:49
  • I know that the output is 1,3,7,9, if the input is 10,2,2,3, but I can't find a way to get to those numbers with that input. I usually end up with 10,10, or just a singular 7. I think that I have the initialization down pat, and now it's just formulas & loops. This is where I'm getting caught up. Also, to everyone responding telling me to use vectors or lists, this is homework for a classroom that didn't teach that. I don't think it would be accepted if I went beyond for/while loops, if statements, basic arrays, etc. – Owen Pauli Jan 21 '16 at 05:19

2 Answers2

1

I've read that question. you need a dynamic list like Linked list because you need to put and remove different items from different index so using arrays will be difficult.

Try to use std::vector or std::list, you can add or remove any any of list

#include <list>
std::list<int> mylist;

How can you add and remove values from list, check this link http://en.cppreference.com/w/cpp/container/list

For using your own Linklist, check this link How could i create a list in c++?

Community
  • 1
  • 1
Muhammad Zeeshan
  • 470
  • 7
  • 24
  • Do I **need** a dynamic list, or is it suggested? Considering that this is a homework problem for a class that never went beyond basic arrays, variables, for/while loops, if statements, and the like, I don't think it'd be appropriate to hand in work made with something beyond the curriculum. – Owen Pauli Jan 21 '16 at 05:20
0

According to your question an std::vector will be the best choice because it is a combination of an array & linked list in raw terms or we can simply say it's a dynamic array.

However as you mentioned n your comment that you haven't been taught anything other than basic arrays & want a solution within whatever you have learnt, then you have to take an array. Now the problem is that arrays are static & you cannot delete elements from it. So all you can do is to keep a counter that will take care of the number of elements in it.

// array insert and delete
#include <iostream>
using namespace std;

void see_array (int a[], int f, int b)
{
    for (int i=f; i<b; ++i)
    cout << a[i] << ' ';
    cout << '\n';
}

int get_pos (int a[], int f, int b, int num)
{
    for (int i=f; i<b; ++i)
    {
        if (a[i] == num)
        return i;
    }
    return -1;  // if search is unsuccessful
}

int main()
{
    int a[] = {1,2,3,4,5,6,7,8,9,10};
    int front = 0, back = 10; // as we have 10 elements

    // delete last element
    --back;
    see_array(a, front, back);

    // delete first element
    ++front;
    see_array(a, front, back);

    // delete element from middle say 6
    int pos = get_pos(a, front, back, 6);
    if (pos != -1)
    {
        for (int i = pos; i<back; ++i)  // shift elements towards left
        {
            a[i] = a[i+1];
        }
        --back;  // decrease size of the array
        see_array(a, front, back);
    }

    return 0;
}
/* Output :-
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
2 3 4 5 7 8 9
*/

I hope the above program is of some help to you !!! Good luck !!!

Ankit Acharya
  • 2,833
  • 3
  • 18
  • 29