Hello. I've got a homework assignment involving reading an unspecified number of numbers from a data file, storing it into a dynamically allocated array and doing various sorts and such(the output is basically a list of grades on some test that I do stuff like sort, average, give an accurate grade curve for, etc...that's not really important as I have that all figured out).

I guess I'm stuck in three parts:

1) Creating the array. If I'm not mistaken, would I can pass an array that's called and treat it like a pointer for a function, right? So something like this is kosher?

int grades[50];
sort_grades(grades, 50)
 /* rest of main */

void sort_grades(int *array, int numbers)
/*
function goes here */

I guess I'm just a little confused. I was sick for a week during some of this stuff for my class, so there's a big, gaping chasm of confusion going on with this stuff.

2) Assuming I have the dynamic array thing correct, or after correcting in that regard it does what my assignment is asking me to write, how exactly do I store an unspecified amount of data from a binary file in that array? It seems like it should be someting simple, but from the various things I've read in the book(terrible book) and online stuff, I can't seem to place my finger on it. I know there are hundreds of "advanced" ways to do this that are hilariously simple, but I'm a noob at programming so the simpler the actions, the better. I'm not necessarily asking for code to do it, but perhaps if you told me the general logic behind it I could write a little snippet following that logic and see if it would work?

3) Ideally, I don't want to lob all this stuff in the main function. It's a fairly long program, so naturally I have the work divided up into functions, and I'd like to keep it that way. If I write a function to write X amount of numbers into a .dat in binary(X being the size of the array that's established in main), close the file, open it back up and read a random number of numbers from that file, how do I take that number of numbers and use it as my array paramenter? In simpler form:

array of size Z in main --> call function to do file stuff with read/write --> write Z ints into data file(now in new function) --> read X** ints from data file ---> end function --> store X ints from the data file into array of size Z --> make the array now X size instead of Z size --> Me happy that program runs --> Me finally get some sleep

**X can be any number of ints up so long as it's not more than Z(which wouldn't make sense anyways...I think)

The last bit is my sad attempt at dry humor after a few too many hours on school work.

Ahem!

So yeah, those are my issues. Does my effort sound utterly hopeless, or is there some chance I may actually get this thing?

Looking forward to your response(s) and I'll be as johnny-on-the-spot with replying to you as I can about this.

[EDIT] Just to be clear, this is an entry-level programming thing. Although I'd love some quick solutions that are more advanced, I'm trying to figure out the "noob's way" to do this.

Is there going to be one number per line?

There many different ways to do it. I'll list two that come to mind.
One way is to read the data in just to count how many elements there are and then allocate an array that is that size. Then you read it in "for real" into that freshly allocated array.
The second way is to declare a fixed-sized array that's likely to be larger than what you need and read directly into that. That approach has the limitations that you may run short or end up wasting space.

The first approach can be accomplished by reading in the lines using fgets (assuming there's a fixed number of values per line) to get the initial count. Allocate your array using malloc. Then reread the data using fgets and then use sscanf to extract the numbers from the lines you read in. If you haven't been taught those things yet, you can probably get away with using fscanf.

For the second approach you'd just use the fgets/sscanf combo without doing the initial read.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.