Overloading a function

I'm stuck on a C++ assignment and would like a nudge in the right direction. The assignment: http://www.c-jump.com/CIS62/hw1.htm
I'm stuck on number 3:
Overload push() to take in an array of integers and push them onto the stack:
1
2
3
4
5
6
class IntStack
{
    //...
    void push( int a[], size_t array_size );
    //...
};


Push a[0] first and then a[1] and so forth until array_size.

I'm not sure how to implement this function. the size_t array_size parameter is throwing me for a loop (figuratively).


the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <stdlib.h>
using namespace std;

class IntStack{
    static const int INITIAL_SIZE = 100;
    int i;
    int st_arr[INITIAL_SIZE];
public:
    void reset();
    void push(int n);
    void push( int a[], size_t array_size );
    int pop();
    void pop( int a[], size_t n );
    void test();
};
int main()
{
    IntStack stack;
    stack.reset();
    stack.push(5);
    stack.test();
    stack.pop();
    stack.test();
    //stack.pop(arr, 5);
    return 0;
}
void IntStack::test(){
    for (int i = 0; i < 100; i++){
        cout << st_arr[i] << " ";
    }
    cout << endl << endl;
}
void IntStack::reset(){
    //loop through the stack and set each element to NULL
    for (i = 0; i < INITIAL_SIZE; i++){
        st_arr[i] = NULL;
    }
    i = 0;
}

void IntStack::push(int n){
    //if stack is full, int cannot be pushed, signal error
    if (i == 100){
        cerr << "error: stack is full, cannot push another int";
        exit(1);
    }
    //set current element of stack to pushed int then increment index
    st_arr[i] = n;
    i++;
}

void IntStack::push( int a[], size_t array_size ){

}

int IntStack::pop(){
    //if stack is empty, int cannot be deleted, signal error
    if (i == 0){
        cerr << "error: stack is empty, cannot delete element";
        exit(1);
    }
    //decrement to previous stack index then set it to NULL
    --i;
    st_arr[i] = NULL;
}

//void Intstack::pop( int a[], size_t n ){

//} 


Any help would be greatly appreciated. I'd prefer hints instead of a full out solution.
I'm not sure how to implement this function. the size_t array_size parameter is throwing me for a loop (figuratively).


You should use a loop (literally).

array_size is just the number of elements in the passed array 'a'. So use a for loop and just push each element in the array the same way you'd push a singular element. You should even be able to call your existing push function -- just call it once for each element in the array.
Right under my nose... Thanks. One more problem.
I'm not sure where to start to implement the print_stack function. The specifications:
void print_stack( IntStack* pstack );

which will print the integers on the stack to cout. Print each integer on a seperate line. Make it a friend of IntStack class, so it can access the stack without popping any elements.


The pointer is what's throwing me off. I'm not sure how to pass stack to the print_stack function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <stdlib.h>
using namespace std;

class IntStack{
    static const int INITIAL_SIZE = 100;
    int i;
    int st_arr[INITIAL_SIZE];
public:
    void reset();
    void push(int n);
    void push(int a[], size_t array_size);
    int pop();
    void pop(int a[], size_t n);
    //void test();
    bool is_full();
    bool is_empty();
};

void print_stack(IntStack* pstack){

}

int main()
{
    int arr[5] = {1,2,3,4,5};
    IntStack stack;
    stack.reset();
    stack.push(9);
    //stack.test();
    stack.pop();
    //stack.test();
    stack.push(arr, 5);
    //stack.test();
    stack.pop(arr, 5);
    //stack.test();
    return 0;
}
/*
void IntStack::test(){
    for (int i = 0; i < 100; i++){
        cout << st_arr[i] << " ";
    }
    cout << endl << "i = " << i << " st_arr[i] = " << st_arr[i] << endl << endl;
}
*/
void IntStack::reset(){
    //loop through the stack and set each element to NULL
    for (i = 0; i < INITIAL_SIZE; i++){
        st_arr[i] = NULL;
    }
    i = 0;
}

void IntStack::push(int n){
    //if stack is full, int cannot be pushed, signal error
    if (i == 100){
        cerr << "error: stack is full, cannot push another int";
        exit(1);
    }
    //set current element of stack to pushed int then increment index
    st_arr[i] = n;
    i++;
}

void IntStack::push( int arr[], size_t array_size ){
    for (int idx = 0; idx < array_size; idx++){
        push(arr[idx]);
    }
}

int IntStack::pop(){
    //if stack is empty, int cannot be set to null, signal error
    if (i == 0){
        cerr << "error: stack is empty, cannot delete element";
        exit(1);
    }
    //decrement to previous stack index then set it to NULL
    --i;
    st_arr[i] = NULL;
}

void IntStack::pop( int a[], size_t n ){
    i--;
    for (int idx = 0; idx < n; idx++){
            st_arr[i] = a[idx];
            i--;
    }
    for (i = 0; st_arr[i] != NULL; i++){} //figure out how to make this line unnecessary
}

bool IntStack::is_full(){
    if(i == 100)
        return true;
    else
        return false;
}

bool IntStack::is_empty(){
    if(i != 0)
        return false;
    else
        return true;
}
Topic archived. No new replies allowed.