Returning a value from function

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1443 Views - Last Post: 17 October 2007 - 06:21 PM Rate Topic: -----

#1 pacit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-October 07

Returning a value from function

Post icon  Posted 16 October 2007 - 12:24 AM

Hey, I'm writing this program to read in various files, sort the data, output the sorted data to a output file and keep track of how many times a line in the functions were executed. Basically, the output files (which should be 16 after the program ends) should contain the count for the specific line of the algorithm I was asked to keep track of and the contents of the array after having been sorted. The sorting functions are working correctly, now I'm trying to get my counter working right. My functions (bubble sort and insertion sort) were voids but I changed them to int so they could have a return value. But after that I'm stuck, here's what I have so far....I haven't added the insertion sort to all of the files/arrays cause I'm trying to get my counter right before I get too far.

 
#include <iostream>
#include <fstream>
using namespace std;

int sortArray(int [], int);
int insertion_sort(int [], int);


int main()
{
	const int ARRAY_SIZE1 = 8;
	const int ARRAY_SIZE2 = 16;
	const int ARRAY_SIZE3 = 32;
	const int ARRAY_SIZE4 = 64;
	const int ARRAY_SIZE5 = 128;
	const int ARRAY_SIZE6 = 256;
	const int ARRAY_SIZE7 = 128;
	const int ARRAY_SIZE8 = 128;
	int numbers[ARRAY_SIZE1];
	int numbers2[ARRAY_SIZE2];
	int numbers3[ARRAY_SIZE3];
	int numbers4[ARRAY_SIZE4];
	int numbers5[ARRAY_SIZE5];
	int numbers6[ARRAY_SIZE6];
	int numbers7[ARRAY_SIZE7];
	int numbers8[ARRAY_SIZE8];
	int count, count2, count3, count4, count5, count6, count7, count8;
	ifstream inputFile;
	ifstream inputFile2;
	ifstream inputFile3;
	ifstream inputFile4;
	ifstream inputFile5;
	ifstream inputFile6;
	ifstream inputFile7;
	ifstream inputFile8;
	ofstream outputFile;
	ofstream outputFile2;
	ofstream outputFile3;
	ofstream outputFile4;
	ofstream outputFile5;
	ofstream outputFile6;
	ofstream outputFile7;
	ofstream outputFile8;
	ofstream outputFile9;
	ofstream outputFile10;
	ofstream outputFile11;
	ofstream outputFile12;
	ofstream outputFile13;
	ofstream outputFile14;
	ofstream outputFile15;
	ofstream outputFile16;


	
//Num 8
	//Read contents of file into array
	inputFile.open("NUM8.txt");
	for (count = 0; count < ARRAY_SIZE1; count++)
		inputFile >> numbers[count];
	inputFile.close();

	//Bubble Sort Array
	sortArray(numbers, count);	
	//Write contents of array to file
	outputFile.open("NUM8output.txt");
	
	for (count = 0; count < ARRAY_SIZE1; count++)
		outputFile << numbers[count] << endl;
	
	
	

	//close the file
	outputFile.close();	

	//Insertion Sort Array
	insertion_sort(numbers, count);
	outputFile9.open("NUM8output2.txt");
	for (count = 0; count < ARRAY_SIZE1; count++)
		outputFile9 << numbers[count] << endl;
	outputFile9.close();


//Num 16
		//Read contents of file into array
	inputFile2.open("NUM16.txt");
	for (count2 = 0; count2 < ARRAY_SIZE2; count2++)
		inputFile2 >> numbers2[count2];
	inputFile2.close();

	//Bubble Sort Array
	sortArray(numbers2, count2);
	
	//Write contents of array to file
	outputFile2.open("NUM16output.txt");	

	for (count2 = 0; count2 < ARRAY_SIZE2; count2++)
		outputFile2 << numbers2[count2] << endl;

	//close the file
	outputFile2.close();	


//Num 32
		//Read contents of file into array
	inputFile3.open("NUM32.txt");
	for (count3 = 0; count3 < ARRAY_SIZE3; count3++)
		inputFile3 >> numbers3[count3];
	inputFile3.close();

	//Bubble Sort Array
	sortArray(numbers3, count3);
	
	//Write contents of array to file
	outputFile3.open("NUM32output.txt");	

	for (count3 = 0; count3 < ARRAY_SIZE3; count3++)
		outputFile3 << numbers3[count3] << endl;

	//close the file
	outputFile3.close();	



//Num 64
		//Read contents of file into array
	inputFile4.open("NUM64.txt");
	for (count4 = 0; count4 < ARRAY_SIZE4; count4++)
		inputFile4 >> numbers4[count4];
	inputFile4.close();

	//Bubble Sort Array
	sortArray(numbers4, count4);
	
	//Write contents of array to file
	outputFile4.open("NUM64output.txt");	

	for (count4 = 0; count4 < ARRAY_SIZE4; count4++)
		outputFile4 << numbers4[count4] << endl;

	//close the file
	outputFile4.close();


//Num 128
		//Read contents of file into array
	inputFile5.open("NUM128.txt");
	for (count5 = 0; count5 < ARRAY_SIZE5; count5++)
		inputFile5 >> numbers5[count5];
	inputFile5.close();

	//Bubble Sort Array
	sortArray(numbers5, count5);
	
	//Write contents of array to file
	outputFile5.open("NUM128output.txt");	

	for (count5 = 0; count5 < ARRAY_SIZE5; count5++)
		outputFile5 << numbers5[count5] << endl;

	//close the file
	outputFile5.close();	

//Num 256
		//Read contents of file into array
	inputFile6.open("NUM256.txt");
	for (count6 = 0; count6 < ARRAY_SIZE6; count6++)
		inputFile6 >> numbers6[count6];
	inputFile6.close();

	//Bubble Sort Array
	sortArray(numbers6, count6);
	
	//Write contents of array to file
	outputFile6.open("NUM256output.txt");	

	for (count6 = 0; count6 < ARRAY_SIZE6; count6++)
		outputFile6 << numbers6[count6] << endl;

	//close the file
	outputFile6.close();


//Inv 128
		//Read contents of file into array
	inputFile7.open("INV128.txt");
	for (count7 = 0; count7 < ARRAY_SIZE7; count7++)
		inputFile7 >> numbers7[count7];
	inputFile7.close();

	//Bubble Sort Array
	sortArray(numbers7, count7);
	
	//Write contents of array to file
	outputFile7.open("INV128output.txt");	

	for (count7 = 0; count7 < ARRAY_SIZE7; count7++)
		outputFile7 << numbers7[count7] << endl;

	//close the file
	outputFile7.close();


//Order 128
		//Read contents of file into array
	inputFile8.open("ORDER128.txt");
	for (count8 = 0; count8 < ARRAY_SIZE8; count8++)
		inputFile8 >> numbers8[count8];
	inputFile8.close();

	//Bubble Sort Array
	sortArray(numbers8, count8);
	
	//Write contents of array to file
	outputFile8.open("ORDER128output.txt");	

	for (count8 = 0; count8 < ARRAY_SIZE8; count8++)
		outputFile8 << numbers8[count8] << endl;

	//close the file
	outputFile8.close();


	return 0;
}




//Bubble Sort

int sortArray(int array[], int size)
{
	bool swap;
	int temp;
	int count = 0; 
	do
	{
		swap = false;
		for (int count = 0; count < (size - 1); count++)
		{
			if (array[count] > array[count + 1])
			{
				temp = array[count];
				array[count] = array[count + 1];
				array[count + 1] = temp;
				swap = true;
			}
		}
	} while (swap);
	return count;
}

//Insertion Sort
int insertion_sort(int x[],int length)
{
	int count = 0;
	int key,i;
	for(int j=1;j<length;j++)
	{
		key=x[j];
		i=j-1;
		while(x[i]>key && i>=0)
		{
			count = count + 1;
			x[i+1]=x[i];
			i--;
		}
		x[i+1]=key;
	}
	return count;
}



Any help would be appreciated...thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Returning a value from function

#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Returning a value from function

Posted 16 October 2007 - 12:46 AM

Good cripes, that's a lot of code :)

Is the problem that you're having with the counter the fact that it's apparently not incrementing? If so, the problem would appear to lie here (sorry about the line width, but scroll and look at the inserted comments):
int sortArray(int array[], int size) {
	bool swap;
	int temp;
	int count = 0;		//   <-declared counter variable here
	do {
		swap = false;
		for (int count = 0; count < (size - 1); count++) {	//<-   redeclared and used with local scope here
			if (array[count] > array[count + 1]) {
				temp = array[count];
				array[count] = array[count + 1];
				array[count + 1] = temp;
				swap = true;
			}
		}
	} while (swap);
	return count;
}

So when you return the value from the function, you're just returning your less-local count variable count, which was intialized to zero.

Get rid of the redeclaration of count in your for-loop, and your problem should go away.

Hope that helps,

-jjh

This post has been edited by jjhaag: 16 October 2007 - 12:47 AM

Was This Post Helpful? 0
  • +
  • -

#3 pacit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-October 07

Re: Returning a value from function

Posted 16 October 2007 - 09:35 AM

ok, i took out the redeclaration of count like you said but now I'm getting the errors:

warning C4552: '<' : operator has no effect; expected operator with side-effect
error C2143: syntax error : missing ';' before ')'
error C2059: syntax error : ')'

here's my code with the revision:

int sortArray(int array[], int size)
{
	bool swap;
	int temp;
	int count = 0; 
	do
	{
		swap = false;
		for (count < (size - 1); count++)
		{
			if (array[count] > array[count + 1])
			{
				temp = array[count];
				array[count] = array[count + 1];
				array[count + 1] = temp;
				swap = true;
			}
		}
	} while (swap);
	return count);
}





thanks
Was This Post Helpful? 0
  • +
  • -

#4 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Returning a value from function

Posted 16 October 2007 - 10:44 AM

View Postpacit, on 16 Oct, 2007 - 10:35 AM, said:

ok, i took out the redeclaration of count like you said but now I'm getting the errors:

you still need to define the variable in the control statement of the for loop - you just shouldn't declare it by using the int keyword in front of it:

for (count = 0; count < (size - 1); count++)

that way you don't have the problem of losing the value of the local variable.

-jjh
Was This Post Helpful? 0
  • +
  • -

#5 pacit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-October 07

Re: Returning a value from function

Posted 16 October 2007 - 02:54 PM

alright, so now that function looks like this:

//Bubble Sort

int sortArray(int array[], int size)
{
	bool swap;
	int temp;
	int count = 0; 
	do
	{
		swap = false;
		for (count = 0; count < (size - 1); count++)
		{
			if (array[count] > array[count + 1])
			{
				temp = array[count];
				array[count] = array[count + 1];
				array[count + 1] = temp;
				swap = true;
			}
		}
	} while (swap);
	return (count);
}



and now, when I'm writing to my output file, I'm getting the sorted code with the counter after each number, an example.....after running the program, my output file for the following code:

//Num 8
	//Read contents of file into array
	inputFile.open("NUM8.txt");
	for (count = 0; count < ARRAY_SIZE1; count++)
		inputFile >> numbers[count];
	inputFile.close();

	//Bubble Sort Array
	sortArray(numbers, count);	
	//Write contents of array to file
	outputFile.open("NUM8output.txt");
	
	for (count = 0; count < ARRAY_SIZE1; count++)
	outputFile << numbers[count] << endl << "Counter: " << count << endl;



looks like this:

1
Counter: 0
2
Counter: 1
3
Counter: 2
4
Counter: 3
5
Counter: 4
6
Counter: 5
7
Counter: 6
8
Counter: 7

I mean that looks right, but i'm not sure if it really is.
Was This Post Helpful? 0
  • +
  • -

#6 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Returning a value from function

Posted 16 October 2007 - 04:49 PM

I was under the impression that the reason for returning count from your sort function was so that you could output the number of sorting steps in the file, along with the sorted array.

But now I really have no idea. The output you showed is just the index of the array element, along with it's value. If you're trying to output the number of sorting steps taken, you need to actually store the return value from your sort function in a variable, and output that to the file instead. And only once; it wouldn't need to be in your loop where it would be outputted every time you write an element to the file.

And you may want to stop using the variable count in so many places and for so many different things. I think that it may be confusing what you are actually trying to do here.

If I'm way off base with what you're trying to do, please clarify you problem.

-jjh
Was This Post Helpful? 0
  • +
  • -

#7 pacit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-October 07

Re: Returning a value from function

Posted 16 October 2007 - 05:22 PM

View Postjjhaag, on 16 Oct, 2007 - 04:49 PM, said:

If you're trying to output the number of sorting steps taken, you need to actually store the return value from your sort function in a variable, and output that to the file instead. And only once; it wouldn't need to be in your loop where it would be outputted every time you write an element to the file.

And you may want to stop using the variable count in so many places and for so many different things. I think that it may be confusing what you are actually trying to do here.

If I'm way off base with what you're trying to do, please clarify you problem.

-jjh


ok, that's exactly what i'm trying to do, i'm trying to display whatever my count is whenever the sort is over. On the directions it's telling me that the count variable should end up equal to the total number of times the line with the count is executed.
Was This Post Helpful? 0
  • +
  • -

#8 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Returning a value from function

Posted 16 October 2007 - 06:00 PM

Okay, then you're looking to do just what I said - declare a variable to receive the output from the sort algorithm, and then output it to the file either before or after you output the array, but not in the loop. Something like this:
	//Bubble Sort Array
	int numberCalls8=sortArray(numbers, count);	
	//Write contents of array to file
	outputFile.open("NUM8output.txt");
	
	outputFile << "Number of steps in sorting: " << numberCols8 << endl;
	
	for (count = 0; count < ARRAY_SIZE1; count++)
	outputFile << numbers[count] << endl << "Counter: " << count << endl;


Hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

#9 hayate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-October 07

Re: Returning a value from function

Posted 16 October 2007 - 08:09 PM

substitute your bubble function with this:

//bubble
int sortArray(int array[], int size)
{
	bool swap;
	int temp;
	int count = 0;
	int idx;
	do
	{
		swap = false;
		count++;
		for (idx = 0; idx < (size - count); idx++)
		 if (array[idx] > array[idx + 1])
			{
				temp = array[idx];
				array[idx] = array[idx + 1];
				array[idx + 1] = temp;
				swap = true;
			}
	}
	while (swap);
	return count;
}

Was This Post Helpful? 0
  • +
  • -

#10 pacit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-October 07

Re: Returning a value from function

Posted 16 October 2007 - 08:57 PM

alright then, I got that, thanks for the help...however, I tried doing the same thing with my insertion sort function but my counter isn't working right, it sorts everything fine, the counter just doesn't count anything...

//Insertion Sort
int insertion_sort(int x[],int length)
{
	int count = 0;
	int key,i;
	for(int j=1;j<length;j++)
	{
		key=x[j];
		i=j-1;
		while(x[i]>key && i>=0)
		{
			count = count + 1;
			x[i+1]=x[i];
			i--;
		}
		x[i+1]=key;
	}
	return (count);
}


Was This Post Helpful? 0
  • +
  • -

#11 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Returning a value from function

Posted 16 October 2007 - 09:05 PM

umm...it should be counting just fine. for instance, the return value from insertion_sort() for this array {1,2,4,0,4,4,3,3,2,4} is 14; and it gets larger with increasing array complexity and length.

How is the function being called (post the source for main)? Are you not assigning the return value to an appropriate variable?

-jjh
Was This Post Helpful? 0
  • +
  • -

#12 pacit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-October 07

Re: Returning a value from function

Posted 16 October 2007 - 09:28 PM

I'm calling it in main similar to the way i'm calling my bubble sort in main:

//Insertion Sort Array
	int numberCalls82 = insertion_sort(numbers, count);
	outputFile9.open("NUM8output2.txt");
	for (count = 0; count < ARRAY_SIZE1; count++)
		outputFile9 << numbers[count] << endl;
	outputFile9 << "Number of steps in sorting: " << numberCalls82 << endl;
	outputFile9.close();




also, hayate, i switched my bubble sort with what you put and that seemed to work...thanks.

This post has been edited by pacit: 16 October 2007 - 09:31 PM

Was This Post Helpful? 0
  • +
  • -

#13 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Returning a value from function

Posted 16 October 2007 - 10:45 PM

I didn't notice this the first time through...you shouldn't be passing count to your functions as the length variable - you should be passing ARRAY_SIZE1 instead. If you are calling one of your sort functions for the first time, the variable count will be at its initialized value of zero. When you pass a zero length to your function, it will exit without really doing anything, and return 0.

If you pass ARRAY_SIZE1 as your length argument (or whichever ARRAY_SIZEX variable is appropriate), I think that it should work. But this again gets back to my earlier point of not using the variable count for so many different things in your code - if at any point you forget to set it to the proper value, or forget that it has been set in another statement, the rest of the code execution will suffer.

-jjh
Was This Post Helpful? 0
  • +
  • -

#14 pacit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-October 07

Re: Returning a value from function

Posted 17 October 2007 - 12:55 AM

ok, if it worked for my bubble sort using hayate's wouldn't the same method work for my insertion sort? I've tried changing my insertion sort to kinda resemble what hayate did for my bubble sort but that hasn't been working. If I passed ARRAY_SIZE# to my function, wouldn't that require me to change alot more things?
Was This Post Helpful? 0
  • +
  • -

#15 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Returning a value from function

Posted 17 October 2007 - 01:35 AM

I was referring to passing the ARRAY_SIZE1 variable in your call from main() to the sorting function, not redefining the function itself:
int numberCalls82 = insertion_sort(numbers, ARRAY_SIZE1);

If you can, please post a complete, compilable version next time, with the function prototypes and definitions, and your main() routine. It's a little tough to keep track of the changes from the original source, and you never know where there might have been a small error.

-jjh
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2