working with files and arrays

April 18, 2011 at 19:46:04
Specs: Windows 7
for c++

This lab suse the file number.txt

step 1
creat an array of 20 interger storage spots. (for example, int array[20];) open the file for reading. read the first 20 intergers from the file into the 20 array spots, 0-19. You can use a for loop fo rthis, since you are counting a fixed number of times through the loop and you are not reading until the end of the file.

write another separate loop that finds the larges of the 20 numbers in the array. After you've found the larges number, put the larges number into the array[19] AND put what was in array[19] into the array location where you found the largest.

(this is just the begining and would like any help i can get on how to do these 2 parts?) plz help!

See More: working with files and arrays

Report •

April 18, 2011 at 21:10:35
if the data file is plain text


open file for reading
loop in file while file has data to read and array index < 20
store data in array and increment index


use a couple of variables - perhaps largest and cur_index
loop through your array
check current value against largest; update largest and cur_index (if test)
after loop termination, swap the data as required

maybe something like that... give it a whirl; the more effort you put into it, the better off you'll be. good luck.

Report •

April 18, 2011 at 22:26:26
i need help with the starting structure like how you call the files and write back to the array. thanks for the reply but you basically re worded my question

Report •

April 18, 2011 at 23:37:25
If you don't understand those basic operations then you need to talk to your teacher.

Report •

Related Solutions

April 18, 2011 at 23:45:48
#include <fstream>
.... other bits of code

std::ifstream ifs_obj("number.txt");
int array[20] = { 0 }, idx = 0;

... check if the file was opened

while(idx < 20 && (ifs_obj >> array[idx])) { ++idx; }

maybe something like that...

[edit]oops just landed in the doghouse again[/edit]

Report •

April 25, 2011 at 16:31:23
who said anything about a teacher?

Report •

Ask Question