please help me understand

so i have to write a program that will do either a bubble sort or a selection sort on a previous program that contained a file that roman numerals and their integer values in the following format:
I 1
IV 4
IX 9
etc.

now we dont know how many roman numeral will be in the file so i cant really create an array, but he said something about dynamic array being able to help us out. i tried reading the tutorial on dynamic array and how it would help my situation out but i just ended up getting lost. im hoping maybe someone on here can help explain it in a way i can understand that will help me out with my program.
Ask if you are allowed to use std::vector - it does exactly what you want. Dynamic arrays are tedious to deal with and easy to mess up - a vector will do it for you and is really easy to work with.

http://www.cplusplus.com/reference/vector/vector/
http://en.cppreference.com/w/cpp/container/vector
Last edited on
LB is on the money here if you're working in c++. However in C there are no vectors.

IF you are working only in C (and I stress if, because as LB states it's very easy to make a mistake) then you need to know a few things.

Memory has different aspects, but in this regard we will only talk about the stack and the heap. The stack is where the program reserves all the memory needed for it's local variables and stuff.

The size that needs to be reserved in the stack is calculated at compile time, and not executable time. This means you cannot have dynamic variables in the stack.

Don't worry the world isn't ending. If you're not capping out on memory, then you still have the heap!

The heap is basically all the unallocated data space, and there's no regulation to the allocation and deallocation of blocks from it. This means it is prime for dynamic allocation!

How do you access the heap?

1
2
3
4
int x* = new int//this is used to create an integer pointer to a heap memory block. Let's say
                       //You're running a typical system and an int is 4 bytes. You've just allocated 4 bytes of 
                       //memory in the heap.
delete x; //this is used to delete the all the memory in a heap allocation. 


You MUST delete any data you create in the heap, because it will not be deleted until you tell the computer to delete it. This means that even if you leave the scope of the heap variable, the data will still be there in the heap.

Now how would you allocate an array?

1
2
3
4
int x;
cin >> x;
int *y = new int [x];
delete y[];


Before testing any of this, read these :
http://www.cplusplus.com/forum/general/111693/
http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/

DO NOTE, I haven't worked with stack/heap in a while, while confident... I'm not entirely sure that the array delete in the 2nd code reference will work safely.
Last edited on
I'm not entirely sure that the array delete in the 2nd code reference will work safely.

Yeah one small thing: Should be
delete[] y;
Last edited on
i still dont understand how this helps me. if im reading from a file and my code is some like:
int*y = new int[x]; how do i know what to put in the bracket if i want it to say be the size of my whole file if i dont know how many lines it is going to contain ahead of time.
closed account (48T7M4Gy)
You could always run through the file and count the number of entries first?
this is the code i used to try and read the number of lines in a file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	int lines = 0;
	string numbers;
	ifstream files("read.txt");
	while (!files.eof())
	{
		getline(files, numbers);
		system("pause");
		cout << numbers << endl;
		++ lines;

	}

	cout << lines<<endl;
	return 0;
}


my text file has this in it:
5
4
3
2
1

at first it was telling me it had 6 lines and now it is saying it has 7. im not sure what is wrong
Last edited on
closed account (48T7M4Gy)
Check the file and make sure you haven't got a blank line at the end. That will explain 7 in this case.
Topic archived. No new replies allowed.