Here is what I have to do.

1)Read integers from a file
2)If it cannot be opened, display an error message and exit the program.
3) Otherwise, read an integer from the file (there is guaranteed to be at least 1).
4) If that count is 0, display an error message and exit the program.
5) Otherwise, allocate an array of integers via dynamic memory, of the size that was just read. So, if the first value in the file is 3, you should allocate a section of dynamic memory with room for 3 integers.
6) Display the number of values to be read, along with a meaningful message, i.e. “Reading 3 values from input file”.
7) Read in that many values from the file, storing them in the dynamic memory; remember that it can be treated as an array! There is no need to use a while loop with >> as the condition here: I have guaranteed that the file will contain as many other values as the first value says. Thus, you can use a for loop to read that many values. You could use a while loop also, it is up to you. In fact, you can use one of the functions we’ve used previously, that reads an array of values from a file: remember, arrays are pointers, and pointers are arrays!
8) Once the values have been read in, pass it to a sort function. You can use either Bubble or Selection sort,just make sure that you use a version which sorts arrays of int. Note that you DO NOT have to modify the sort at all, since arrays are pointers, and pointers are arrays! You must call the sort function from within main(), and pass it the dynamic memory pointer, and the number of items read from the file.
9) After the sort is complete, display the values in the dynamic memory.
10) Before exiting, remember to delete the dynamic memory that you allocated, and be sure to use the correct syntax for the deletion!

If someone can please give me an example on how this is done so I can use it to refrence to do thi sproject or show me how to do it. It would be a huge help!!!!

Thanks!

Recommended Answers

All 10 Replies

>If someone can please give me an example on how this is done so I
>can use it to refrence to do thi sproject or show me how to do it.

Reference, my ass. Go do your own homework, slacker.

I actually have it all complete accept I just cant figure out how to use the first number in the file as how many integers there will be in the array. The rest is easy. And yes a refrence that is what this site is for moron! I just put the direction on what I need done up so that noone would get confused in what it needs to do. Far from a slacker its called using resources, my professor would be able to easily search this and compare to what I hand in, LOL hes not that stupid!

commented: Way to decend into insults. +0

I have looked and still cannot find that answer a couple good examples but none that show how I can read the first integer and allow it to create the array size.

Secondly she insulted me first and accused me of trying to cheat. I really dont care who she is lol just looking for some help.

>I actually have it all complete
You failed to post code, posted a list of requirements, and asked for code. You didn't ask a specific question either. What do you expect us to think? You still haven't proven that you've done any work, so I still believe that you haven't. It's quite simple.

>I just cant figure out how to use the first number in the file as how many integers there will be in the array.
Your requirements say to allocate an array using dynamic memory. I suspect you fell asleep during the class where malloc was explained. :icon_rolleyes:

int *array;
int size;

fscanf(file, "%d", &size);
array = malloc(size * sizeof *array);

/* Taadaa! */
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int j;
    char *file = "ex2-input.txt";
    ifstream txt(file);

    if (!txt) {
        cout << "Error"<< file << endl;
        return 0;
    }
    cout << file << endl;
    while (txt >> j) {
        cout << j << endl;
    }
	system( " pause" );
    return 0;
	
}

This will obviously simply ready and print from the file. Sorting it is no problem that is just another function. I do not know how to implement that code to start the memory allocation after compiling, this is the part I am having trouble with.

Well, since you're using C++ and not C, new[] would be the better option (as you don't have to remember to include <stdlib.h>:

int *array;
int size;

txt>> size;
array = new int[size];

Otherwise my first answer remains the same.

I mean whouldent this line look more like this?

sscanf(line,"??", &array);	
a[i] = array;

The ?? would be whatever needs to go there to read the first integer in the first line correct?

>I mean whouldent this line look more like this?
No, that's just a mishmash of ideas that don't make sense. Here are your steps to populate the array:

  1. Open the file stream (verify success)
  2. Read one integer from the file stream into N
  3. Allocate a dynamic array with N as the size
  4. Read N integers from the file stream into the new array

OK I am getting a bunch of weird numbers with this now but this is where I have gotten.

#include <iostream>
#include <fstream>

using namespace std;


int main()
{
    int j;
	int h;
	int b;
	int *array;
	int size; 
	int firstnum = 0;
	char *file = "ex2-input.txt";
    
	ifstream txt(file);

    if (!txt) {
        cout << "Error, File Not found"<< file << endl;
        return 0;
    }
    cout << file << endl;



    while (txt >> j && firstnum == 0 ) 
	{

		firstnum = j;
		break;
       // cout << j << endl;
	
    }
	
	size = firstnum;
	array = new int [size];
	cout << "\n" << firstnum << "\n";

	while (txt >> h) 
	{
		b = size++; 
		h = array[b];
		cout << h << endl;
	
    }
	


	system( " pause" );
    return 0;
	
}
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.