Hey guys, I need some help here. I think most of the required code is finished, but I'm struggling to actually set my input values equal to the memory address of said array index. I'm thinking my errors are conversion from pointer to int(throws an error without a double equals operator) and possibly the fact that by looking at my code, the FOR LOOP seemingly exits and causes an error after the SIZE variable is hit the first time. Help is much appreciated!
//Objective: Create an array of numbers based upon user input.
//
// Program logic :
// Ask the user for how big to initially size the array. CHECK
// Create an array based upon that size. CHECK
// Ask for a number, insert that number into the next unused place in the array.
// Repeat inserting and resizing the array as needed or until they enter -1 for the number.
// Print the list.
#include <iostream>
usingnamespace std;
int main() {
int size, num = 0, i = 1;
int *arr;
cout << "Please enter your desired array size: "<< endl;
cin >> size;
arr = newint[size];
delete[] arr;
cout << "Please enter your array index values: " << endl;
while (num != -1) {
for (i; i <= size; i++) {
cin >> num;
arr[i] == num;
if (i == size) { //Increasing the array size.
int x;
cout << "Your array has stored its maximum amount of values. Current array size: " << size << endl;
cout << "If you would like to exit the program, please enter 0. " << endl;
cout << "Otherwise, If you would like to increase your array size, " <<
"please enter a number other than 0 to add to your current array size: " << endl;
cin >> x;
if (x == 0) {
exit(0);
}
else {
arr = newint[size + x];
delete[] arr;
cout << "The size of your array is now: " << size + x << endl;
cout << "Please enter the next indexed value. This value will be stored in the " << size + 1 << "th index." << endl;
cin >> num;
arr[i] == num;
}
}
}
}
system("pause");
return 0;
}
Setting arr[i] == num; for some reason allowed me to run through to my other code. There are only two spots I'm running into errors and that's initializing array indexed values and continuing the loop after size is hit... :/
I don't think you want lines 27-31. The point of an expanding array is that it should expand automatically. In terms of how much to add, make it easy and just double the current amount.
When you resize the array, you need to keep track of both the old array and the new one while you copy the existing values from the old to the new. Then you can delete the old one.
It looks like you've put pieces of the algorithm together but you've lost sight of how they all fit. To avoid this, try writing the algorithm out in English first. Work it over a few times until it seems right. Then convert it to code. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
prompt for the array size
initialize arr to "size"whiletrue {
Prompt fornew value
if value == -1 then break out of loop
if array is full then {
make newArray that is twice as big as arr
copy all items from arr to newArray
delete[] arr
arr = newArray
change size and capacity (how??)
}
// When you get here, you're guaranteed that there is enough size
arr[size] = new value
++ size;
}
Note line 11. Here I'm using "size" to indicate how many items the user has put into the array, and I'm using "capacity" to mean now many items the array is capable of holding before you have to grow it again. These are TWO different concepts but you're using one variable (size) to represent them both. You need two. So add those to the algorithm and then go back over it, paying attention to whether each "size thing" refers to the size or the capacity.
My problem now lies with the conversion from pointer to int. Any ideas?
@dhayden:
Thanks for giving me the hint as to how to increase size in an easier and more manageable fashion. Question though: Why do I need a size and capacity variable? Isn't the capacity just the size of the array to begin with?
Isn't the capacity just the size of the array to begin with?
Yes, but you also need to store the number of elements in the array. You don't know how many items will go into it since the user can enter -1 at any time to stop input. Right now you exit the program when they stop entering values. You still need to print the values out.
So, you're recommending that I set the maximum size of the array equal to a variable called 'capacity' and set the amount of numbers input to a variable called 'size?'
Thus, when the user inputs -1, the program will exit and print the actual amount of numbers input, regardless of capacity?
In my case, can't I just print out 'i' because it's incrementing from 0 until the user inputs -1?
int main() {
int size, num = 0, i = 0;
int *arr;
cout << "Please enter your desired array size: "<< endl;
cin >> size;
arr = newint[size];
delete[] arr;
cout << "Please enter your array index values: " << endl;
while (num != -1) {
for (i; i <= size; i++) {
cin >> num;
arr[i] = num;
if (i == size) {
++size; //Increasing the array size.
arr = newint[size];
delete[] arr;
cout << "The size of your array is now: " << size << endl;
}
cout << "Please enter the next indexed value. This value will be stored in the " << size << "th index." << endl;
}
}
system("pause");
return 0;
}
Sets the array size equal to a new integer size and deletes the old size stored in the heap's memory address to free up space for the new one, if I'm not mistaken.
I'm totally lost with what you guys are saying... Here's my code now:
Sets the array size equal to a new integer size and deletes the old size stored in the heap's memory address to free up space for the new one, if I'm not mistaken.
No, you are mistaken:
TheIdeasMan wrote:
1. Makes a batch of biscuit mixture.
2. Throws mixture straight in the rubbish bin.
You are immediately deleting the thing you just created.
Use a different variable name for each of the two things.
I also got rid of the 2nd arr memory dump like you implied with your previous comment,
No, I meant get rid of both of them. Actually I would start again with a file with dhayden's pseudo-code in it, nothing else.
Look at the pseudo-code dhayden did for you here :
Be careful with the copy, you have to copy all the values not the pointers (a deep copy), before you delete the original array. Otherwise a segmentation fault.
So, can you give me a code example of garbage collection that would work for initializing my array to the size variable?
I'm gathering that I don't need delete[] arr; unless I've already used the arr = newint[size]; code snippet?
For example:
1 2 3 4
arr = newint[size];
pseudo-code
arr = newint[size[;
delete[] arr; // Because this is the 2nd time I'm using it and I need this to overwrite the first time?
I just created a new program and here's what I have:
Right, now I am wondering if you are messing with me/us. It has been explained how things work, but I get the impression you continue to post the same sort of non-sense you had at the start. Sure you changed your code, but now it is just distilled the nonsense into something smaller.
Maybe that's not the case - you just don't get it yet? If so, then I suggest you read up about all this. It's sometimes hard for me to tell which way things are going: Lack of understanding, or trolling.
Sorry I don't have much patience for this, I would rather not spend an entire afternoon wondering whether I am wasting my time.
I assure you I'm not trolling. This assignment is due in an hour and a half...
Now my only errors that are visible to me are:
Error when outputting the array indexes and for some reason, when my size variable hits 20 (Initial size is 5), the next variable that is input is rejected and my program runs into a breakpoint...
UPDATE:
I think I solved it. I got it all to print properly now. I realized that I was printing the 'x' variable after size doubled, so that's why I was getting the output errors.
int main() {
int size, num = 0, inc = 0;
int *arr;
cout << "Please enter your desired array size: " << endl;
cin >> size;
arr = newint[size];
cout << "Please enter your array index values: " << endl;
while (num != -1) {
cin >> num;
if (num == -1) {
int x = 0;
for (x; x < inc; x++) {
cout << "The value stored at index " << x << " is: " << arr[x] << endl;
}
}
else {
arr[inc] = num;
inc++;
if (inc == size) {
size += size;
cout << "The size of your array is now: " << size << endl;
}
}
}
system("pause");
return 0;
}
I assure you I'm not trolling. This assignment is due in an hour and a half..
.
Can you explain how you repeatedly post something obviously wrong, have it explained twice in very simple terms why it is wrong, then still maintain that is how it should be done? Then post some more code with UB that completely misses the point of the supposed assignment?
I think I solved it.
Well submit that as the supposed assignment then .... Good Luck !!
It was obviously wrong to me, but I didn't understand why. You were half-assing your help and started to insult me... I haven't had much experience actually coding and don't appreciate your attitude.