Reading a file of unknown length using dynamic arrays etc.

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Reading a file of unknown length using dynamic arrays etc.

Postby Guest » Mon Oct 06, 2003 12:36 pm

This is the link to my homework.
http://cis.poly.edu/cs1124/Homework/hw02/hw02.htm
I am having trouble understanding how I would do this. My friend leaned only vectors and knows somewhat how to do this in vector but I'm very lost as to how to do this with arrays. I am only allowed to use arrays. Can anyone assist me with this? Thanks.
Guest
 

Postby user125 » Mon Oct 06, 2003 12:38 pm

Sorry, username thing didn't work. Please note that I'm not asking anyone to solve this for me, just if they can help me understand how to start this thing, or how to go about doing this.
user125
 
Posts: 3
Joined: Mon Oct 06, 2003 12:33 pm

Postby Alvaro » Mon Oct 06, 2003 2:20 pm

Read the documentation on realloc(). That, the description of the assignment and hard thinking should get you started.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Postby user125 » Mon Oct 06, 2003 2:24 pm

we can't use that function.
user125
 
Posts: 3
Joined: Mon Oct 06, 2003 12:33 pm

Postby Wizard » Mon Oct 06, 2003 2:42 pm

Are you familiar with pointers as arrays?
ie,
int *myAr = new int[50];

if, later on, you need a larger array
int *newAr = new int[100];
// copy myAr[i] to newAr[i] for all i in myAr
delete [] myAr;
myAr = newAr;

Then your array gets a size of 100. Are you allowed to do this?
User avatar
Wizard
Site Admin
 
Posts: 3226
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA

making sure

Postby user125 » Mon Oct 06, 2003 2:52 pm

hey thanx for the response i appreciate it!

i can't assign a global / local constant so i don't know if i can use your commands, but then again i am a newbie when it comes to C++ and your code may just be the one i need

i did ask my professor for some tips and he said what i had to do was copy the array and increase the size of the array by a certain number allowing space for more information....
will this command do that?? because i do know using the new command is vital to this program...... i just don't know how to use it in the right way......

but either way thanx for the input
user125
 
Posts: 3
Joined: Mon Oct 06, 2003 12:33 pm

Postby Alvaro » Tue Oct 07, 2003 9:00 am

user125 wrote:we can't use that function.

I didn't say that you should use realloc(). I said that you should read the documentation for it. It explains how a new block of memory is allocated if needed and the content is copied.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Re: making sure

Postby Wizard » Tue Oct 07, 2003 9:15 am

user125 wrote:hey thanx for the response i appreciate it!

i can't assign a global / local constant so i don't know if i can use your
<snip>
Dwuh? What else is there besides global and local constants?

Anywho, you don't need a constant, you need a variable that tracks the size of your array. It can be either global or local, it's up to you.
You should have no problem using the code that I posted earlier, if you take the time to understand it. It looks to me like that's exactly what your teach wants you to do.
User avatar
Wizard
Site Admin
 
Posts: 3226
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA


Return to Homeworks

Who is online

Users browsing this forum: No registered users and 2 guests