how to fix this program?

It's solved:)
Last edited on
The first argument of memcpy is the destination, the second is the source, the third is the size.

Also, do you actually want to store -1 into the array or is it supposed to just be a quit marker?
its supposed to be a quit marker
closed account (D80DSL3A)
You're simulating the behavior of std::vector here.
It keeps track of both the size = number of elements used, and the capacity = the number of elements allocated. The max value of size = capacity.
I'd recommend using 2 variables to keep track of this. Trying to use just 1 (int i) may be making things harder.
Also, read the new value into a temp variable before copying it to the array, in case the new value = -1.

I'd approach more like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
    int cap = 5;// initial capacity
    int sz = 0;// initial size
    int* pArr = new int[cap];// allocate for capacity

    int n;/ the temp variable for reading into
   
    while( cin >> n && n != -1 )// read in the number and bail if n = -1
    {
        if( sz == cap )// array is full. Resize
        {
            cap *= 2;// double the capacity
            int* arrayPtr2 = new int[cap];// new array with double capacity
            // copy the existing sz elements from the old array to the new one
            // delete the old array
            // assign pArr to point to the new array
        }
        // good to go! Assign the new value and increment sz.
    }

    // other code to use/display the array contents
    // free the memory
}

Hope that helps.
thanks for the answer i just have a question. what am i assigning the new value to?
closed account (D80DSL3A)
Do you mean the assignment on line 19?
You assign n to the next element in the array:
1
2
pArr[sz] = n;// assign to next available element
++sz;// increment the size. 
Yes that is what I meant
Last edited on
closed account (D80DSL3A)
It's all good. I reply to threads because I enjoy it.
It's certainly more interesting than watching tv!

The blank screen may just be the program waiting for input at line 19.

Line 19 is intended to replace the input routine you have on lines 11-16, which actually won't work very well because it's throwing away every value for n except the last one.
So, just remove lines 11-16 from the code. The point of line 19 is that it allows any number of entries (not limited to 5) until a -1 is entered.

Your memcopy call should work, but 2 params are wrong.
How many ints are to be copied? 100? There are only sz ints in the array at that point.
Also, the copy source isn't new int, it's pArr (the array to be copied from).
Same re. line 27. It should be like line 48.

Otherwise, I think it looks good.
ok i changed everything you said to. thank you so much! it's perfect now. you explained it in a great way because even i could understand it quickly when usually it takes me a long time. have you considered teaching comp sci on the side? or tutoring? i think you'd be great at it. you saved my grade on this homework :)
closed account (D80DSL3A)
You're welcome for the help. Thank's for the complement.

I have done some tutoring, but I can't work as a teacher (though I may know enough to teach C++ at the beginner level) because I lack any formal CS education or certificates and I have no employment experience in the field. I'm actually just a lowly construction worker.
Alas, this shall remain just a hobby.
really?! i never would have guessed. well you seem to be really good at your hobby! i feel like college is overrated sometimes. you're clearly a great programmer despite lacking CS education. i sometimes struggle with homework like this for hours when i get stuck. if i get stuck again do you accept private messages on here? you're one of the best tutors on here in my opinion because when others explain i just don't get it. is it okay if the next time i'm struggling i private message you the program and the trouble i'm having?
closed account (D80DSL3A)
You may PM if you wish, though I prefer to field problems in the forums.
Most of the regulars here know far more than I do overall, and a fair fraction are actual professionals.
Given your low post count, you probably haven't much feel yet for the replies you'll get here.
They do vary somewhat though, for sure.

Best of luck with your course.
Topic archived. No new replies allowed.